GoalBuild a micro:bit program from an algorithm, step by step, and test it.
New words
simulator · a program on the screen that acts like a real micro:bitrun · to make a program do what its blocks saytest · to try a program to see if it works
Explanation
To build a micro:bit program in a block editor, work in steps. Start a new project and give it a name. A new project already has two blocks, “on start” and “forever”, but our program only needs the button block, so we drag those two away. Take the “on button pressed” block from the Input group and choose the button. Then add the other blocks one by one, in the same order as the algorithm, and set each dropdown and number. Every block must be inside the button block. Last, click Play and click the button on the simulator to test. Save your project.
Worked examples
Step
Instruction
1
Start when button B is pressed
2
Show icon heart
3
Set volume to 150
4
Play sound giggle until done
5
Show string “Hooray!”
6
Clear screen
This algorithm becomes the program at the top of the page. Each step is one block, and they are all stacked under the button block.
Blocks must be attached to the button block. A block that is lying loose on its own never runs, because nothing starts it. In the second program the “show icon” block is not attached, so pressing A shows nothing.
I click
What should happen
button A
nothing, because the program waits for B
button B
heart, giggle, “Hooray!” scrolls, screen clears
Test the program in the simulator. Click the buttons and check that what happens matches the algorithm.
Class activity
Block cards (in pairs): write each block of the program on a card: “on button B pressed”, “show icon heart”, “set volume 150”, “play sound giggle until done”, “show string Hooray!”, “clear screen”. Lay the cards in a stack. One of you is the simulator and stands ready, the other “clicks” the button by tapping a shoulder. The simulator follows the cards from top to bottom. Then take away the first card. Does anything happen now? If you have a computer or tablet, build the program in a micro:bit simulator.
Practice
1
Put these steps in the right order: (a) click Play and test (b) add the “on button pressed” block (c) start a new project (d) add the icon and sound blocks (e) save the project.
c, b, d, a, e.
2
Look at the program. The sound never plays. Why?
The “play sound” block is not attached to the button block, so nothing starts it. It has to be moved under the other blocks.
3
Write the blocks for this algorithm: 1 Start when button A is pressed. 2 Show icon target. 3 Set volume to 80. 4 Play sound mysterious until done. 5 Clear screen.
Step
Instruction
1
Start when button A is pressed
2
Show icon target
3
Set volume to 80
4
Play sound mysterious until done
5
Clear screen
on button [A v] pressed, show icon [target v], set volume (80), play sound [mysterious v] until done, clear screen.
4
Which button do you click on the simulator to run the first program in this lesson?
Button B, because the first block says “on button B pressed”.