Last active
November 1, 2022 13:25
-
-
Save pingswept/1d37a74943f73a6266688db44f3e382d to your computer and use it in GitHub Desktop.
State machine example for ME 30 at Tufts
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
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