Script: pushbutton_test_x2
The pushbutton_test_x2 script displays the states of both pushbuttons in the Serial Monitor.
- Enter the pushbutton_test_x2 script (python.microbit.org/v/2 matches the screencaptures).
- Set the Script Name field to pushbutton_test_x2.
- Click Load/Save, and then click Download Project Hex to save your work.
- Close the Load/Save dialog box after downloading.
- In the python.microbit.org editor, click Connect, and then click Flash.
# pushbutton_test_x2
from microbit import *
state9 = 0
state6 = 0
display.off()
pin9.set_pull(pin9.NO_PULL)
pin6.set_pull(pin6.NO_PULL)
while True:
state9 = pin9.read_digital()
state6 = pin6.read_digital()
print("state9:", state9, " state6:", state6)
sleep(250)
Tests
- In the editor, click Open Serial.
- Verify that when you do not press the P6 pushbutton, it displays state6 = 0.
- Verify that when you press and hold the P6 pushbutton, it displays state6 = 1.
- Verify that when you do not press the P9 pushbutton, it displays state9 = 0.
- Verify that when you press and hold the P9 pushbutton, it displays state9 = 1.
- When you are done, click Close Serial.