Arduino LED Reflex Game - Test Your Reaction Speed!
by 900006 in Circuits > Arduino
9 Views, 0 Favorites, 0 Comments
Arduino LED Reflex Game - Test Your Reaction Speed!


The goal of this project is to create an interactive LED Reflex Game using an Arduino. The game lights up LEDs in sequence, and the player must press a button when the red LED lights up. If the button is pressed in time, the buzzer beeps and the player scores a point. This game challenges your reaction time and is fun and easy to play. It uses simple components and basic coding but allows for customization and enhancements. Perfect for beginners aiming for an A+ project.
Supplies









Components Needed:
- Arduino UNO or compatible board
- Breadboard
- 12 LEDs (11 green, 1 red)
- 12 resistors (220Ω) or (330Ω)
- 1 push button
- 1 piezo buzzer
- Jumper wires/ Jumpers
Place LEDs

Start by placing all 12 LEDs on the breadboard. First 5 green LEDs, and then one red LED, and then the remaining 6 green LEDs.
Place Resistors (220Ω) or (330Ω)

Connect the cathode that is a short led of each LED to one end of (220Ω) or (330Ω) resistor. Connect the other end of the resistor to the negative rail of the breadboard.
Arduino Pins

Now where the LED anodes the long lead to the Arduino pins. First five green LEDs to digital pin D2, D3, D4, D5, and D6. And then the anode of red LED to pin D7. Then connect the remaining anodes od green LEDs to pin D9, D9, D10, D11, and D12 in proper sequence.
Push Button

Next, place the push button on the breadboard. Connect one leg of the button to the negative rail. Connect the other leg to analog pin A1.
Buzzer

And now for the buzzer. Connect the negative leg to the negative rail and the positive leg to analog pin A2.
Connect Ground

Finally connect the G&D of Arduino to the negative rail.
Check Connection

Double check the connection with he circuit diagram to avoid short circuits.
Code Timeeee!

Here is the code:
// Define pin numbers
const int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
const int redLedPin = 7; // Among them pin 7 is red LED. That's our target LED.
const int buttonPin = A1; // We also set up the push button on analog pin A1
const int buzzerPin = A2; // The buzzer is on A2
int totalLeds = 12;
int currentLed = 0;
bool gamePaused = false;
// We're keeping track of the total number of LEDs, current active LED and whether the game is paused or not.
void setup() {
// Set all LED pins as OUTPUT
for (int i = 0; i < totalLeds; i++) {
pinMode(ledPins[i], OUTPUT);
}
pinMode(buttonPin, INPUT_PULLUP); // use internal pull-up resistor
pinMode(buzzerPin, OUTPUT);
// Turn off all LEDs and buzzer
turnOffAllLeds();
digitalWrite(buzzerPin, LOW);
}
// In the setup function, we set all the LED pins as output. The push button is set as a input_pullup to use as Arduino's internal resistor. The buzzer is set to output. Then we turn off all LEDs and mute the buzzer initially.
void loop() {
if (!gamePaused) {
// Turn off all LEDs
turnOffAllLeds();
// Turn on current LED
digitalWrite(ledPins[currentLed], HIGH);
delay(100); // Adjust scrolling speed here
// The loop function keeps the game running. If the game is not paused, it's turn off all LEDs. Then it lights up the Current LED, wait for 100 milliseconds and then control the speed of the scrolling light.
// Check for button press
if (digitalRead(buttonPin) == LOW) {
gamePaused = true;
delay(200); // simple debounce
checkResult();
}
// If the button is pressed, the game is paused and it checks whether the player stopped at the red LED.
// Move to next LED
// Finally, it moves the current LED to the next one in the list and loops again.
currentLed = (currentLed + 1) % totalLeds;
}
}
void checkResult() {
if (ledPins[currentLed] == redLedPin) {
// Correct stop
happyBeep();
} else {
// Wrong stop
sadBeep();
}
delay(2000); // Wait before restarting game
gamePaused = false;
}
void happyBeep() {
tone(buzzerPin, 1000, 200); // High tone
delay(300);
tone(buzzerPin, 1500, 200); // Higher tone
delay(300);
noTone(buzzerPin);
}
void sadBeep() {
tone(buzzerPin, 400, 400); // Low tone
delay(500);
noTone(buzzerPin);
}
void turnOffAllLeds() {
for (int i = 0; i < totalLeds; i++) {
digitalWrite(ledPins[i], LOW);
}
}
Connect to Computer & Play!



Now upload the code to the Arduino. Connect the Arduino UNO to your computer using the USB cable for uploading the code. Select port and board in the same menu and select Arduino UNO. Now clock on the upload button. Once uploaded, Power your Arduino. The LEDs light up one by one in sequence like a chaser. The red LED is a target LED. When you press the button exactly when the red LED lights up, you win. If you press it at the wrong time, the buzzer gives you a sad tone. A successful press triggers a happy double beat.
Will you get the happy beat or the sad buzz?