Defuse the doomsday device on an Arduino Uno
A TM1637 counts down a minute, a clue lamp blinks a number, and three buttons are the wires: cut the right one before time runs out. A game that teaches state machines. The whole build, an Arduino Uno and 11 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 35 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $35. On Mokxi they're free.
What you'll learn
- Games
- Lights
- Sound
- Displays
- Choosing and reading a resistor
- Difficulty: Intermediate
- Time: About 35 minutes
- Board: Arduino Uno R3
- Parts: 13
How it works
A TM1637 counts down a minute, a clue lamp blinks a number, and three buttons are the wires: cut the right one before time runs out. A game that teaches state machines.
Arduino Uno R3 runs the sketch below. The circuit around it: Resistor, 220 Ω, LED, yellow, LED, green, LED, red, Pushbutton, Piezo speaker. pinMode sets WIRE[i] as an input with its pull-up on. digitalWrite drives BOOM_LED HIGH. digitalRead reads whether WIRE[i] is HIGH or LOW. analogRead measures the voltage on A2 as a number. Serial.begin opens the serial port at 115200 baud.
Parts list
13 parts, plus the jumper wires. They retail for about $35; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Full-size breadboard
- 3 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × LED, yellowIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
- 1 × LED, greenIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
- 1 × LED, redIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
- 3 × PushbuttonIn Starter Kit, Christmas Lights Kit, Logic Lab Kit.
- 1 × Piezo speakerIn Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Four-digit display, TM1637In Pinball Table Kit, Spaceship Panel Kit.
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 pin | Connects to |
|---|---|
| LED, yellow pin C | Arduino Uno R3 pin GND |
| LED, green pin C | Arduino Uno R3 pin GND |
| LED, red pin C | Arduino Uno R3 pin GND |
| Pushbutton (1) pin 2a | Arduino Uno R3 pin GND |
| Pushbutton (2) pin 2a | Arduino Uno R3 pin GND |
| Pushbutton (3) pin 2a | Arduino Uno R3 pin GND |
| Piezo speaker pin 2 | Arduino Uno R3 pin GND |
| Four-digit display, TM1637 pin GND | Arduino Uno R3 pin GND |
| Piezo speaker pin 1 | Arduino Uno R3 pin 10 |
| Pushbutton (3) pin 1a | Arduino Uno R3 pin 8 |
| Pushbutton (2) pin 1a | Arduino Uno R3 pin 7 |
| Pushbutton (1) pin 1a | Arduino Uno R3 pin 6 |
| Resistor, 220 Ω (3) pin 1 | Arduino Uno R3 pin 4 |
| Resistor, 220 Ω (2) pin 1 | Arduino Uno R3 pin 3 |
| Resistor, 220 Ω (1) pin 1 | Arduino Uno R3 pin 2 |
| Four-digit display, TM1637 pin VCC | Arduino Uno R3 pin 5V |
| Four-digit display, TM1637 pin CLK | Arduino Uno R3 pin A0 |
| Four-digit display, TM1637 pin DIO | Arduino Uno R3 pin A1 |
| LED, yellow pin A | Resistor, 220 Ω (1) pin 2 |
| LED, green pin A | Resistor, 220 Ω (2) pin 2 |
| LED, red pin A | Resistor, 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.
// Defuse the doomsday device: a TM1637 timer, three "wires", a clue LED and a
// piezo, on an Uno.
//
// A0, A1 the TM1637's CLK and DIO (analog pins work as digital ones)
// pin 2 clue LED (yellow)
// pin 3 SAFE LED (green)
// pin 4 BOOM LED (red)
// pins 6, 7, 8 wire 1, wire 2, wire 3, each a button to GND
// pin 10 piezo
//
// The clock starts at 60 seconds. The clue LED blinks one, two or three times,
// pauses, and blinks again. Cut the wire with that number: press its button.
// The right wire stops the clock and the green LED lights. A wrong wire costs
// ten seconds and a strike, and the third strike, or zero on the clock, sets
// off the red LED. The piezo ticks once a second, and twice as often in the
// last ten. Power-cycle (Stop, then Run) to get a new bomb.
//
// # A game is a state machine
//
// There are only three states: ARMED, SAFE and BOOM. loop() checks which one it
// is in and does that state's work, and the only way out of ARMED is through
// the checks in cutWire() and tick(). Writing the states down first makes it
// hard to get a bomb that is both defused and exploded.
//
// # No delay() anywhere
//
// A delay() would freeze the buttons while the clue LED blinked. Instead each
// job keeps the time it last ran, and loop() asks "is it my turn yet?" with
// millis(). The clock, the clue pattern and the tick all share one loop.
#include <TM1637Display.h>
const uint8_t WIRE[3] = {6, 7, 8};
const uint8_t CLUE = 2;
const uint8_t SAFE_LED = 3;
const uint8_t BOOM_LED = 4;
const uint8_t PIEZO = 10;
const int START_SECONDS = 60;
const int PENALTY = 10;
const int MAX_STRIKES = 3;
enum State { ARMED, SAFE, BOOM };
TM1637Display display(A0, A1);
State state = ARMED;
int secondsLeft = START_SECONDS;
int strikes = 0;
int answer; // 1 to 3: the wire that defuses it
bool cut[3]; // wires already cut
bool wasDown[3]; // for catching the press, not the hold
unsigned long lastSecond = 0;
unsigned long clueStart = 0;
bool halfTicked = false; // the faster tick in the last ten seconds
void show() {
// minutes and seconds with the colon lit
display.showNumberDecEx((secondsLeft / 60) * 100 + secondsLeft % 60, 0x40, true);
}
void explode() {
state = BOOM;
digitalWrite(BOOM_LED, HIGH);
digitalWrite(CLUE, LOW);
tone(PIEZO, 120, 1500);
Serial.println("BOOM");
}
void defuse() {
state = SAFE;
digitalWrite(SAFE_LED, HIGH);
digitalWrite(CLUE, LOW);
tone(PIEZO, 1800, 400);
Serial.print("defused with ");
Serial.print(secondsLeft);
Serial.println(" seconds left");
}
void cutWire(int n) { // n is 1 to 3
if (cut[n - 1]) return;
cut[n - 1] = true;
Serial.print("cut wire ");
Serial.println(n);
if (n == answer) {
defuse();
return;
}
strikes++;
secondsLeft = max(0, secondsLeft - PENALTY);
Serial.print("strike ");
Serial.println(strikes);
tone(PIEZO, 300, 300);
show();
if (strikes >= MAX_STRIKES || secondsLeft == 0) explode();
}
// The clue: `answer` blinks of 150 ms, then a 1.5 s pause, repeated.
void drawClue(unsigned long now) {
unsigned long t = (now - clueStart) % (answer * 300UL + 1500UL);
bool on = t < answer * 300UL && (t % 300UL) < 150UL;
digitalWrite(CLUE, on ? HIGH : LOW);
}
void setup() {
Serial.begin(115200);
for (int i = 0; i < 3; i++) pinMode(WIRE[i], INPUT_PULLUP);
pinMode(CLUE, OUTPUT);
pinMode(SAFE_LED, OUTPUT);
pinMode(BOOM_LED, OUTPUT);
pinMode(PIEZO, OUTPUT);
display.setBrightness(0x0f);
// Nothing is wired to A2, so it reads noise: a different bomb each Run.
randomSeed(analogRead(A2));
answer = random(1, 4);
show();
Serial.println("doomsday device armed: count the blinks, cut that wire");
clueStart = lastSecond = millis();
}
void loop() {
unsigned long now = millis();
if (state == ARMED) {
// a press is a pin that is low now and was not low last time round
for (int i = 0; i < 3; i++) {
bool down = digitalRead(WIRE[i]) == LOW;
if (down && !wasDown[i]) cutWire(i + 1);
wasDown[i] = down;
}
}
if (state == ARMED) {
drawClue(now);
if (now - lastSecond >= 1000) {
lastSecond += 1000;
halfTicked = false;
secondsLeft--;
show();
tone(PIEZO, 2400, 20);
if (secondsLeft == 0) explode();
} else if (secondsLeft <= 10 && !halfTicked && now - lastSecond >= 500) {
halfTicked = true;
tone(PIEZO, 2400, 20);
}
}
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Runs once when the board starts. It gets the pins and libraries ready.
void setup() {
Serial.begin(115200);
for (int i = 0; i < 3; i++) pinMode(WIRE[i], INPUT_PULLUP);
pinMode(CLUE, OUTPUT);
pinMode(SAFE_LED, OUTPUT);
pinMode(BOOM_LED, OUTPUT);
pinMode(PIEZO, OUTPUT);
display.setBrightness(0x0f);
// Nothing is wired to A2, so it reads noise: a different bomb each Run.
randomSeed(analogRead(A2));
answer = random(1, 4);
show();
Serial.println("doomsday device armed: count the blinks, cut that wire");
clueStart = lastSecond = millis();
}loop sketch.ino
Runs over and over for as long as the board has power. This is where the project does its work.
void loop() {
unsigned long now = millis();
if (state == ARMED) {
// a press is a pin that is low now and was not low last time round
for (int i = 0; i < 3; i++) {
bool down = digitalRead(WIRE[i]) == LOW;
if (down && !wasDown[i]) cutWire(i + 1);
wasDown[i] = down;
}
}
if (state == ARMED) {
drawClue(now);
if (now - lastSecond >= 1000) {
lastSecond += 1000;
halfTicked = false;
secondsLeft--;
show();
tone(PIEZO, 2400, 20);
if (secondsLeft == 0) explode();
} else if (secondsLeft <= 10 && !halfTicked && now - lastSecond >= 500) {
halfTicked = true;
tone(PIEZO, 2400, 20);
}
}
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- 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. - The button seems always pressed, or never changes. Pushbutton
A 4-leg tactile switch has legs joined in pairs. Place it across the breadboard center gap so the pairs sit on different rows. Wired along one side, the legs are already connected. - A passive piezo stays silent when you just set the pin HIGH. Piezo speaker
A passive piezo needs a changing signal. Use tone(pin, frequency) or a PWM wave. Around 2 to 4 kHz is loudest. A buzzer with a built in oscillator makes sound from plain DC instead. - Sound is very quiet or the pin seems overloaded. Piezo speaker
Drive the piezo from a pin directly, or through a 100 ohm resistor. For a bigger buzzer, switch it with a transistor. Check that you wired the right two legs and joined grounds.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.