Remote-controlled RGB mood light
Warm, cool, rainbow or breathing: cycle lighting moods from your sofa.
Shop by category
Shop all componentsTest reflexes: a random LED lights up and you must hit its matching button before the time runs out. The waiting loop is the interesting part. It cannot use delay(), because the game must notice a button press the moment it happens, so it compares millis() against a deadline and checks all four buttons each time round. Every press is validated: hitting the wrong button costs a point, which is what makes input validation a game mechanic rather than a technicality. The time allowed shrinks each round, so the game gets harder as you get better.
Every part for this build comes in the Geekobot Explorer Kit box, a Geekobot exclusive, together with the parts for all 61 projects, from a first blinking LED to a smart fan and a reaction game. The wiring below is the kit's own wiring.
Tick anything you already have and the total updates.
The brain of every Explorer Kit project
Male-to-female and female-to-female leads for the modules
LEDs from the kit's red, green, yellow and blue packs
220 ohm resistors: one in series with every LED segment
16x2 LCD with I2C backpack: four wires, two lines of text
Nano I/O shield from the kit: every pin has its own S, V, G header, so modules plug in with jumpers
Follow the colours: red is power, dark grey is ground, blue is a signal.
explorer_kit_55_whack_a_mole.inoVerify & upload in Electrolark#include <Wire.h>
#include <LiquidCrystal_I2C.h>
int ledPins[4] = {2, 3, 4, 5};
int buttonPins[4] = {8, 9, 10, 11};
int buzzer = 12;
LiquidCrystal_I2C lcd(0x27, 16, 2);
int score = 0;
int round_ = 0;
int allowed = 1500; // ms to react
void showScore()
{
lcd.setCursor(0, 0);
lcd.print("Score: ");
lcd.print(score);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print("Round: ");
lcd.print(round_);
lcd.print(" ");
}
void setup()
{
for (int i = 0; i < 4; i++)
{
pinMode(ledPins[i], OUTPUT);
pinMode(buttonPins[i], INPUT_PULLUP);
}
pinMode(buzzer, OUTPUT);
randomSeed(analogRead(A0));
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Whack a Mole!");
lcd.setCursor(0, 1);
lcd.print("Get ready...");
delay(2000);
lcd.clear();
}
void loop()
{
round_ = round_ + 1;
delay(random(400, 1200));
int mole = random(0, 4);
digitalWrite(ledPins[mole], HIGH);
unsigned long deadline = millis() + allowed;
bool hit = false;
while (millis() < deadline && !hit)
{
for (int i = 0; i < 4; i++)
{
if (digitalRead(buttonPins[i]) == LOW)
{
hit = true;
if (i == mole)
{
score = score + 1;
tone(buzzer, 900, 120);
Serial.println("Hit");
}
else
{
score = score - 1;
tone(buzzer, 200, 300);
Serial.println("Wrong button");
}
while (digitalRead(buttonPins[i]) == LOW)
{
delay(10);
}
}
}
}
if (!hit)
{
tone(buzzer, 150, 400);
score = score - 1;
Serial.println("Too slow");
}
digitalWrite(ledPins[mole], LOW);
// Speed up, but never below 500 ms
if (allowed > 500)
{
allowed = allowed - 40;
}
showScore();
}
Use the kit's mini-USB cable, install the CH340 driver on Windows if no port appears, pick Tools > Board > Arduino Nano and try Tools > Processor > ATmega328P (Old Bootloader) if the upload times out.
Turn it round: the long leg (anode) goes to the pin side through the 220 ohm resistor, the short leg to GND.
The sketch uses INPUT_PULLUP, so one leg goes to the pin and the other to GND. Use two legs on opposite sides of the button.
This project needs the passive buzzer (the one that plays notes with tone()), not the active one.
The waiting loop is the interesting part. It cannot use delay(), because the game must notice a button press the moment it happens, so it compares millis() against a deadline and checks all four buttons each time round. Every press is validated: hitting the wrong button costs a point, which is what makes input validation a game mechanic rather than a technicality. The time allowed shrinks each round, so the game gets harder as you get better.
A random LED lights and you have 1.5 seconds to hit its button. A correct hit adds a point with a high beep. A wrong button or a miss costs a point with a low buzz.
Yes. It is project 55 of 61 in the Geekobot Explorer Kit, in level 9 (Interactive games). Every part on the list comes in the kit box, and you can also buy the parts one by one from the list above.
No. Every Explorer Kit project is wired on the Nano I/O shield and the breadboard with jumper wires.
10 parts: Generic Nano CH340 Development Board (Soldered), USB Cable, Solderless Breadboard, Dupont Jumper Wires (40 pcs), Through-Hole LED Pack (Pack of 10), Carbon Film Resistor 1/4W 5% (Pack of 10), 4 × Tactile Push Button Switch, Buzzer Module 3.3–5V, LCD1602 16x2 Character LCD Display and Arduino Nano IO Expansion Shield (328P Compatible). On this page you can tick the parts you already have and add only the rest to your cart.
It is a intermediate project and takes about 1 hour. The build on this page has 7 steps.
No. This page has the complete, tested code (explorer_kit_55_whack_a_mole.ino).
About ₹617 for the parts at the prices on this page. Quantity price breaks lower the cost when you build several.
Randomisation and input validation; Non-blocking deadlines with millis(); Penalising wrong input as a game mechanic; Scaling difficulty as the game progresses.
Warm, cool, rainbow or breathing: cycle lighting moods from your sofa.
Press to roll: the pips tumble and slow down before landing on a number.
Wait for the light, hit the button: how many milliseconds can you beat?
A bright 4-digit clock that keeps time through power cuts.
Lessons are free to read. Maker (₹499/month) and Pro add quizzes, worked problems and certificates, paid with your Geekobot account. See plans
Learning project. Check every circuit and the datasheet of each part before you power it up.