About the Author
Les is a maker and trainer who has worked with the Raspberry Pi Foundation and the BBC to deliver computing training.
Timers are handy, they can time a boiled egg to perfection, help us take group photos, and be used to help rockets to launch. We shall make our own timer using a micro:bit, some crocodile clips and a speaker/headphones.
You will need to connect the headphones to the micro:bit as per this image.
For this project, we shall be using the Makecode Editor from makecode.microbit.org and we should already be comfortable copying code to our micro:bit.
From the Input, menu drag the “on button A pressed” block into the coding area.
Any code inside the block will be run when button A is pressed.
In the Variables, menu click on “Make a Variable” and call it “time” We shall use this variable to store the duration of our countdown.
From the Variables menu drag “set item to 0” block to the coding area.
Click on the arrow next to “item” and change to “time” then change 0 to 10.
We use a while true loop from loops and from Logic we connect a _ > _ block. Then from Variables, we drag time and place it in the left space and type 3 in the right space.
From the Basic menu, we use Show Number and use our time variable as the number. Then from Music, we use Play Tone and choose middle C to indicate a countdown is running.
Each time the countdown runs, it needs to pause for 1 second (pause (ms) from Basic) and then change the value of our time variable by -1.
Once the countdown reaches 3, we use another loop, and for 3 loops we change the tone to a higher note, to indicate that there is not much time left.
With the countdown reaching 0 we trigger a loop that will repeat 4 times. Each time it goes around, it will play a high G note really quickly. Just like an alarm clock!
Here is all of the code for this project.
We have learnt how to use loops, conditional tests, input, and make music with the micro:bit!
That’s it, now download the code to your micro:bit and when it has finished, press the A button to start the countdown!
Well done you have made your own countdown timer! Can you change the timer to time a boiled egg?