Created
January 5, 2021 05:34
-
-
Save thewrongjames/da961118a2198db223a02920e2241cdb to your computer and use it in GitHub Desktop.
Microbit Micropython was pressed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from microbit import * | |
import music | |
class PinButton: | |
def __init__(self, pin): | |
self.pin = pin | |
self.was_pressed_last_update = False | |
self._was_pressed = False | |
def update(self): | |
pressed = self.pin.read_digital() | |
if not self.was_pressed_last_update and pressed: | |
self._was_pressed = True | |
self.was_pressed_last_update = pressed | |
def was_pressed(self): | |
self.update() | |
result = self._was_pressed | |
self._was_pressed = False | |
return result | |
pin_button = PinButton(pin8) | |
while True: | |
pin_button.update() | |
if running_time() % 1000 == 0 and pin_button.was_pressed(): | |
music.play(music.JUMP_UP) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment