Projects / Three-lane rhythm tapper

Three-lane rhythm tapper

Lights run down three lanes and you tap the matching button as each one reaches the end, scoring for timing and getting faster as you go. A quick rhythm game that shows how reaction time and timing windows work.

Intermediate1.7 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. NANO

    Wire the three buttons

    D2, D3 and D4, each other leg to GND.

  2. NANO13.5 mA

    Wire the three LEDs

    D5 to D7, each through a 220 ohm resistor, short legs to GND.

  3. NANO13.5 mASSD1306

    Wire the buzzer and OLED

    Buzzer to D8 and GND, OLED to A4, A5, 5V and GND.

  4. NANO

    Upload the sketch

    Three boxes appear on the screen.

  5. NANO

    Wait for a note

    It falls down one lane.

  6. NANO13.5 mA

    Press the matching button

    While its LED is lit, for a point and a high beep.

  7. NANO

    Play on

    Until your five lives run out.

In short

How do you build a three-lane rhythm tapper?

Lights run down three lanes and you tap the matching button as each one reaches the end, scoring for timing and getting faster as you go. A quick rhythm game that shows how reaction time and timing windows work. You need Arduino Nano, Tactile push button 6 mm × 3, LED 5 mm red × 3, Resistor 220 Ω × 3, Passive buzzer, SSD1306 OLED Display, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 7 steps and about 100 minutes.

Level
Intermediate
Time
About 100 min
Parts
8

Parts list

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

Wiring

FromToWhy
Lane buttons 1 to 3Nano D2, D3 and D4 and GNDLeft, middle and right lane
Lane LEDs 1 to 3 long legsNano D5, D6 and D7, each through a 220 ohm resistorLight when a note is in the hit zone
LED short legsNano GNDReturn
Passive buzzer + and -Nano D8 and GNDHit and miss sounds
OLED SDA and SCLNano A4 and A5Shows the falling notes
OLED VCC and GNDNano 5V and GNDPower for a 0.96 inch module

Code

One note at a time drops down one of three lanes on the screen. The lane's LED lights while the note is inside the hit zone, a box near the bottom, and you score by pressing that lane's button during that time. A press in the wrong lane or too early counts as a miss, and so does letting the note fall past. You have five lives, the notes speed up after every ten, and any button restarts after the game is over. The timing window is the height of the zone divided by the speed: at a speed of 2 pixels per frame and about 33 frames a second, the window is long enough to be easy, and by speed 6 it is only a few frames, which is why the game gets hard. The sketch uses a 30 millisecond frame timer instead of delay, so button presses are never missed. Install "Adafruit SSD1306" and "Adafruit GFX" from the Library Manager. Try changing ZONE_TOP to make the window shorter, or keep a longest streak score.

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).

// Libraries: Adafruit SSD1306 and Adafruit GFX.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

Adafruit_SSD1306 oled(128, 64, &Wire, -1);

const uint8_t BTN[3] = {2, 3, 4};
const uint8_t LED[3] = {5, 6, 7};
const uint8_t BUZZ = 8;

const int ZONE_TOP = 46;                 // a note is hittable between these two rows
const int ZONE_BOTTOM = 62;
const int NOTE_H = 6;
const uint8_t MAX_LIVES = 5;

bool prevDown[3];
bool active = false;
bool resolved = false;
int lane = 0;
int y = -NOTE_H;
int speedPx = 2;
uint8_t beats = 0;
uint8_t lives = MAX_LIVES;
unsigned int score = 0;
bool gameOver = false;
unsigned long lastFrame = 0;
unsigned long nextAt = 0;

bool inZone() {
  return (y + NOTE_H >= ZONE_TOP) && (y <= ZONE_BOTTOM);
}

void restart() {
  active = false;
  resolved = false;
  y = -NOTE_H;
  speedPx = 2;
  beats = 0;
  lives = MAX_LIVES;
  score = 0;
  gameOver = false;
  nextAt = millis() + 800;
}

void miss() {
  resolved = true;
  tone(BUZZ, 200, 150);
  if (lives > 0) lives--;
  if (lives == 0) gameOver = true;
}

void draw() {
  oled.clearDisplay();
  oled.setTextSize(1);
  oled.setTextColor(SSD1306_WHITE);
  oled.setCursor(0, 0);
  if (gameOver) {
    oled.print(F("GAME OVER  Score "));
    oled.print(score);
    oled.setCursor(0, 12);
    oled.print(F("Press any button"));
  } else {
    oled.print(F("Score "));
    oled.print(score);
    oled.print(F("  Lives "));
    oled.print(lives);
  }
  for (uint8_t i = 0; i < 3; i++) {
    int x = 8 + i * 42;
    oled.drawRect(x, ZONE_TOP, 26, ZONE_BOTTOM - ZONE_TOP + 1, SSD1306_WHITE);   // hit zone
    if (active && !resolved && !gameOver && i == lane) oled.fillRect(x + 4, y, 18, NOTE_H, SSD1306_WHITE);
  }
  oled.display();
}

void setup() {
  for (uint8_t i = 0; i < 3; i++) {
    pinMode(BTN[i], INPUT_PULLUP);
    pinMode(LED[i], OUTPUT);
  }
  pinMode(BUZZ, OUTPUT);
  Serial.begin(9600);
  oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  randomSeed(analogRead(A0));
  restart();
}

void loop() {
  unsigned long now = millis();

  for (uint8_t i = 0; i < 3; i++) {
    bool down = (digitalRead(BTN[i]) == LOW);
    bool pressed = down && !prevDown[i];
    prevDown[i] = down;
    if (!pressed) continue;
    if (gameOver) { restart(); continue; }
    if (active && !resolved) {
      if (i == lane && inZone()) {
        resolved = true;
        score++;
        tone(BUZZ, 1800, 80);
      } else {
        miss();                                   // wrong lane or too early
      }
    }
  }

  if (!active && !gameOver && now >= nextAt) {    // start a new note
    lane = random(3);
    y = -NOTE_H;
    active = true;
    resolved = false;
    beats++;
    if (beats % 10 == 0 && speedPx < 6) speedPx++;
  }

  if (now - lastFrame >= 30) {
    lastFrame = now;
    if (active) {
      y += speedPx;
      if (y > 63 || (resolved && !inZone())) {
        if (!resolved) miss();                    // note fell past the zone
        active = false;
        nextAt = now + 350;
      }
    }
    for (uint8_t i = 0; i < 3; i++)
      digitalWrite(LED[i], (active && !resolved && !gameOver && i == lane && inZone()) ? HIGH : LOW);
    draw();
  }
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Note in the zoneThat lane's LED lights
Correct button in timeScore goes up with a high beep
Wrong lane or early pressLow beep and a lost life
Every ten notesThe notes fall faster
Five missesGame over screen, any button restarts

If it does not work

Blank screen

Check the OLED wires and try address 0x3D.

Notes never appear

Check the sketch uploaded and the OLED shows the boxes.

A lane never scores

Check the button goes to its pin and to GND.

LED stays dark

Check its direction and the 220 ohm resistor.

Too hard or too easy

Change the starting speedPx or the zone height.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the three-lane rhythm tapper?

Arduino Nano, Tactile push button 6 mm × 3, LED 5 mm red × 3, Resistor 220 Ω × 3, Passive buzzer, SSD1306 OLED Display, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).

How long does the three-lane rhythm tapper take to build?

About 100 minutes for a intermediate maker, in 7 steps.

How do I know the three-lane rhythm tapper works?

Note in the zone: That lane's LED lights. Correct button in time: Score goes up with a high beep. Wrong lane or early press: Low beep and a lost life.

Blank screen: what should I check?

Check the OLED wires and try address 0x3D.

Notes never appear: what should I check?

Check the sketch uploaded and the OLED shows the boxes.

A lane never scores: what should I check?

Check the button goes to its pin and to GND.

LED stays dark: what should I check?

Check its direction and the 220 ohm resistor.