A2 Worksheet - Input
A2 Worksheet - Input
Input
Example .
The program below displays a ‘happy face' whenever the accelerometer detects that
the micro:bit is lying face up.
Notes
● The code is nested in a while loop with a True condition, which means it will be
repeated forever. This is a common pattern.
You can find a reference with all the available accelerometer methods at: microbit-
micropython.readthedocs.io/en/latest/accelerometer.html
The documentation also includes tutorials on detecting movement and gestures:
microbit-micropython.readthedocs.io/en/latest/tutorials/movement.html
microbit-micropython.readthedocs.io/en/latest/tutorials/gestures.html
Source: https://microbit-micropython.readthedocs.io/en/latest/display.html
Step 1
Copy the program below in your development environment.
5 sleep(100)
Flash the program to your micro:bit, to see it run. You will see successive light level
values scrolling through the micro:bit’s display.
Step 2
Remove the lines that display the light values, as you no longer need them. Replace
them with the statements below, that blink a square on the display.
- display.scroll(light)
- sleep(100)
+ delay = .
+ display.show(Image.SQUARE)
+ sleep(delay)
+ display.clear()
+ sleep(delay)
Fill in the incomplete statement so that the delay between successive blinks is 3 times
the light value obtained from the sensor.
Question: When do you expect to see fast blinking? Will it be when there is little light or a
lot of light? Explain your answer.
Step 3
Flash the program to your micro:bit, to see it run. Place your hand over the display and
start moving it closer and further away to investigate what happens. Try covering the
micro:bit or shining a light at it.
x = 0
while True:
if button_b.was_pressed():
x = x + 1
display.show(x)\
Task 2 . Buttons
Step 1
Copy the program below in your development environment.
Question: Can you briefly describe what you expect this program to do?
Step 2
Flash the program to your micro:bit, to see it run. Press button B on the micro:bit a few
times to investigate what happens.
Note: If is_pressed had been used instead of was_presed, then the multiple button presses
would be detected over and over again, making the program difficult to use.
Question: What is the initial value of the x variable? In which line of the program is that
initial value assigned to x?
0,
line 2
Question: How is the value of the x variable modified when button B is pressed? Which
line of the program modifies the value of x?
Step 3
Extend your program so that the value of x is decremented (reduced by 1) when
button A is pressed.
Step 4
Instead of displaying the value of x, modify your program so that it lights up the pixel at
column x, in the middle row of the 5⨉5 LED display.
- display.show(x)
+ display.clear()
+ display.set_pixel(x, , 9)
You will need to fill in the y-coordinate of the middle row. If you are wondering about the
purpose of the clear method, investigate what happens when you don’t include it.
⚠ If the value of the x-coordinate in set_pixel is below 0 or above 4, you will see
an error message scrolling on the display.
Step 5
Extend your program to perform the appropriate checks so that the value of x is never
reduced below 0 or increased above 4.
Task 3 . Tilt
Step 1
Replace the conditions in the if statements that check if the dot on the display should
move left or right. Instead of checking if a button was pressed, your program will check
if the user performed the ‘gesture’ of tilting the micro:bit to the left or to the right.
- if button_a.was_pressed():
+ if accelerator.was_gesture( ):
- if button_b.was_pressed():
+ if accelerator.was_gesture( ):
Fill in the incomplete statements with the appropriate gesture. You will find the list of
recognised gestures in the documentation:
microbit-micropython.readthedocs.io/en/latest/accelerometer.html
Flash the program to your micro:bit, to see it run and check if it works correctly.
Note: You need to tilt the micro:bit in the appropriate direction at around 45° for these gestures
to be detected.
Note: If is_gesture had been used instead of was_gesture, then the same gesture would
be detected over and over again, making the program difficult to use. However, in order to
detect a second tilt in the same direction, you will need to restore the micro:bit to a horizontal
position and then tilt again.
Step 2
Extend the program so that the dot on the display can move across rows as well, by
tilting the micro:bit forwards and backwards. You will need to introduce a y variable to
keep track of the dot’s current row.
Page 6 Last updated: 01-06-21
Year 9 – Physical computing Activity worksheet
Lesson 2 – Bare bones
Step 3 [optional]
If you remove the statement that clears the display between successive dot movements,
you will create a trail of dots. You will be able to draw images on the display by tilting!
- display.clear(x)
Button A Button B
0 0 0
0 1 1
1 0 2
1 1 3
Copy the code below into your development environment and complete it so that the
decimal value displayed is equivalent to the 2-bit binary number represented by the
micro:bit’s buttons.
Replace line 4. Instead of displaying the value of light on the 5⨉5 LED matrix, your
program will print that value.
- display.scroll(light)
+ print((light,))
⚠ Flash the program to your micro:bit. You will see nothing displayed, but the
program is running and obtaining values from the light sensor.
Activate the plotter tool. The values of the light variable that are being printed will be
displayed on a rolling graph. This is an excellent way to visually inspect numerical sensor
inputs.
Resources are updated regularly — the latest version is available at: ncce.io/tcc.
This resource is licensed under the Open Government Licence, version 3. For more
information on this licence, see ncce.io/ogl.