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

Typing for the micro:bit

Week 7 · Unit 7.2: Decomposing problems · 3 lessons
2

Two buttons, two jobs

Computing 7: Unit 7.2, pp. 44–81
GoalTurn a flowchart with a decision into an if statement, and use two buttons in one program.
New words
if statement · a line that runs the indented lines below it only when its question is trueindentation · spaces at the start of a line that show which lines belong togetherwhile True · a loop that repeats the indented lines again and again, foreverbutton_a.is_pressed() · a question that is true while button A is held down
Explanation

A decision diamond in a flowchart becomes an if statement in MicroPython. The question is button_a.is_pressed(), and the line must end with a colon. The lines that happen when the answer is yes are indented by four spaces. Python uses the indentation to know which lines belong to the if. A program that checks the button only once would miss a press that comes later. So we put everything inside while True:, which repeats its indented lines forever, like the forever block in a block language. Button B has its own question, button_b.is_pressed(). Each decision in a flowchart gets its own if.

Python
1from microbit import *
2while True:
3    if button_a.is_pressed():
4        display.show(Image.YES)
5        display.scroll("Ready")
Output
when A is pressed: shows a tick, then scrolls Ready
Worked examples
FlowchartMicroPython line
Startfrom microbit import *
keep checkingwhile True:
Is A pressed? if button_a.is_pressed():
show a tick display.show(Image.YES)
scroll the word Ready display.scroll("Ready")
This table matches the steps of a flowchart (“Is A pressed? Show a tick, scroll Ready”) with the lines of the program above.
Python
1from microbit import *
2while True:
3    if button_a.is_pressed():
4        display.show(Image.ARROW_E)
5    if button_b.is_pressed():
6        display.show(Image.ARROW_W)
Output
A: arrow pointing right. B: arrow pointing left.
Two buttons mean two if statements, one after the other. This program points an arrow to the right when A is pressed and to the left when B is pressed. The picture stays until the other button is pressed.
Python
1from microbit import *
2while True:
3    if button_a.is_pressed():
4        display.scroll("A")
5    display.show(Image.NO)
Output
a cross is always shown; pressing A also scrolls the letter A
Indentation changes the meaning. In this program the last line is indented to the same level as the if, so it is inside the while loop but not inside the if. The cross is shown again and again, whether or not A is pressed.
Class activity

Be the micro:bit (no computer). A friend holds a paper micro:bit with a grid and buttons A and B. You read this program line by line, like a computer would, and you only do what the lines say. Your friend taps A or B whenever they like. Draw on the grid what appears each time. Then swap roles and let your friend write the program with one if for each button. If you have a computer or tablet, an adult can help you try a two-button program.

Practice
1
Write the MicroPython program for this flowchart.
StartIs A pressed?yesnoscroll the word Homeshow a houseEnd
2
Look at the program. First A is pressed and released. Then B is pressed and released. What does the board show after each press?
Python
1from microbit import *
2while True:
3    if button_a.is_pressed():
4        display.scroll("1")
5    if button_b.is_pressed():
6        display.scroll("2")
3
This program has two mistakes. Find them and write the correct lines.
Python
1from microbit import *
2while True
3    if button_a.is_pressed():
4    display.show(Image.HEART)
4
Why do micro:bit programs usually put their if statements inside while True?