GamesA Two-Player Reaction Game
An LED lights at a random moment. Whoever presses their button first wins. Built from Student Kit parts in 45 minutes.
Lights and displaysCars have green until a pedestrian presses the button. Then the cars get red, the pedestrians get green and a buzzer ticks, just like at a real crossing.
Written by the RoboHub team. A question? Write to us.
Choose a variation and the parts list updates.
Connect the red, yellow and green LEDs through 330 Ω resistors to pins 4, 3 and 2, the same way as in “A Traffic Light with Three LEDs”.
Connect the pedestrian red LED to pin 6 and the green one to pin 5, again through resistors.
One leg of the button goes to pin 7, the other to ground. On a four-leg button use two diagonally opposite legs, so you can't get it wrong. No resistor needed: the code switches on the board's internal one (INPUT_PULLUP). Connect the buzzer's “+” to pin 8 and “−” to ground.
The car green always lasts at least eight seconds: press the button earlier and the light waits until those eight seconds are up.
const int CAR_RED = 4, CAR_YELLOW = 3, CAR_GREEN = 2;
const int WALK_RED = 6, WALK_GREEN = 5;
const int BUTTON = 7, BUZZER = 8;
void setup() {
for (int pin = 2; pin <= 6; pin++) pinMode(pin, OUTPUT);
pinMode(BUTTON, INPUT_PULLUP); // pressed = LOW, no resistor needed
pinMode(BUZZER, OUTPUT);
}
void cars(bool red, bool yellow, bool green) {
digitalWrite(CAR_RED, red ? HIGH : LOW);
digitalWrite(CAR_YELLOW, yellow ? HIGH : LOW);
digitalWrite(CAR_GREEN, green ? HIGH : LOW);
}
void walkers(bool green) {
digitalWrite(WALK_GREEN, green ? HIGH : LOW);
digitalWrite(WALK_RED, green ? LOW : HIGH);
}
void loop() {
cars(false, false, true); // cars drive...
walkers(false);
unsigned long greenSince = millis();
while (digitalRead(BUTTON) == HIGH) delay(10); // ...until someone presses the button
tone(BUZZER, 1000, 100); // "request received"
while (millis() - greenSince < 8000) delay(10); // cars keep at least 8 s of green
cars(false, true, false); delay(2000); // yellow
cars(true, false, false); delay(1000); // red
walkers(true); // pedestrians cross
for (int i = 0; i < 20; i++) { // 10 seconds of ticking
tone(BUZZER, 1500, 40);
delay(500);
}
for (int i = 0; i < 5; i++) { // green flashes: hurry up
digitalWrite(WALK_GREEN, LOW); delay(300);
digitalWrite(WALK_GREEN, HIGH); delay(300);
}
walkers(false);
delay(1000);
cars(true, true, false); delay(1500); // red + yellow, then green again
}Press the button: the buzzer beeps once, the cars get yellow and then red, and the pedestrians get green. After ten seconds the green starts flashing, and then it all starts again.
GamesAn LED lights at a random moment. Whoever presses their button first wins. Built from Student Kit parts in 45 minutes.
Lights and displaysAn RGB LED holds three small LEDs: red, green and blue. Mix their brightness and you get any colour, while the lamp slowly changes from one colour to the next.
SensorsA photoresistor measures the light. When the room gets dark the LED turns on by itself and goes off again in daylight, just like street lighting.