Whack-a-LED game
A random LED lights and you must press its matching button before it goes out, with the speed rising as your score grows.
Build it step by step
0 of 8 doneInstall the library
Search for TM1637Display in the Library Manager.
Wire the four LED and button pairs
Keep LED 1 beside button 1 so play feels natural.
Wire the display
CLK to D10, DIO to D11, power from 5V.
Upload the sketch
LEDs chase and the display shows 0.
Press any button
The game starts after a short pause.
Play a round
Press the button next to the lit LED.
Miss one on purpose
Four flashes and the score shows.
Power-cycle the board
The best score is still shown.
How do you build a whack-a-LED game?
A random LED lights and you must press its matching button before it goes out, with the speed rising as your score grows. You need Arduino Nano, LED 5 mm red × 4, Tactile push button 6 mm × 4, Resistor 220 Ω × 4, TM1637 4-digit display, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 8 steps and about 120 minutes.
- Level
- Beginner
- Time
- About 120 min
- Parts
- 7
Parts list
- Development BoardsBuyArduino Nano × 1
- SemiconductorsBuyLED 5 mm red × 4
- SwitchesBuy
- PassivesBuyResistor 220 Ω × 4
- DisplaysBuy
- PrototypingBuy
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| Four LEDs plus | Nano D2, D3, D4, D5, each through a 220 ohm resistor, minus to GND | One LED per button |
| Four buttons, one side each | Nano D6, D7, D8, D9, other side to GND | Internal pull-ups, pressed reads LOW |
| TM1637 CLK and DIO | Nano D10 and D11 | Score display |
| TM1637 VCC and GND | Nano 5V and GND | Display power |
| Nano A0 | Leave unconnected | Floating pin gives a random seed |
Code
Each round the Nano waits a random short time, lights one random LED and watches for a press. If the matching button is pressed inside the time window you score a point and the window gets 40 milliseconds shorter, down to a minimum of 350 milliseconds. A wrong button or a timeout ends the game with four flashes. The best score is stored in EEPROM with a marker byte so the first power-up does not show random memory content, and EEPROM.update only writes when the value changes, which saves wear. Install the TM1637Display library by Avishay Orpaz from the Library Manager.
Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
#include <TM1637Display.h>
#include <EEPROM.h>
const uint8_t LED[4] = {2, 3, 4, 5};
const uint8_t BTN[4] = {6, 7, 8, 9};
TM1637Display disp(10, 11); // CLK, DIO
const int START_WINDOW = 1200; // ms to react in the first round
const int MIN_WINDOW = 350;
const int STEP = 40; // window shrinks by this each hit
int best = 0;
int readPress() {
for (uint8_t i = 0; i < 4; i++) if (digitalRead(BTN[i]) == LOW) return i;
return -1;
}
void allLeds(bool on) {
for (uint8_t i = 0; i < 4; i++) digitalWrite(LED[i], on);
}
void waitForStart() {
disp.showNumberDec(best);
uint8_t k = 0;
while (readPress() < 0) { // chase until a button is pressed
allLeds(false);
digitalWrite(LED[k % 4], HIGH);
k++;
delay(150);
}
allLeds(false);
while (readPress() >= 0) delay(10); // wait for release
delay(400);
}
void setup() {
for (uint8_t i = 0; i < 4; i++) {
pinMode(LED[i], OUTPUT);
pinMode(BTN[i], INPUT_PULLUP);
}
disp.setBrightness(5);
randomSeed(analogRead(A0));
if (EEPROM.read(0) == 0xA5) best = EEPROM.read(1); // saved best score
}
void loop() {
waitForStart();
int score = 0;
int window = START_WINDOW;
bool alive = true;
while (alive) {
disp.showNumberDec(score);
delay(random(250, 700));
uint8_t target = random(4);
digitalWrite(LED[target], HIGH);
unsigned long t0 = millis();
int pressed = -1;
while (millis() - t0 < (unsigned long)window && pressed < 0) pressed = readPress();
digitalWrite(LED[target], LOW);
if (pressed == target) {
score++;
if (window > MIN_WINDOW) window -= STEP;
while (readPress() >= 0) delay(10);
} else {
alive = false; // wrong button or too slow
}
}
for (uint8_t n = 0; n < 4; n++) { allLeds(true); delay(150); allLeds(false); delay(150); }
if (score > best) {
best = score;
EEPROM.update(0, 0xA5);
EEPROM.update(1, (uint8_t)min(best, 255));
}
disp.showNumberDec(score);
delay(1500);
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Press the matching button quickly | Score goes up by one |
| Wait without pressing | Game over after the window |
| Press a wrong button | Game over at once |
| Reach score 10 | Lights stay on for a shorter time than at score 1 |
| Power off and on | Best score is kept |
If it does not work
Display shows nothing
Check CLK and DIO are not swapped and that you installed the right library.
Same LED comes up every time
A0 must be left floating, not tied to ground.
Game ends right away
A button is stuck or wired to GND all the time.
Two LEDs seem to light
Check the D2 to D5 wiring for a shared resistor.
Best score does not save
Use EEPROM.update as shown, not a loop that never finishes.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the whack-a-LED game?
Arduino Nano, LED 5 mm red × 4, Tactile push button 6 mm × 4, Resistor 220 Ω × 4, TM1637 4-digit display, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).
How long does the whack-a-LED game take to build?
About 120 minutes for a beginner maker, in 8 steps.
How do I know the whack-a-LED game works?
Press the matching button quickly: Score goes up by one. Wait without pressing: Game over after the window. Press a wrong button: Game over at once.
Display shows nothing: what should I check?
Check CLK and DIO are not swapped and that you installed the right library.
Same LED comes up every time: what should I check?
A0 must be left floating, not tied to ground.
Game ends right away: what should I check?
A button is stuck or wired to GND all the time.
Two LEDs seem to light: what should I check?
Check the D2 to D5 wiring for a shared resistor.
