Built-in project · Arduino Uno R3

Reaction duel on an Arduino Uno

Two buttons, three lamps and a piezo: the Uno lights the middle lamp and the first press wins. The whole build, an Arduino Uno and 9 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Intermediate About 20 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $33. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Reading two buttons with internal pull-ups
  • Random delays for a fair start
  • Timing a reaction with millis
  • Rules and edge cases: false start, tie, no press
  • Splitting a program into small helper functions
  • Difficulty: Intermediate
  • Time: About 20 minutes
  • Board: Arduino Uno R3
  • Parts: 11

How it works

Two players each have a button, on pins 2 and 3. A round starts when both buttons are up. The sketch waits a random time between 1.5 and 4 seconds, then lights the white go lamp on pin 9.

The first button pressed after the lamp lights wins. The winner's lamp lights, red on pin 10 for player one or blue on pin 11 for player two. The Serial Monitor prints the reaction time in milliseconds and the gap to the other player.

Pressing before the go lamp is a false start. The round goes to the other player. A gap under 20 ms is reported as a whisker.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
LED, red pin CArduino Uno R3 pin GND
LED, white pin CArduino Uno R3 pin GND
LED, blue pin CArduino Uno R3 pin GND
Pushbutton (1) pin 2aArduino Uno R3 pin GND
Pushbutton (2) pin 2aArduino Uno R3 pin GND
Piezo speaker pin 2Arduino Uno R3 pin GND
Resistor, 220 Ω (3) pin 1Arduino Uno R3 pin 11
Resistor, 220 Ω (1) pin 1Arduino Uno R3 pin 10
Resistor, 220 Ω (2) pin 1Arduino Uno R3 pin 9
Piezo speaker pin 1Arduino Uno R3 pin 7
Pushbutton (2) pin 1aArduino Uno R3 pin 3
Pushbutton (1) pin 1aArduino Uno R3 pin 2
LED, red pin AResistor, 220 Ω (1) pin 2
LED, white pin AResistor, 220 Ω (2) pin 2
LED, blue pin AResistor, 220 Ω (3) pin 2

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Reaction duel: two players, one lamp, and whoever presses first wins.
//
//   pin 9   the go lamp, through 220 ohm to GND
//   pin 10  player one's lamp        pin 2  player one's button, to GND
//   pin 11  player two's lamp        pin 3  player two's button, to GND
//   pin 7   a passive piezo straight to GND
//
// Both buttons use the pad's internal pull-up, so a button is one wire to the
// pin and one to ground and nothing else. A round waits a random one and a
// half to four seconds, lights the go lamp, and then watches both pins until
// one of them falls.
//
// Pressing before the lamp is a false start and hands the round to the other
// player, which is the rule that makes the game worth playing: you cannot win
// by holding the button down.
//
// The margin is the gap between the two presses. Anything under twenty
// milliseconds is a tie as far as a person is concerned, and the sketch says
// so rather than printing a number nobody could have felt.

#include "mokxi_tone.h"

const int GO_LAMP = 9;
const int LAMP_A = 10;
const int LAMP_B = 11;
const int BUTTON_A = 2;
const int BUTTON_B = 3;
const int PIEZO = 7;

/** How long the round waits for the second player before calling it. */
const long LATE_MS = 400;

static bool downA() { return digitalRead(BUTTON_A) == LOW; }
static bool downB() { return digitalRead(BUTTON_B) == LOW; }

static void win(int lamp, const char *who, long ms, long margin) {
  digitalWrite(GO_LAMP, LOW);
  digitalWrite(lamp, HIGH);
  Serial.print(who);
  Serial.print(" wins in ");
  Serial.print(ms);
  Serial.print(" ms, by ");
  if (margin < 20) {
    Serial.println("a whisker");
  } else {
    Serial.print(margin);
    Serial.println(" ms");
  }
  mokxiTone(PIEZO, NOTE_C5, 90);
  mokxiTone(PIEZO, NOTE_G5, 140);
  delay(900);
  digitalWrite(lamp, LOW);
}

static void falseStart(const char *who, int otherLamp) {
  Serial.print(who);
  Serial.println(" jumped the lamp: the round goes the other way");
  mokxiTone(PIEZO, 160, 250);
  digitalWrite(otherLamp, HIGH);
  delay(700);
  digitalWrite(otherLamp, LOW);
}

void setup() {
  pinMode(GO_LAMP, OUTPUT);
  pinMode(LAMP_A, OUTPUT);
  pinMode(LAMP_B, OUTPUT);
  pinMode(PIEZO, OUTPUT);
  pinMode(BUTTON_A, INPUT_PULLUP);
  pinMode(BUTTON_B, INPUT_PULLUP);
  Serial.begin(115200);
  Serial.println("Mokxi Arduino Uno: reaction duel");
  Serial.println("wait for the middle lamp, then press first");
}

void loop() {
  digitalWrite(GO_LAMP, LOW);
  digitalWrite(LAMP_A, LOW);
  digitalWrite(LAMP_B, LOW);

  // a round starts with both buttons up
  while (downA() || downB()) {
    delay(10);
  }
  delay(400);

  unsigned long wait = 1500UL + (unsigned long)random(2500);
  unsigned long start = millis();
  while (millis() - start < wait) {
    if (downA()) {
      falseStart("player one", LAMP_B);
      return;
    }
    if (downB()) {
      falseStart("player two", LAMP_A);
      return;
    }
    delay(2);
  }

  digitalWrite(GO_LAMP, HIGH);
  unsigned long lit = millis();
  int winner = 0;
  long winnerMs = 0;
  long loserMs = -1;

  while (millis() - lit < 5000UL) {
    long now = (long)(millis() - lit);
    if (winner == 0) {
      if (downA()) {
        winner = 1;
        winnerMs = now;
      } else if (downB()) {
        winner = 2;
        winnerMs = now;
      }
    } else if (winner == 1 && downB()) {
      loserMs = now;
      break;
    } else if (winner == 2 && downA()) {
      loserMs = now;
      break;
    } else if (now - winnerMs > LATE_MS) {
      break;
    }
    delay(1);
  }

  if (winner == 0) {
    Serial.println("nobody pressed: round abandoned");
    mokxiTone(PIEZO, 220, 200);
    return;
  }

  long margin = loserMs < 0 ? LATE_MS : loserMs - winnerMs;
  if (winner == 1) {
    win(LAMP_A, "player one", winnerMs, margin);
  } else {
    win(LAMP_B, "player two", winnerMs, margin);
  }
}

Code walkthrough

The key functions of the sketch, and what each one does.

downA sketch.ino

Returns true when player one's button is held down. A button wired to ground with a pull-up reads LOW when pressed, so the check is for LOW. downB does the same for player two.

static bool downA() { return digitalRead(BUTTON_A) == LOW; }

falseStart sketch.ino

Called when someone presses during the random wait. It prints who jumped the lamp, plays a low tone, and lights the other player's lamp for 700 ms to show they get the round.

static void falseStart(const char *who, int otherLamp) {
  Serial.print(who);
  Serial.println(" jumped the lamp: the round goes the other way");
  mokxiTone(PIEZO, 160, 250);
  digitalWrite(otherLamp, HIGH);
  delay(700);
  digitalWrite(otherLamp, LOW);
}

win sketch.ino

Turns off the go lamp, lights the winner's lamp and prints the winner's time plus the margin. It plays two rising notes on the piezo. A margin under 20 ms prints as a whisker, because nobody could feel such a gap.

static void win(int lamp, const char *who, long ms, long margin) {
  digitalWrite(GO_LAMP, LOW);
  digitalWrite(lamp, HIGH);
  Serial.print(who);
  Serial.print(" wins in ");
  Serial.print(ms);
  Serial.print(" ms, by ");
  if (margin < 20) {
    Serial.println("a whisker");
  } else {
    Serial.print(margin);
    Serial.println(" ms");
  }
  mokxiTone(PIEZO, NOTE_C5, 90);
  mokxiTone(PIEZO, NOTE_G5, 140);
  delay(900);
  digitalWrite(lamp, LOW);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • A round starts or ends on its own This project
    A button stuck down, or wired to the wrong side, reads as pressed. The round only starts once both buttons are up.
  • Wrong lamp lights for a player This project
    Check that player one's button is on pin 2 with the red LED on pin 10, and player two's is on pin 3 with the blue LED on pin 11.
  • The piezo stays silent This project
    The piezo goes between pin 7 and ground.
  • The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
    Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
  • A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
    Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground.
  • The LED does not light, or it lights once and then dies. LED
    LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin.
  • A blue or white LED stays dark on 3.3 V. LED
    Blue and white LEDs drop about 3 V, so 3.3 V leaves almost nothing for the resistor. Use a 5 V supply, or a smaller resistor and a red or green LED.
  • The input reads random HIGH and LOW when the button is not pressed. Pushbutton
    A floating pin picks up noise. Add a 10 k pull-down or pull-up resistor, or use INPUT_PULLUP and wire the button between the pin and GND. Then pressed reads LOW.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.