Projects / Quick-draw duel

Quick-draw duel

Beginner1.5 hGuided buildReviewed

Build it step by step

0 of 8 done
  1. NANO

    Wire the buttons

    D2 and D3 each to GND.

  2. NANO

    Wire the lamps

    Red GO on D5, green winners on D6 and D7, all with 220 ohm.

  3. NANO

    Wire the buzzer

    D8 and GND.

  4. NANO

    Upload the sketch

    Open the serial monitor.

  5. NANO

    Start a round

    Both players hold their buttons for one second.

  6. NANO

    Release and wait

    A random delay of two to six seconds.

  7. NANO

    React

    First press after the red lamp wins.

  8. NANO

    Try a false start

    Press early and see the other player win.

In short

How do you build a quick-draw duel?

Two players wait for a random go signal and the first to press wins, while a press before the signal is a false start. You need Arduino Nano, Tactile push button 6 mm × 2, LED 5 mm red, LED 5 mm green × 2, Resistor 220 Ω × 3, Passive buzzer, Jumper wires male to male (40 pcs, 20 cm) and Breadboard 830 Tie Points. It takes 8 steps and about 90 minutes.

Level
Beginner
Time
About 90 min
Parts
8

Parts list

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

Wiring

FromToWhy
Player 1 buttonNano D2 and GNDPressed pulls D2 low
Player 2 buttonNano D3 and GNDPressed pulls D3 low
Red GO lamp anode220 ohm to Nano D5Lights at the go signal
Player 1 green LED anode220 ohm to Nano D6Lights if player 1 wins
Player 2 green LED anode220 ohm to Nano D7Lights if player 2 wins
All three LED cathodesNano GNDCommon ground
Passive buzzer positiveNano D8Go beep and result tones
Passive buzzer negativeNano GNDCommon ground

Code

A reaction game measures how fast two players respond to a signal, and a fair game must also punish cheating. This duel is a state machine. Both players hold their buttons to start, then must release them before the countdown begins. After a random wait of two to six seconds, the red lamp lights and a beep sounds, and the first player to press wins. A press during the wait is a false start, and the other player wins, so players cannot just hold the button down and hope. The random wait comes from the random function, which the sketch seeds from an unconnected analog pin, because a floating pin picks up different noise each time. The loop checks both buttons in a few microseconds, so two presses must be closer together than that to be a tie, and if the sketch sees both in the same pass, it picks player 1, a rare case you can mention to players. The reaction time in milliseconds goes to the serial monitor. Typical human reaction times to a light are about 200 to 250 milliseconds, which makes a nice target to beat.

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

const int BTN1 = 2, BTN2 = 3, GO_LED = 5, W1 = 6, W2 = 7, BUZZ = 8;

enum State { IDLE, WAIT_RELEASE, COUNTDOWN, GO, DONE };
State state = IDLE;
unsigned long waitUntil = 0, goAt = 0, heldSince = 0;

bool p1() { return digitalRead(BTN1) == LOW; }
bool p2() { return digitalRead(BTN2) == LOW; }

// true when both buttons have been held for 800 ms
bool bothHeld() {
  if (p1() && p2()) {
    if (heldSince == 0) heldSince = millis();
    return millis() - heldSince >= 800;
  }
  heldSince = 0;
  return false;
}

void lightsOff() { digitalWrite(GO_LED, LOW); digitalWrite(W1, LOW); digitalWrite(W2, LOW); }

void win(int player, const char *why) {
  digitalWrite(GO_LED, LOW);
  digitalWrite(player == 1 ? W1 : W2, HIGH);
  tone(BUZZ, 1500, 250);
  Serial.print("Player "); Serial.print(player); Serial.print(" wins "); Serial.println(why);
  state = DONE;
  heldSince = 0;
}

void setup() {
  pinMode(BTN1, INPUT_PULLUP); pinMode(BTN2, INPUT_PULLUP);
  pinMode(GO_LED, OUTPUT); pinMode(W1, OUTPUT); pinMode(W2, OUTPUT);
  randomSeed(analogRead(A0));
  Serial.begin(9600);
  Serial.println("Hold both buttons for a second to start.");
}

void loop() {
  switch (state) {
    case IDLE:
    case DONE:
      if (bothHeld()) {
        lightsOff();
        tone(BUZZ, 800, 100);
        Serial.println("Get ready. Release both buttons.");
        state = WAIT_RELEASE;
      }
      break;

    case WAIT_RELEASE:                           // both must let go before the countdown
      if (!p1() && !p2()) {
        delay(300);
        waitUntil = millis() + random(2000, 6000);
        state = COUNTDOWN;
      }
      break;

    case COUNTDOWN:
      if (p1() && p2()) { tone(BUZZ, 300, 400); Serial.println("Both false-started, nobody wins."); state = DONE; heldSince = 0; }
      else if (p1()) win(2, "(player 1 false start)");
      else if (p2()) win(1, "(player 2 false start)");
      else if (millis() >= waitUntil) {
        digitalWrite(GO_LED, HIGH);
        tone(BUZZ, 2000, 150);
        goAt = millis();
        state = GO;
      }
      break;

    case GO:
      if (p1() || p2()) {
        unsigned long rt = millis() - goAt;
        int w = p1() ? 1 : 2;                    // if both are seen in the same pass, player 1 is chosen
        Serial.print("Reaction time "); Serial.print(rt); Serial.println(" ms");
        win(w, "(fastest)");
      } else if (millis() - goAt > 3000) {
        digitalWrite(GO_LED, LOW);
        Serial.println("No one pressed.");
        state = DONE; heldSince = 0;
      }
      break;
  }
}

What the serial monitor should show

Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual Arduino Nano with its sensors at their default values. Your numbers will follow your real readings, but the format should match.

Hold both buttons for a second to start.

Test checklist

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

CheckExpected
Hold both buttonsA beep and Get ready on the monitor
Wait with buttons releasedNothing for two to six seconds
Red lamp lightsFirst press wins, winner's green LED lights
Press before the red lampThe other player wins
Serial monitorReaction time in milliseconds

If it does not work

Never starts

Hold both buttons for a full second.

Red lamp never lights

Check D5 and the LED direction.

False starts every round

Release both buttons fully during the countdown.

No buzzer

Use a passive buzzer on D8.

Same delay every time

Check the random seed line is present.

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

Ask Lark AI

Common questions

What parts do I need for the quick-draw duel?

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

How long does the quick-draw duel take to build?

About 90 minutes for a beginner maker, in 8 steps.

How do I know the quick-draw duel works?

Hold both buttons: A beep and Get ready on the monitor. Wait with buttons released: Nothing for two to six seconds. Red lamp lights: First press wins, winner's green LED lights.

Never starts: what should I check?

Hold both buttons for a full second.

Red lamp never lights: what should I check?

Check D5 and the LED direction.

False starts every round: what should I check?

Release both buttons fully during the countdown.

No buzzer: what should I check?

Use a passive buzzer on D8.