GoalTurn a three-part algorithm into micro:bit blocks and test each part in turn.
New words
simulator · a program on a computer that shows how the board would behavepause · a block that waits for some milliseconds before the next blockclear screen · a block that switches all the lights offmillisecond · one thousandth of a second, so 1000 ms is 1 second
Explanation
Each part of the algorithm becomes one input block, with the output blocks inside it. Build one part, test it, and then build the next. In a simulator you can press the buttons on the screen to test the program. A pause block waits for a number of milliseconds, and 1000 ms is 1 second. Pauses matter. If two shapes are shown one after another with no pause, the first shape disappears at once and we see only the last one. Use clear screen to turn all the lights off. When you test, press each input and compare what you see with the algorithm. If it is different, find the part with the bug and fix it.
Part
Step
Instruction
1
1
Start program when button A is pressed
1
2
Show plus
1
3
Pause for 1 second
1
4
Show X
2
1
Start program when the board is shaken
2
2
Show small diamond
2
3
Pause for 3 seconds
2
4
Clear screen
3
1
Start program when the logo is touched
3
2
Play the yawn sound until done
Worked examples
The three parts as micro:bit blocks. Part 1 shows the plus, waits 1000 ms and then shows the X. The X stays on, because nothing clears it. Part 2 shows the small diamond for 3000 ms and then clears the screen. Part 3 plays the sound.
Test
Expected result
Actual result
Passed?
press A
plus for 1 second, then X stays on
same
yes
shake the board
small diamond for 3 seconds, then blank
same
yes
touch the logo
yawn sound plays
same
yes
Test one input at a time. This is the test table for the program.
What if the pause is missing? Here, part 1 shows the plus and then the X straight away. The plus is on for such a short time that nobody can see it. The test table shows a mismatch, and the fix is to add pause (1000) between the two show blocks.
Class activity
Block cards (with family): write each block on a small card: on button A pressed, show leds plus, pause 1000, show leds X, on shake, show leds small diamond, pause 3000, clear screen. Put the cards in the order of the algorithm for Part 1 and Part 2. Ask somebody to be the board and to follow the cards, holding up a paper shape for each show block and counting the seconds. If you have a computer or tablet, ask a grown-up to help you build the program in a simulator, one part at a time.
Practice
1
Write the blocks for this algorithm: start when button B is pressed; show the big diamond; pause for 2 seconds; clear the screen.
Step
Instruction
1
Start program when button B is pressed
2
Show big diamond
3
Pause for 2 seconds
4
Clear screen
on button B pressed, containing show leds with the rows 00100, 01010, 10001, 01010, 00100, then pause (ms) 2000, then clear screen.
2
The algorithm says: when buttons A and B are pressed together, show the letter E for 2 seconds and then the small plus. Find the bug in the program.
There is no pause between the letter E and the small plus, so the E disappears at once. Add pause (ms) 2000 between the two show leds blocks.
3
What should you see when you press button B? What is on the display one second after the press?
The heart icon for 0.5 seconds, and then a blank screen. After one second the screen is blank.
4
How many seconds of pauses are in this program in all?
1000 + 500 + 2000 = 3500 ms, which is 3.5 seconds.