AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Break it into parts

Week 18 · Unit 8: Be an innovator · 3 lessons
2

Write the algorithm

Computing 4: Unit 8, pp. 104–122
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”.

ComponentBecomes the step
Input that starts itStart program when…
PictureShow … picture
Time onWait … seconds
Time offClear screen, then wait … seconds
Flashes in allRepeat steps … more times
Worked examples
StepInstruction
1Start program when button A is pressed
2Show left arrow
3Wait 0.5 seconds
4Clear screen
5Wait 0.5 seconds
6Repeat steps 2 to 5 five more times
7Stop 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.
show ledspause (ms)2000clear screenpause (ms)1000repeat3timeson buttonBpressed
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 allThe repeat step saysThe repeat block says
2one more timerepeat (2) times
3two more timesrepeat (3) times
6five more timesrepeat (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).
StepInstruction
1Start program when the logo is pressed
2Show sun picture
3?
4Clear screen
5Wait 2 seconds
6?
7Stop program
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.
3
How many times is the moon shown? How long does the whole program take?
StepInstruction
1Start program when the board is turned screen up
2Show moon picture
3Wait 2 seconds
4Clear screen
5Wait 1 second
6Repeat steps 2 to 5 three more times
7Stop program
4
This algorithm should flash the arrow 3 times, but the arrow appears only once. Find the mistake and fix it.
StepInstruction
1Start program when button A is pressed
2Show left arrow
3Wait 1 second
4Clear screen
5Wait 1 second
6Repeat steps 3 to 5 two more times
7Stop program