GoalRead a micro:bit program and write its algorithm, including the blanks.
New words
code · the blocks or words that make up a programstring · a piece of text, such as a word or a messageblank · a gap in an algorithm that the code can tell you how to fill
Explanation
We can read code to find out what a program does. Read from the top. The hat block tells you what starts the program. Each block below is one step. To write the algorithm, say each block in plain words, in the same order. For example, “show number (3)” becomes “Show number 3”. If an algorithm has blanks, look at the matching block in the code and copy what is in the dropdowns and the number slots. Last, check the numbers, words and choices again, because one small slip changes the result.
Worked examples
Step
Algorithm M
1
Start the program when buttons A and B are pressed
2
Show number 3
3
Play sound soaring until done
4
Pause 1 second
5
Show string “Go!”
Here is Code M and its algorithm. Each line of the algorithm comes from the block on the same line.
The same idea works for Code N. It starts when the logo is pressed. It scrolls the word Hi, starts the power up melody once and then shows a diamond.
Step
Code M says
Algorithm says
Same?
2
show number 3
show number 4
no
3
soaring
soaring
yes
4
1 second
1 second
yes
Checking the details matters. The code says 3, but the algorithm says 4 on step 2. They are not the same, so one of them has a mistake.
Class activity
Code detectives (in pairs): each of you writes a small micro:bit program on a strip of paper, for example “on shake, show icon ghost, play sound yawn until done”. Swap strips. Write the algorithm for your friend’s program in plain words, one line per block. Then check each line together. Did you copy the dropdown words correctly? Fix any lines that are different. Then both change one number in the code and predict the new result.
Practice
1
Fill in the blanks for Code P: 1 Start the program when button ____ is pressed. 2 Set volume ____. 3 Show string ____. 4 Play sound ____ until done. 5 Show icon ____.
1 B. 2 40. 3 “Hop”. 4 spring. 5 tortoise.
2
Write the algorithm for this program.
1 Start when button A is pressed. 2 Show icon house. 3 Play sound yawn until done. 4 Show string “Bye”. 5 Clear screen.
3
In Code N the melody is changed from repeating once to repeating forever. Predict what changes.
The word Hi still scrolls and the diamond still appears, but the power up tune plays again and again and does not stop by itself.
4
Write the blocks for this algorithm: 1 Start when the board is shaken. 2 Show icon umbrella. 3 Play sound mysterious until done.
on [shake v], show icon [umbrella v], play sound [mysterious v] until done.