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 https://makecode.microbit.org/ and we should already be comfortable copying code to our micro:bit.
Step 1 - Working with input
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.
Step 2 - Create a variable
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.
Step 3 - Using the variable
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.
Step 4 - Creating a loop and a test
We use a while true loop from loop 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
Step 5 - Showing and hearing the countdown
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.
Step 6 - Countdown and pause
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.
Step 7 - Final 3!
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.
Step 8 - End on a high note!
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!
Step 9 - Putting it together
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?