Skip to content

Instantly share code, notes, and snippets.

@pingswept
Last active November 1, 2022 13:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pingswept/1d37a74943f73a6266688db44f3e382d to your computer and use it in GitHub Desktop.
Save pingswept/1d37a74943f73a6266688db44f3e382d to your computer and use it in GitHub Desktop.
State machine example for ME 30 at Tufts
import board
import digitalio as dio
import time
led = dio.DigitalInOut(board.LED)
led.direction = dio.Direction.OUTPUT
button = dio.DigitalInOut(board.D6)
button.direction = dio.Direction.INPUT
other_led = dio.DigitalInOut(board.D5)
other_led.direction = dio.Direction.OUTPUT
STATE_TOGGLE = 1
STATE_CHECK_BUTTON = 2
state = STATE_TOGGLE
next_toggle = 0
led.value = False
while True:
if state is STATE_TOGGLE:
if led.value is True:
led.value = False
else:
led.value = True
next_toggle = time.monotonic() + 1.0
state = STATE_CHECK_BUTTON
elif state is STATE_CHECK_BUTTON:
if button.value is True:
other_led.value = True
else:
other_led.value = False
if time.monotonic() > next_toggle:
state = STATE_TOGGLE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment