int leds[5] = {13, 12, 11, 10, 9}; // pin numbers for the LEDs
int buttons[5] = {2, 3, 4, 5, 6}; // pin numbers for the buttons
int order[5]; // array to hold the order of the buttons
int current_button = 0; // current button to light up
int num_buttons = 5; // number of buttons in the game
int player1_score = 0; // player 1's score
int player2_score = 0; // player 2's score
void setup() {
randomSeed(analogRead(0)); // seed the random number generator
Serial.begin(9600); // initialize serial communication
pinMode(LED_BUILTIN, OUTPUT); // initialize built-in LED as output
for (int i = 0; i < num_buttons; i++) {
pinMode(leds[i], OUTPUT); // initialize LEDs as output
pinMode(buttons[i], INPUT_PULLUP); // initialize buttons as input
order[i] = i; // initialize order array
}
shuffleButtons(); // shuffle the order of the buttons
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn on built-in LED
for (int i = 0; i < num_buttons; i++) {
digitalWrite(leds[order[i]], HIGH); // light up the next button
delay(500); // wait for half a second
digitalWrite(leds[order[i]], LOW); // turn off the LED
delay(500); // wait for half a second
}
current_button = 0; // start with the first button
while (current_button < num_buttons) {
int player = current_button % 2 + 1; // determine which player is playing
int button = waitForButton(player); // wait for the player to press the button
if (button == buttons[order[current_button]]) { // if it's the right button
if (player == 1) {
player1_score++; // increment player 1's score
} else {
player2_score++; // increment player 2's score
}
current_button++; // move on to the next button
}
}
Serial.print("Player 1 score: ");
Serial.println(player1_score); // print player 1's score
Serial.print("Player 2 score: ");
Serial.println(player2_score); // print player 2's score
delay(1000); // wait for a second before starting again
shuffleButtons(); // shuffle the order of the buttons
}
int waitForButton(int player) {
while (1) {
for (int i = 0; i < num_buttons; i++) {
if (digitalRead(buttons[i]) == LOW) {
return buttons[i]; // return the button that was pressed
}
}
}
}
void shuffleButtons() {
for (int i = 0; i < num_buttons; i++) {
int j = random(i, num_buttons); // choose a random index from i to the end
if (i != j) { // swap the values at index i and j in the order array
int temp = order[i];
order[i] = order[j];
order[j] = temp;
}
}
}