Skip to content

Instantly share code, notes, and snippets.

@spdskatr
Created March 29, 2019 11:56
Show Gist options
  • Save spdskatr/d7e0f8a632d22618f3cfb12107c14409 to your computer and use it in GitHub Desktop.
Save spdskatr/d7e0f8a632d22618f3cfb12107c14409 to your computer and use it in GitHub Desktop.
from microbit import *
# State: 0 if on standby (waiting for button), 1 if active (waiting for shake)
state = 0
# The time button A was pressed, in milliseconds
last_press = 0
# Target image on display means it's in standby state, stick figure image on display means it's in active state
display.show(Image.TARGET)
# Infinite loop?
while True:
# Registers button A press if and only if state is 0 (standby)
if state == 0 and button_a.is_pressed():
# Set state to 1 (active)
state = 1
# Record start time
last_press = running_time()
# Since state is now 1, update display image to stick figure
display.show(Image.STICKFIGURE)
# Registers shake gesture if and only if state is 1 (active)
elif state == 1 and accelerometer.is_gesture("shake"):
# Set state to 0 (standby)
state = 0
# Record end time and display time difference
end_time = running_time()
difference = end_time - last_press
display.scroll("%.3f sec" % (difference / 1000.0))
# Since state is now 0, update display image to target
display.show(Image.TARGET)
# Since there is an infinite loop, the code never gets here.
# Below code is optional (but preferred)
display.scroll("PIZZA TACOS PIZZA TACOS HA HA LOL")
display.show(Image.COW)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment