By Kevin McAleer, 7 Minutes
Page last updated November 20, 2026

Time to put it all together. You’re going to build a game: an LED lights up after a random wait, and you hit the button as fast as you can. The Pico times you in milliseconds and beeps when you beat your best.
Everything in this project has appeared earlier in the course:
| Idea | Lesson |
|---|---|
| Variables and maths | Variables, Maths and Loops |
| if and comparisons | Making Decisions |
| LED and button blocks | LEDs and Buttons |
| Buzzer blocks | PWM, Servos and Buzzers |
There are two new blocks, both to do with time. You’ll meet them in step 3.
You can build the program and check it runs on the simulator, but to play you need a real button to press. Wire up a Pico like this:
| Part | Connect to |
|---|---|
| LED long leg, through a 330Ω resistor | GP15 |
| LED short leg | GND |
| Button, one side | GP14 |
| Button, other side | GND |
| Buzzer + | GP16 |
| Buzzer - | GND |
Start a new file in the Blocks workspace and open the split view.
best, start and reaction.9999. That’s a time anyone can beat.Get ready...If the wait were always the same, you’d learn the rhythm and cheat.
Your time:, the reaction variable, and ms.reaction < best.New best!, and buzzer on GP16 play 880 Hz for 150 ms.Your finished stack should look like this:

Connect to your Pico, press Run and watch the console. When the LED lights, hit the button.
On the simulator: the program runs, but there’s no button to press. A simulated input pin always reads 0, so every round scores
Your time: 0 ms. That’s a handy way to check your blocks are in the right order before you wire anything up.
Here’s the complete program, with its blocks.
You built this from blocks. Now read it as Python, top to bottom.
Imports and setup
import lines at the top bring in the tools: random numbers, time, pins and Snakie’s helpersbest = 9999 is your first blockThe round
while True: is forevertime.sleep(random.randint(2, 5)) is one block inside another, and in Python it’s one call inside another. The inside one runs firststart = time.ticks_ms() takes the first stopwatch readingWaiting for you
while pin_14.value() == 1:
time.sleep_ms(1)
This is a loop that does almost nothing, on purpose. It keeps checking the button, a thousand times a second, and falls through the moment the pin reads 0.
The score
time.ticks_diff(time.ticks_ms(), start) is the time now minus the time at the start. MicroPython has a special function for this because the tick counter eventually wraps round to zero, and ticks_diff copes with thatif reaction < best: only runs its three lines when you’ve beaten your recordIf you can follow that, you can read Python. You didn’t sit through a syntax lesson to get here. You built things.
To make the game start whenever the Pico is powered, save it to the board as main.py. Files, Local and Device in the Getting Started course shows you how.
Round 1, Round 2 and so on.Too soon! and don’t count the round.Problem: It says Your time: 0 ms straight away.
Solution: On the simulator that’s expected. On a real Pico, check the while loop’s question compares with 1, and that the pin is set with pull-up.
Why: Without the pull-up the pin can read 0 with nothing pressed, so the loop ends at once.
Problem: The LED never goes off between rounds. Solution: Make sure turn LED off is inside forever, near the top. Why: It has to run at the start of every round, not once.
Problem: New best! prints every single round.
Solution: Check set best to reaction is inside the if block.
Why: If best never changes from 9999, every time beats it.
Problem: On a real board the times look too good to be true. Solution: Check the button is wired between GP14 and GND, not 3.3V. Why: A button wired to 3.3V never pulls the pin to 0, so a loose wire does the “pressing”.
Quick quiz 1 of 3
Why is the wait before the LED lights random?
Quick quiz 2 of 3
What does the loop while pin_14.value() == 1: do?
Quick quiz 3 of 3
What does time.ticks_diff(time.ticks_ms(), start) work out?
You can use the arrows ← → on your keyboard to navigate between lessons.
Comments