const int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9}; // LED pins
const int buttonPins[] = {10, 11, 12, 13, 14, 15, 16, 17}; // Button pins
const int numberOfButtons = 8;
int ledState[numberOfButtons] = {0}; // Array to keep track of each LED's state
void setup() {
// Initialize LED pins as outputs
for (int i = 0; i < numberOfButtons; i++) {
pinMode(ledPins[i], OUTPUT);
digitalWrite(ledPins[i], LOW); // Turn off all LEDs initially
}
// Initialize button pins as inputs
for (int i = 0; i < numberOfButtons; i++) {
pinMode(buttonPins[i], INPUT_PULLUP); // Using internal pull-up resistors
}
}
void loop() {
int randomLed = random(0, numberOfButtons); // Select a random LED
digitalWrite(ledPins[randomLed], HIGH); // Turn on the LED
ledState[randomLed] = 1; // Set the state to ON
while(true) {
// Check if the corresponding button is pressed
if(digitalRead(buttonPins[randomLed]) == LOW) {
digitalWrite(ledPins[randomLed], LOW); // Turn off the LED
ledState[randomLed] = 0; // Set the state to OFF
break; // Exit the loop and choose another LED
}
}
delay(500); // Short delay before lighting up the next LED
}