Countdown Timer
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
||basic:on start||
on to the workspace. - Ok, in Variables click on
Make a Variable
. Name the variable asseconds
. Drag out a||variables:set to||
block and change the name with the dropdown toseconds
. Place the variable into the||basic:on start||
block.
let seconds = 0
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
||input:on button pressed||
an put it somewhere on the workspace. - Get an
||logic:if then||
block from Logic and put it in the||input:on button pressed||
. - From the same Logic category, get a
||logic:0 < 0||
and replace thefalse
condition with it. - Change the left
0
in the condition to theseconds
variable. Change0
on the right to50
. This limits time to just one minute. - In the
||logic:then||
section, put a||variables:change by||
there. Select theseconds
variable name from the dropdown and change the0
on the right to10
. - Add a
||basic:show number||
below the||variables:change by||
. Change the value to theseconds
variable. Then, put a||basic:clear screen||
under that.
let seconds = 0;
input.onButtonPressed(Button.A, function () {
if (seconds < 50) {
seconds += 10;
basic.showNumber(seconds)
basic.clearScreen()
}
})
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
||input:on button pressed||
an put it somewhere on the workspace. - Get an
||logic:if then||
block from Logic and put it in the||input:on button pressed||
. - From the same Logic category, get a
||logic:0 < 0||
and replace thefalse
condition with it. - Change the left
0
in the condition to theseconds
variable. Change0
on the right to60
. Again, this limits the time to just one minute. - In the
||logic:then||
section, put a||variables:change by||
there. Select theseconds
variable name from the dropdown. - Add a
||basic:show number||
below the||variables:change by||
. Change the value to theseconds
variable. Then, put a||basic:clear screen||
under that.
let seconds = 0;
input.onButtonPressed(Button.B, function () {
if (seconds < 60) {
seconds += 1
basic.showNumber(seconds)
basic.clearScreen()
}
})
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
||input:on shake||
block and place it in the workspace. - Pull out a
||loops:while||
from Loops and put it in the||input:on shake||
. Replace thetrue
condition with the||logic:0 < 0||
condition from Logic. Make the<
go to>
. Change the0
on the left to theseconds
variable. - Take out another
||basic:show number||
and put it inside the||loops:while||
. Change the0
to theseconds
variable. Put a||basic:pause||
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
||variables:change by||
and place it below the||basic:pause||
. Find the||math:0 - 0||
block in Math and put it in the||variables:change by||
. Set the0
on the right of the minus to be a1
.
let seconds = 0;
input.onGesture(Gesture.Shake, function () {
while (seconds > 0) {
basic.showNumber(seconds);
basic.pause(1000);
seconds -= 1;
}
})
Be alarming!
Add a few ||basic:show icon||
blocks at the bottom of the ||loops:while||
to make an alarm to show that the time is up! We have some diamonds and the ‘X’ symbol for ours right now.
let seconds = 0;
input.onGesture(Gesture.Shake, function () {
while (seconds > 0) {
basic.showNumber(seconds)
basic.pause(1000)
seconds -= 1
}
basic.showIcon(IconNames.Diamond)
basic.showIcon(IconNames.SmallDiamond)
basic.showIcon(IconNames.No)
})
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.