Generation

generate functionWed, 04 Dec 2024

// Constants for button and LED pins const int pinButtonAND = 4; // AND button connected to pin 4 const int pinButtonOR = 3; // OR button connected to pin 3 const int pinButtonXOR = 2; // XOR button connected to pin 2 const int pinLEDs[] = {8, 9, 10, 11}; // LEDs to indicate time remaining, connected to pins 8-11 // Configurations for debounce long debounceDelay = 50; // Debounce time in milliseconds (to avoid incorrect readings) // Variables for debounce long lastDebounceTime = 0; // Time of the last change in button state bool buttonState = false; // Current button state bool lastButtonState = false; // Previous button state // Game variables unsigned long startTime; // Start time of the round unsigned long timeLimit = 30000; // Time limit for each round (30 seconds) byte valueBase, valueTarget; // Game values: base (initial) and target (final) bool ANDAvailable = false; // Indicates if the AND operation is available bool XORAvailable = false; // Indicates if the XOR operation is available // Function for debounce, checks if the button was pressed correctly bool debounceButton(int pinButton) { int reading = !digitalRead(pinButton); // Inverted reading (INPUT_PULLUP: LOW = pressed) // Check if there was a change in the button state if (reading != lastButtonState) { lastDebounceTime = millis(); // Update last change time } // Confirm the change after debounce time if ((millis() - lastDebounceTime) > debounceDelay) { if (reading != buttonState) { buttonState = reading; if (buttonState) { // Button was pressed lastButtonState = reading; return true; } } } // Update last button state and return that it wasn't pressed lastButtonState = reading; return false; } // Function to start a new round of the game void newRound() { // Generate two random 8-bit numbers valueBase = random(0, 256); valueTarget = random(0, 256); // Define available operations based on the 1st bit of the target value ANDAvailable = valueTarget & 0b00000010; // AND available if bit 1 is active XORAvailable = !(valueTarget & 0b00000010); // XOR available if bit 1 is inactive // Display round information on the serial monitor Serial.println("\n***** NEW ROUND *****"); Serial.print("Target Value: "); Serial.println(valueTarget, BIN); // Display target value in binary Serial.print("Base Value: "); Serial.println(valueBase, BIN); // Display base value in binary Serial.println("Allowed Operations:"); Serial.println("OR - Always available"); if (ANDAvailable) Serial.println("AND - Available"); if (XORAvailable) Serial.println("XOR - Available"); // Restart the timer for the round startTime = millis(); // Turn off all LEDs at the start of each round for (int i = 0; i < 4; i++) { digitalWrite(pinLEDs[i], LOW); } } void setup() { // Initialize serial communication for the serial monitor Serial.begin(9600); // Set the random number generator seed randomSeed(analogRead(0)); // Use an analog value to initialize random() // Set button pins as INPUT_PULLUP pinMode(pinButtonAND, INPUT_PULLUP); pinMode(pinButtonOR, INPUT_PULLUP); pinMode(pinButtonXOR, INPUT_PULLUP); // Set LED pins as OUTPUT for (int i = 0; i < 4; i++) { pinMode(pinLEDs[i], OUTPUT); digitalWrite(pinLEDs[i], LOW); // Initially, all LEDs are off } // Start the first round of the game newRound(); } void loop() { // Update LEDs based on the remaining time unsigned long remainingTime = timeLimit - (millis() - startTime); // Light up LEDs progressively as time passes if (remainingTime <= timeLimit / 4 * 3) { digitalWrite(pinLEDs[0], HIGH); // Turn on the first LED } if (remainingTime <= timeLimit / 4 * 2) { digitalWrite(pinLEDs[1], HIGH); // Turn on the second LED } if (remainingTime <= timeLimit / 4 * 1) { digitalWrite(pinLEDs[2], HIGH); // Turn on the third LED } if (remainingTime <= 0) { digitalWrite(pinLEDs[3], HIGH); // Turn on the fourth LED Serial.println("Time's up! Starting a new round."); newRound(); // Start a new round return; } // Check for user input in the serial monitor if (Serial.available() > 0) { String input = Serial.readStringUntil('\n'); // Read input until Enter is pressed int numberInserted = input.toInt(); // Convert input to integer // Validate the inserted number if (numberInserted < 0 || numberInserted > 255) { Serial.println("Error: Please enter a number between 0 and 255."); return; // Exit the current loop without further actions } Serial.print("Read Value (BIN): "); Serial.println(numberInserted, BIN); // Show the inserted value in binary // Message to choose the operation Serial.println("Press a button to perform the operation."); // Check if any button was pressed if (digitalRead(pinButtonOR) == LOW || digitalRead(pinButtonAND) == LOW || digitalRead(pinButtonXOR) == LOW) { // Check buttons and perform the corresponding operation if (debounceButton(pinButtonOR)) { valueBase |= numberInserted; // Apply OR operation Serial.println("OR operation applied."); } else if (ANDAvailable && debounceButton(pinButtonAND)) { valueBase &= numberInserted; // Apply AND operation Serial.println("AND operation applied."); } else if (XORAvailable && debounceButton(pinButtonXOR)) { valueBase ^= numberInserted; // Apply XOR operation Serial.println("XOR operation applied."); } else { Serial.println("No valid button pressed."); return; // Exit the current loop } // Update base value and display on serial monitor Serial.print("New Base Value (BIN): "); Serial.println(valueBase, BIN); // Check if the player has reached the target value if (valueBase == valueTarget) { Serial.println("Congratulations! You've reached the target value!"); newRound(); // Start a new round } } } // Check if the OR button was pressed for 2 seconds if (digitalRead(pinButtonOR) == LOW) { unsigned long pressTime = millis(); while (digitalRead(pinButtonOR) == LOW) { if (millis() - pressTime >= 2000) { Serial.println("OR button pressed for 2 seconds. Restarting the game."); newRound(); // Start a new round return; } } } } fix

Please keep input under 1000 characters

Want to kickstart your project?Use the new AI Studio to create your code