About this project
This project utilises the BBC micro:bit's input buttons and introduces some more complex commands, giving pupils the opportunity to create a simple countdown timer on their device.
What you'll need
1 x BBC micro:bit
1 x Micro USB
1 x Computer or tablet
2 x AAA Batteries & Battery Holder
Project walk through
- Go to the BBC micro:bit website, click 'create code' and begin a new Microsoft Block Editor project.
- Click 'Variables' in the left-hand command menu and select the 'set [item] to...' tile. Drag this tile into the main coding space and click [item] to bring up a drop-down menu. Select 'rename variable' and change the title from 'item' to 'counter' in the command box which should appear. Click OK.
- Now click 'Maths' in the command menu and find the small number block, which should have the figure 0 inside the text box as a default. Attach this to the 'set [counter] to...' tile, and change the number to 3.
- Click 'Basics' on the command menu and drag the 'Show number' tile into the main coding space. Then under 'Variables' find a tile that simply says [Counter] and attach this to the 'Show number' tile like so...
- Next, wrap the 'Show number' tile in a 'forever' loop (found under 'Basics') to tell your device to continuously display the number in the counter.
- Click 'run' to test your program on the emulator. You should see the number three appear on the animated BBC micro:bit's LED array.
- Now it's time to program the buttons. Under 'Inputs', find a tile that says 'On button [A] pressed do...'. Drag this into the main coding space, and attach a 'change [item] by [1] tile from the 'Variables' menu. Change [item] to [counter] to tell the program to increase the counter by one every time the 'A' button is pressed.
- Drag in another 'on Button [A] pressed do...' tile, and change [A] to [B].
- To tell the program to count down when the 'B' button is pressed, you need to create another variable. Under the 'Variables' dropdown, drag in another 'set [item] to...' tile and rename this tile to 'CountingDown' or similar. Connect it to your first 'set [counter] to' tile like so...
- Under 'Logic' drag in a [False] tile and attach it to your 'set [CountingDown] to...' tile.
- Right-click the 'set [CountingDown] to...' tile and select 'Duplicate' to clone this tile. Attach the clone to the 'on button [B] pressed do...' tile and change the value from 'false' to 'true'.
- You have now told the program that whenever the 'B' button is pressed, the 'CountingDown' command should be set to true. The next step is to tell the program what we want it to do in this instance... in our case, this should prompt the program to start counting down backwards.
- To do this, find the 'If...do' tile under the 'Logic' menu, and attach it to your forever loop.
- Also under 'Logic', drag in an 'If [0] = [0]' tile and attach to the 'If...do' tile.
- When you open the 'Variables' menu, you should see that you can now select a 'CountingDown' tile. Drag this into the first [0] on the 'If [0] = [0]' tile.
- Replace the second [0] on the 'If [0] = [0]' tile with another 'true' tile from the 'Logic' menu.
- Attach a 'Pause (ms) tile to your forever loop and set it to 1,000 milliseconds - i.e. one second.
- Attach another 'change [item] by [1]' tile, and change [item] to [counter] and [1] to [-1].
- Finally, you just need to tell the program to stop counting down when it reaches zero, otherwise it will continue into negative figures.
- To do this, add another 'If...do' tile, and under 'if' attach another 'If [0] = [0]' tile, replace the first [0] with a 'counter' tile, and change '=' to '≤' - the symbol for 'less than or equal to'. This tells the program that an action is required when the countdown reaches zero.
- Under 'do' attach a 'set [item] to' variable and change [item] to [CountingDown]. Now attach a [false] tile.
- This tells the program that when the counter is equal or less than zero, the CountingDown command should be reset to false, essentially freezing the countdown.
- Your full code should now look something like this...
- Click 'run' to test the program with the BBC micro:bit emulator. Your countdown should start at 3. You can press the 'A' button to increase the figure, and the 'B' button to start the countdown. When the countdown reaches zero, it should stop.
- Once you're happy that your code has worked, click 'compile' to download the script and drag it onto your BBC micro:bit. Congratulations! You've created a workable countdown timer!
Once your pupils have mastered the basics of creating a countdown timer, encourage them to experiment with the new commands they've learned to discover what else they can make it do. Let us know how they responded to the challenge in the comments section below...
Top Comments