Countdown Timer
Countdown Timer
Let’s make a countdown timer and see the seconds tick by on your micro:bit watch.
Duration: ~10 minutes
Make the time variable
We need a variable to keep track of how many seconds are left on the watch.
- Go into Basic in the toolbox and pull an
on start
on to the workspace. - Ok, in Variables click on
Make a Variable
. Name the variable asseconds
. Drag out aset to
block and change the name with the dropdown toseconds
. Place the variable into theon start
block.
Set the time with buttons
There has to be a way to set the time on your watch. We’ll use the buttons to set the amount of time by adding both 10 seconds and single seconds. We’ll use one button for adding 10
seconds and another button for adding just 1
second.
Set seconds by ten
We’ll use button A
to add 10
seconds to our time count. The time count of seconds
will increase by 10
each time the button is pressed.
- In Input, find an
on button pressed
an put it somewhere on the workspace. - Get an
if then
block from Logic and put it in theon button pressed
. - From the same Logic category, get a 0 < 0 and relpace the
false
condition with it. - Change the left
0
in the condtion to theseconds
variable. Change0
on the right to50
. This limits time to just one minute. - In the
then
section, put achange by
there. Select theseconds
variable name from the dropdown and change the0
on the right to10
. - Add a
show number
below thechange by
. Change the value to theseconds
variable. Then, put aclear screen
under that.
Link: https://makecode.microbit.org/_d9WXoCP9E7ya
Set seconds by one
Now, we’ll use the B
button to add just 1
second the time count. The time count in seconds
will only increase by 1
when this button is pressed.
- In Input, find an
on button pressed
an put it somewhere on the workspace. - Get an
if then
block from Logic and put it in theon button pressed
. - From the same Logic category, get a 0 < 0 and relpace the
false
condition with it. - Change the left
0
in the condtion to theseconds
variable. Change0
on the right to60
. Again, this limits the time to just one minute. - In the
then
section, put achange by
there. Select theseconds
variable name from the dropdown. - Add a
show number
below thechange by
. Change the value to theseconds
variable. Then, put aclear screen
under that.
Link: https://makecode.microbit.org/_eE2V3q11h8t4
Shake off the time
Ok, now we’ll get the timer going and show how many seconds are left. This will happen when the watch is shaken!
- Get an
on shake
block and place it in the workspace. - Pull out a
while
from Loops and put it in theon shake
. Replace thetrue
condition with the0 < 0
conditon from Logic. Make the<
go to>
. Change the0
on the left to theseconds
variable. - Take out another
show number
and put it inside thewhile
. Change the0
to theseconds
variable. Put apause
under that and set the time to1000
milliseconds. This means our timer will count down by 1000 milliseconds, which is actually one second, each time through the loop. - To change the number of seconds left, get a
change by
and place it below thepause
. Find the0 - 0
block in Math and put it in thechange by
. Set the0
on the right of the minus to be a1
.
Be alarming!
Add a few show icon
blocks at the bottom of the while
to make an alarm to show that the time is up! We have some diamonds and the ‘X’ symbol for ours right now.
Link: https://makecode.microbit.org/_72ch5T95a56j
All Done!
Nice! You’ve got your timer coded now. Go press the Download
button and put your code on the micro:bit. When you shake it, it counts down from the time you have set.
At first, there are zero seconds set. To add ten seconds to the count, press the A button. To increase the count by one second, press the B button.