// To reduce the number of LEDs
const byte MAX_LED = 4;
// Number of blue LEDs
const byte MAX_BLUE = 2;
// Pin definitions
const int buttonPin[] = {40, 41, 42, 43, 44, 45, 46, 47, 48, 49}; // Array of button pins
const int ledGreenPin[] = {12, 9, 6, 3, 23, 26, 29, 32, 35, 38}; // Array of green LED pins
const int ledBluePin[] = {13, 10, 7, 4, 22, 25, 28, 31, 34, 37}; // Array of blue LED pins
const int startButtonPin = 50; // Start button pin
// Variables
int blueLEDs[5]; // Array to store indices of LEDs that will light up blue
int selectedButton = -1; // Variable to store the index of the selected button
bool gameStarted = false; // Flag to indicate if the game has started
void setup() {
Serial.begin(9600);
// Initialize LED and button pins
for (int i = 0; i < MAX_LED; i++) {
pinMode(ledGreenPin[i], OUTPUT);
pinMode(ledBluePin[i], OUTPUT);
digitalWrite(ledGreenPin[i], LOW);
digitalWrite(ledBluePin[i], LOW);
pinMode(buttonPin[i], INPUT_PULLUP);
}
pinMode(startButtonPin, INPUT_PULLUP);
// Seed random number generator
randomSeed(analogRead(0));
Serial.println("RUNNING");
}
void loop() {
if (!gameStarted && digitalRead(startButtonPin) == LOW) {
startGame();
}
if (gameStarted) {
// Check if any button is pressed
for (int i = 0; i < MAX_LED; i++) {
if (digitalRead(buttonPin[i]) == LOW) {
selectedButton = i;
break;
}
}
// If a button is pressed, check if it corresponds to a blue LED
if (selectedButton != -1) {
for (int i = 0; i < MAX_BLUE; i++) {
if (selectedButton == blueLEDs[i]) {
// Button corresponds to a blue LED
digitalWrite(ledBluePin[selectedButton], LOW); // Turn off blue LED
delay(500); // Delay for visual feedback
digitalWrite(ledGreenPin[selectedButton], HIGH); // Turn on green LED
selectedButton = -1; // Reset selected button
break;
}
}
}
}
}
void startGame() {
// Turn off all LEDs
for (int i = 0; i < MAX_LED; i++) {
digitalWrite(ledGreenPin[i], LOW);
digitalWrite(ledBluePin[i], LOW);
}
// Generate random blue LEDs
for (int i = 0; i < MAX_BLUE; i++) {
blueLEDs[i] = random(MAX_LED);
digitalWrite(ledBluePin[blueLEDs[i]], HIGH); // Turn blue LED on
Serial.print("Blue ");Serial.print(i); Serial.print("=");Serial.println(blueLEDs[i]);
}
gameStarted = true;
Serial.println("STARTED");
}