Projects / Whack-a-LED game

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.

Beginner2 hGuided buildReviewed

Build it step by step

0 of 8 done
  1. NANO12:45

    Install the library

    Search for TM1637Display in the Library Manager.

  2. NANO19.4 mA

    Wire the four LED and button pairs

    Keep LED 1 beside button 1 so play feels natural.

  3. NANO

    Wire the display

    CLK to D10, DIO to D11, power from 5V.

  4. NANO19.4 mA

    Upload the sketch

    LEDs chase and the display shows 0.

  5. NANO

    Press any button

    The game starts after a short pause.

  6. NANO19.5 mA

    Play a round

    Press the button next to the lit LED.

  7. NANO

    Miss one on purpose

    Four flashes and the score shows.

  8. NANO

    Power-cycle the board

    The best score is still shown.

In short

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

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
Four LEDs plusNano D2, D3, D4, D5, each through a 220 ohm resistor, minus to GNDOne LED per button
Four buttons, one side eachNano D6, D7, D8, D9, other side to GNDInternal pull-ups, pressed reads LOW
TM1637 CLK and DIONano D10 and D11Score display
TM1637 VCC and GNDNano 5V and GNDDisplay power
Nano A0Leave unconnectedFloating 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.

CheckExpected
Press the matching button quicklyScore goes up by one
Wait without pressingGame over after the window
Press a wrong buttonGame over at once
Reach score 10Lights stay on for a shorter time than at score 1
Power off and onBest 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 →

Ask Lark AI

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.