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

A sound board

Week 20 · Unit 8: Be a musician · 3 lessons
2

Read the code

Computing 3: Unit 8, pp. 119–134
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.

Code Mon buttonA+Bpressedshow number3play soundsoaringuntil donepause (ms)1000show stringGo!
Worked examples
StepAlgorithm M
1Start the program when buttons A and B are pressed
2Show number 3
3Play sound soaring until done
4Pause 1 second
5Show string “Go!”
Here is Code M and its algorithm. Each line of the algorithm comes from the block on the same line.
Code Non logopressedshow stringHistart melodypower uprepeatingonceshow icondiamond
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.
StepCode M saysAlgorithm saysSame?
2show number 3show number 4no
3soaringsoaringyes
41 second1 secondyes
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 ____.
Code Pon buttonBpressedset volume40show stringHopplay soundspringuntil doneshow icontortoise
2
Write the algorithm for this program.
on buttonApressedshow iconhouseplay soundyawnuntil doneshow stringByeclear screen
3
In Code N the melody is changed from repeating once to repeating forever. Predict what changes.
4
Write the blocks for this algorithm: 1 Start when the board is shaken. 2 Show icon umbrella. 3 Play sound mysterious until done.