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 starton to the workspace. - Ok, in Variables click on
Make a Variable. Name the variable asseconds. Drag out aset toblock and change the name with the dropdown toseconds. Place the variable into theon startblock.

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 1second.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 pressedan put it somewhere on the workspace. - Get an
if thenblock from Logic and put it in theon button pressed. - From the same Logic category, get a 0 < 0 and relpace the
falsecondition with it. - Change the left
0in the condtion to thesecondsvariable. Change0on the right to50. This limits time to just one minute. - In the
thensection, put achange bythere. Select thesecondsvariable name from the dropdown and change the0on the right to10. - Add a
show numberbelow thechange by. Change the value to thesecondsvariable. Then, put aclear screenunder that.

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 pressedan put it somewhere on the workspace. - Get an
if thenblock from Logic and put it in theon button pressed. - From the same Logic category, get a 0 < 0 and relpace the
falsecondition with it. - Change the left
0in the condtion to thesecondsvariable. Change0on the right to60. Again, this limits the time to just one minute. - In the
thensection, put achange bythere. Select thesecondsvariable name from the dropdown. - Add a
show numberbelow thechange by. Change the value to thesecondsvariable. Then, put aclear screenunder that.

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 shakeblock and place it in the workspace. - Pull out a
whilefrom Loops and put it in theon shake. Replace thetruecondition with the0 < 0conditon from Logic. Make the<go to>. Change the0on the left to thesecondsvariable. - Take out another
show numberand put it inside thewhile. Change the0to thesecondsvariable. Put apauseunder that and set the time to1000milliseconds. 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 byand place it below thepause. Find the0 - 0block in Math and put it in thechange by. Set the0on 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.
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.
没有评论:
发表评论