Button¶
On this page we will learn
- how to connect the PiicoDev Button
- how to check whether it is pressed or was pressed
- how to detect double presses and count presses
The PiicoDev Button is a large push button that detects presses, double presses and counts how many times it has been pressed.
Possible uses:
- extra game controls
- start and stop buttons
- emergency stop buttons
- counters
Connect it¶
- Connect the button to the PiicoDev adapter with a PiicoDev cable. See Using PiicoDev.
- Upload these files to the micro:bit with
main.py:PiicoDev_Unified.pyPiicoDev_Switch.py
- To use more than one button, give each one a different setting on its ID switches, then create each one with
PiicoDev_Switch(id=[1, 0, 0, 0])and so on.
Set it up¶
Methods¶
| Method | Parameters | Returns | Description |
|---|---|---|---|
button.is_pressed |
none | Boolean | True while the button is held down |
button.was_pressed |
none | Boolean | True if the button was pressed since the last check |
button.was_double_pressed |
none | Boolean | True if the button was double pressed since the last check |
button.press_count |
none | int | Number of presses since the last check |
These are properties, so they have no brackets.
is_pressed¶
Is True while the button is being held down.
PRIMM
- Predict what you think will happen. Be specific.
- Run the program.
- Time to investigate the code. What does each line do?
Code explanation
- line 1 → imports all the commands from the
microbitlibrary. - line 2 → imports the Button driver.
- line 5 → creates the button and calls it
button. - line 8 → starts an endless loop.
- line 9 → checks if the button is being held down.
- line 10 → if it is, shows a happy face.
- line 11 → if it isn't…
- line 12 → …shows a sad face.
- line 13 → waits 100 milliseconds before the loop repeats.
was_pressed¶
Is True if the button has been pressed since the last check, even if it has been let go.
PRIMM
- Predict what you think will happen. Be specific.
- Run the program.
- Time to investigate the code. What does each line do?
Code explanation
- line 1 → imports all the commands from the
microbitlibrary. - line 2 → imports the Button driver.
- line 5 → creates the button and calls it
button. - line 8 → starts an endless loop.
- line 9 → checks if the button was pressed since the last check.
- line 10 → if it was, shows a happy face.
- line 11 → if it wasn't…
- line 12 → …shows a sad face.
- line 13 → waits 1 second. A quick press during this wait is still detected on the next loop.
was_double_pressed¶
Is True if the button has been pressed twice quickly since the last check.
PRIMM
- Predict what you think will happen. Be specific.
- Run the program.
- Time to investigate the code. What does each line do?
Code explanation
- line 1 → imports all the commands from the
microbitlibrary. - line 2 → imports the Button driver.
- line 5 → creates the button and calls it
button. - line 8 → starts an endless loop.
- line 9 → checks if the button was double pressed since the last check.
- line 10 → if it was, shows a happy face.
- line 11 → if it wasn't…
- line 12 → …shows a sad face.
- line 13 → waits 1 second before the loop repeats.
press_count¶
Gives the number of presses since the last check, then resets the count to 0.
PRIMM
- Predict what you think will happen. Be specific.
- Run the program.
- Time to investigate the code. What does each line do?
Code explanation
- line 1 → imports all the commands from the
microbitlibrary. - line 2 → imports the Button driver.
- line 5 → creates the button and calls it
button. - line 8 → starts an endless loop.
- line 9 → prints the number of presses since the last check, then the count resets.
- line 10 → waits 2 seconds, giving you time to press the button.
Documentation¶
Documentation is the official guide written by the people who made the code library, and we can use it to look up every method and its parameters, including ones that aren't covered on this page.