Created
September 9, 2020 15:35
-
-
Save FoamyGuy/83341953c250a5c181259d3fb1d7b3db to your computer and use it in GitHub Desktop.
This file contains hidden or 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
""" | |
Using time.monotonic() to blink the built-in LED. | |
Instead of "wait until" think "Is it time yet?" | |
""" | |
import time | |
import digitalio | |
import board | |
# How long we want the LED to stay on | |
BLINK_ON_DURATION = 0.5 | |
# How long we want the LED to stay off | |
BLINK_OFF_DURATION = 0.25 | |
# When we last changed the LED state | |
LAST_BLINK_TIME = -1 | |
# Setup the LED pin. | |
led = digitalio.DigitalInOut(board.D13) | |
led.direction = digitalio.Direction.OUTPUT | |
while True: | |
# Store the current time to refer to later. | |
now = time.monotonic() | |
if not led.value: | |
# Is it time to turn on? | |
if now >= LAST_BLINK_TIME + BLINK_OFF_DURATION: | |
led.value = True | |
LAST_BLINK_TIME = now | |
if led.value: | |
# Is it time to turn off? | |
if now >= LAST_BLINK_TIME + BLINK_ON_DURATION: | |
led.value = False | |
LAST_BLINK_TIME = now |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment