To the left and right side of the LED display, the micro:bit module has two tactile pushbuttons labeled A and B. The micro:bit module can be programmed to respond to these buttons being pressed. Each button exists as an object referred to as button_a and button_b.
For example, if you want to know how many times button A gets pressed since the micro:bit module has turned on (or since the last time you asked how many times button A was pressed, whichever is most recent), you can use button_a.get_presses. Similarly the same can be done for button B.
Note sleep(5000) at the beginning of the next example program. When you flash the program, use these 5 seconds to press button A as many times as possible.
After 5 seconds, the program then stores the number of times button A got pressed since startup into the variable called numberOfPresses. The last line of the program uses the display to scroll the numberOfPresses.
- Enter, save, and flash get_presses.py to your micro:bit
- Immediately Press button A as many times as possible in 5 seconds.
- Watch the display to see how many times you pressed the button.
#get_presses.py from microbit import * sleep(5000) numberOfPresses = button_a.get_presses() display.scroll(numberOfPresses)
- Using the previous example as a model, write a program that displays the number of times button B gets pressed after 10 seconds pass from startup.
- Write a program that displays the number of times both button A and button B get pressed after 10 seconds from startup.
You can write a statement for each buttontell if it is pressed. The button_a.is_pressed method will return either True or False depending on whether or not button A is currently pressed. These methods often work well with conditionals. For example, the following program displays a check mark if button A is pressed. Otherwise, it will display an X.
- Enter, save, and flash is_pressed.py to your micro:bit.
- Press button A to see the display change.
#is_pressed.py from microbit import * while True: if button_a.is_pressed(): display.show(Image.YES) else: display.show(Image.NO)
Not pressed: Pressed:
- Use the display to show the letter A when the A button is pressed.
- Use the display to show the letter B when the B button is pressed.
Another commonly used method of the buttons is to be able to tell if the button was pressed since the micro:bit module turned on, or since the last time the code checked to see if it was pressed, whichever is most recent. The button_a.was_pressed method returns True or False depending on whether or not button A was pressed.
For example, was_pressed.py will display a check mark if button A was pressed in each 5 second interval. If was not pressed in a five second interval, then it will display an X.
- Enter, save, and flash was_pressed.py to your micro:bit.
#was_pressed.py from microbit import * while True: sleep(5000) if button_a.was_pressed(): display.show(Image.YES) else: display.show(Image.NO)
- Write a script to check if button A was pressed every 10 seconds instead of every 5.
- Write a script to check if button B was pressed every 10 seconds instead of button A.