GoalTurn the components of a task into an algorithm with steps in the right order and a repeat step.
New words
algorithm · a set of steps in the right orderclear screen · turn all the lights on the screen offwait · stop for a number of seconds before the next step
Explanation
After decomposition, each column of components becomes an algorithm. Write the steps in a table, one step on each row. The first step uses the input: “Start program when…”. Then write one flash: show the picture, wait for the time on, clear the screen and wait for the time off. These steps are one round. Then add a repeat step that goes back to the first step of the round. Take care with the counting. If the gadget flashes 3 times in all, the round is done once and then repeated two more times. The last step is “Stop program”.
Component
Becomes the step
Input that starts it
Start program when…
Picture
Show … picture
Time on
Wait … seconds
Time off
Clear screen, then wait … seconds
Flashes in all
Repeat steps … more times
Worked examples
Step
Instruction
1
Start program when button A is pressed
2
Show left arrow
3
Wait 0.5 seconds
4
Clear screen
5
Wait 0.5 seconds
6
Repeat steps 2 to 5 five more times
7
Stop program
The turn-left algorithm. One round is steps 2 to 5. The arrow flashes 6 times in all, so steps 2 to 5 are repeated five more times.
The stop algorithm written with micro:bit blocks. The repeat block counts flashes “in all”, so it says 3, not “two more times”. A pause of 1000 ms is 1 second.
Flashes in all
The repeat step says
The repeat block says
2
one more time
repeat (2) times
3
two more times
repeat (3) times
6
five more times
repeat (6) times
Changing “in all” to “more times” is easy: take away one. A repeat block in micro:bit blocks uses the number “in all”.
Class activity
Human micro:bit (with family): one person is the micro:bit. Make two cards: a left arrow and a square. Another person reads the bag-light algorithms from this lesson, one step at a time. The micro:bit holds up the right card, counts the seconds out loud and hides the card when the screen is cleared. Check: how many times did you show each card? Now write a third algorithm for the wake-up gadget from the last lesson and test it in the same way.
Practice
1
Complete steps 3 and 6 of the wake-up gadget algorithm (sun, 1 second on, 2 seconds off, 5 flashes in all).
Step
Instruction
1
Start program when the logo is pressed
2
Show sun picture
3
?
4
Clear screen
5
Wait 2 seconds
6
?
7
Stop program
Step 3 is Wait 1 second. Step 6 is Repeat steps 2 to 5 four more times.
2
Write the algorithm for a shake gadget. When the board is shaken it shows a diamond for 1 second, clears the screen and waits 1 second. It does this 4 times in all.
1 Start program when the board is shaken. 2 Show diamond. 3 Wait 1 second. 4 Clear screen. 5 Wait 1 second. 6 Repeat steps 2 to 5 three more times. 7 Stop program.
3
How many times is the moon shown? How long does the whole program take?
Step
Instruction
1
Start program when the board is turned screen up
2
Show moon picture
3
Wait 2 seconds
4
Clear screen
5
Wait 1 second
6
Repeat steps 2 to 5 three more times
7
Stop program
4 times: once, and then three more times. One round is 2 + 1 = 3 seconds, so 4 rounds take 12 seconds.
4
This algorithm should flash the arrow 3 times, but the arrow appears only once. Find the mistake and fix it.
Step
Instruction
1
Start program when button A is pressed
2
Show left arrow
3
Wait 1 second
4
Clear screen
5
Wait 1 second
6
Repeat steps 3 to 5 two more times
7
Stop program
Step 6 repeats steps 3 to 5, so step 2, which shows the arrow, is not repeated. It must say “Repeat steps 2 to 5 two more times”.