Micro:bit Buttons and Accelerometer Input
Control the LED matrix with buttons A and B.
The Micro:bit has two main buttons: A and
B. In Python, you can check whether a button is being
pressed with button_a.is_pressed() or
button_b.is_pressed(). These expressions are either
True or False, which makes them perfect
for use in an if statement.
Because button presses can happen at any time, you will usually place your
button checks inside a while True loop so the Micro:bit keeps
watching for input.
from microbit import * while True: if button_a.is_pressed(): display.show(Image.HEART) elif button_b.is_pressed(): display.show(Image.HAPPY) else: display.clear()
In this program, pressing button A shows a heart. Pressing button B shows a happy face. If neither button is pressed, the display turns off.
Control the LED matrix with tilt values.
The Micro:bit accelerometer measures motion and tilt. One useful value is
the x reading, which tells you whether the Micro:bit is
tilted left or right.
You can read that value with accelerometer.get_x(). Positive
values mean tilted to the right, negative values mean tilted to the left,
and values near zero mean mostly level.
This example reads the accelerometer and shows a letter for right tilt,
left tilt, or level. It is the same pattern as your
accelerometer.py file.
from microbit import * while True: reading = accelerometer.get_x() if reading > 20: display.show("R") elif reading < -20: display.show("L") else: display.show("-")
The code compares against 20 and -20 instead of
zero to create a dead zone in the middle so the display does not flicker
constantly when the Micro:bit is almost flat.
You can mix button checks with accelerometer checks to build more interactive behavior.
from microbit import * while True: reading = accelerometer.get_x() if button_a.is_pressed() and reading > 20: display.show(Image.ARROW_E) elif button_a.is_pressed() and reading < -20: display.show(Image.ARROW_W) elif button_b.is_pressed(): display.show(Image.HAPPY) else: display.clear()
Order still matters. The most specific combinations should go first, then the more general cases.
Just as in the movie project, you are not limited to the built-in images.
You can create your own image variables and show them inside the
if statements.
from microbit import * diamond = Image('00900:' '09090:' '90009:' '09090:' '00900') square = Image('99999:' '90009:' '90009:' '90009:' '99999') while True: if button_a.is_pressed(): display.show(diamond) elif button_b.is_pressed(): display.show(square) else: display.clear()
This gives you a simple interactive program: button A shows one picture, and button B shows a different one.
Show different images using both buttons and accelerometer values.
Write a Micro:bit program that shows different images based on combinations of button presses and tilt direction.
from microbit import *.while True loop.reading = accelerometer.get_x().
if and two elif branches.
else case that clears the display or shows a resting
image when no condition matches.
Save your work as lastname_button_tilt.py and upload
it to your OneDrive folder by the end of class.
If you finish early, try adding more combinations or creating more of your own images. You can also check out the example programs here and try to figure out how they work: c2examples.