Pot-of-gold treasure box on an Arduino Uno
Type the lucky code on a 4x4 keypad and a servo unlocks the treasure box, a ring of LEDs shimmers gold inside and a piezo plays a tune. A wrong code just grumbles. The whole build, an Arduino Uno and 5 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 Runs in your browser. Free, and no account needed.
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Pot-of-gold treasure box: a box that only opens for the lucky code. Type it
// on the keypad and the lid unlocks, the gold inside lights up and a little
// tune plays. Get it wrong and it grumbles.
//
// keypad rows -> pins 9, 8, 7, 6, columns -> pins 5, 4, 3, 2
// latch servo PWM -> pin 10, VCC -> 5 V, GND -> GND
// gold 12 WS2812B LEDs in a ring, DIN <- 330 ohms <- pin 11
// piezo pin 12 -> piezo -> GND
//
// The code is 317, for March 17th, then # to try it. * clears what you have
// typed. The sketch keeps up to eight keys, so a long guess cannot run off
// the end of the buffer.
//
// The Keypad library scans the 16 keys by driving one row at a time and
// reading the four columns, so 16 keys need only 8 pins.
//
// Make it a hunt: hide clues to the code around the house, and fill the box
// with chocolate coins. A servo makes a good latch: its horn turns across the
// lid's edge to lock it and swings clear to open.
//
// Things to try. Type 3 1 7 #. Try a wrong code. Change CODE to your own.
#include <Keypad.h>
#include <Servo.h>
#include <Adafruit_NeoPixel.h>
const char CODE[] = "317";
const int LATCH_PIN = 10, GOLD_PIN = 11, PIEZO_PIN = 12;
const int LOCKED = 0, UNLOCKED = 90;
const int LEDS = 12;
const byte ROWS = 4, COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'},
};
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad pad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
Servo latch;
Adafruit_NeoPixel gold(LEDS, GOLD_PIN, NEO_GRB + NEO_KHZ800);
char typed[9];
int count = 0;
void shimmer(unsigned long ms) {
unsigned long until = millis() + ms;
while (millis() < until) {
for (int i = 0; i < LEDS; i++) {
int v = random(0, 8) == 0 ? 255 : random(120, 200);
gold.setPixelColor(i, v, v * 3 / 5, 0);
}
gold.show();
delay(40);
}
gold.clear();
gold.show();
}
void open() {
Serial.println("Lucky! The pot of gold is open");
latch.write(UNLOCKED);
const int tune[] = {659, 784, 880, 1047, 880, 1047};
for (int i = 0; i < 6; i++) {
tone(PIEZO_PIN, tune[i], 120);
delay(150);
}
shimmer(5000);
latch.write(LOCKED);
Serial.println("Locked again");
}
void wrong() {
Serial.println("Not this time, try again");
tone(PIEZO_PIN, 160, 400);
for (int i = 0; i < 3; i++) {
gold.fill(gold.Color(40, 0, 0));
gold.show();
delay(120);
gold.clear();
gold.show();
delay(120);
}
}
void setup() {
Serial.begin(9600);
latch.attach(LATCH_PIN);
latch.write(LOCKED);
gold.begin();
gold.setBrightness(180);
gold.show();
Serial.println("Treasure box locked: type the lucky code, then #");
}
void loop() {
char key = pad.getKey();
if (!key) return;
tone(PIEZO_PIN, 1200, 30);
if (key == '*') {
count = 0;
Serial.println("Cleared");
} else if (key == '#') {
typed[count] = 0;
if (strcmp(typed, CODE) == 0) open();
else wrong();
count = 0;
} else if (count < 8) {
typed[count++] = key;
Serial.print("Key ");
Serial.println(key);
}
}
Parts list
7 parts, plus the jumper wires. Every one is in the editor's parts bin.
- 1 × Arduino Uno R3
- 1 × Full-size breadboard
- 1 × Keypad, 4x4
- 1 × Resistor, 330 Ω
- 1 × Addressable RGB LEDs
- 1 × Piezo speaker
- 1 × Hobby servo
How it is wired
14 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- Arduino Uno R3 pin 12; Piezo speaker pin 1
- Arduino Uno R3 pin 11; Resistor, 330 Ω pin 1
- Arduino Uno R3 pin 10; Hobby servo pin PWM
- Arduino Uno R3 pin 9; Keypad, 4x4 pin R1
- Arduino Uno R3 pin 8; Keypad, 4x4 pin R2
- Arduino Uno R3 pin 7; Keypad, 4x4 pin R3
- Arduino Uno R3 pin 6; Keypad, 4x4 pin R4
- Arduino Uno R3 pin 5; Keypad, 4x4 pin C1
- Arduino Uno R3 pin 4; Keypad, 4x4 pin C2
- Arduino Uno R3 pin 3; Keypad, 4x4 pin C3
- Arduino Uno R3 pin 2; Keypad, 4x4 pin C4
- Arduino Uno R3 pin 5V; Addressable RGB LEDs pin VDD; Hobby servo pin VCC
- Ground: Arduino Uno R3 pin GND; Addressable RGB LEDs pin VSS; Piezo speaker pin 2; Hobby servo pin GND
- Resistor, 330 Ω pin 2; Addressable RGB LEDs pin DIN
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.