Skip to content

Instantly share code, notes, and snippets.

@thewrongjames
Created January 5, 2021 05:34
Show Gist options
  • Save thewrongjames/da961118a2198db223a02920e2241cdb to your computer and use it in GitHub Desktop.
Save thewrongjames/da961118a2198db223a02920e2241cdb to your computer and use it in GitHub Desktop.
Microbit Micropython was pressed
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