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

If, then, else

Week 19 · Unit 8: Be an innovator · 3 lessons
3

Program a micro:bit

Computing 5: Unit 8, pp. 104–118
GoalTrace a micro:bit program that uses a variable, a random number and if … then … else to show different outputs.
New words
micro:bit · a small programmable board with 25 LED lights and two buttonsLED · a small light that can be switched on or offsimulator · a program that shows on a screen what a micro:bit would doicon · a small picture that the lights show, such as a heart
Explanation

A micro:bit is a small board that you program with blocks. It has 25 LED lights, two buttons called A and B, and sensors. We can test a program on a simulator on a screen before we use the real board. Blocks such as ‘on button A pressed’ and ‘on shake’ start a program. The ‘set variable to’ block stores a value, and ‘pick random’ gives a whole number between two limits. The ‘if … then … else’ block is in the Logic group. ‘Show icon’, ‘show number’ and ‘show string’ are output blocks in the Basic group. An adult should help with the real board and its batteries.

ABbutton A pressed
Worked examples
setgametopick random1to3show stringTagshow stringHopifgame=3thenelseon buttonA+Bpressed
A playground game picker. When buttons A and B are pressed together, the board picks 1, 2 or 3. If the number equals 3 it shows the word Tag, otherwise it shows Hop. So Hop comes up for two of the three numbers.
setrolltopick random1to6show iconhappyshow numberrollifroll=6thenelseon buttonBpressed
A lucky dice. When button B is pressed, the board rolls a number from 1 to 6. A 6 shows a happy face. Every other number is shown as a number.
Rollroll = 6?Output
4falseshows 4
6trueshows a happy face
2falseshows 2
We cannot know the random numbers in advance, but we can trace the program for some made-up rolls.
Class activity

Be the micro:bit (pairs). Draw a board with a 5 × 5 grid for the lights and two buttons A and B on paper. Player A is the board. Player B holds a die and says ‘press B’. Then A rolls the die (the random block), writes the number in a cup labelled roll, checks ‘roll = 6?’ and either draws a smile on the grid or writes the number. Play 12 presses. How many smiles did you get? Predict first how many you expect in 12 presses. If you have a computer or tablet, build the lucky dice in a simulator.

Practice
1
In 4 button presses, the random numbers are 3, 2, 1 and 2. What does the board show each time?
setsidetopick random1to3show iconheartshow icondiamondifside=2thenelseon buttonApressed
2
A program uses ‘pick random 1 to 3’ and shows a star only when the variable equals 3. How many possible results are there? On how many of them is the star shown?
3
The program must show this smile when tries = 1, and a sad face when tries is anything else. It starts when button B is pressed and picks tries from 1 to 3. Write the blocks in order.
ABthe smile
4
A micro:bit program does nothing when button A is pressed. Name two things to check.