code.py
 1  # SPDX-FileCopyrightText: 2020 FoamyGuy for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  """
 6  Using time.monotonic() to blink the built-in LED.
 7  
 8  Instead of "wait until" think "Is it time yet?"
 9  """
10  import time
11  import digitalio
12  import board
13  
14  # How long we want the LED to stay on
15  BLINK_ON_DURATION = 0.5
16  
17  # How long we want the LED to stay off
18  BLINK_OFF_DURATION = 0.25
19  
20  # When we last changed the LED state
21  LAST_BLINK_TIME = -1
22  
23  # Setup the LED pin.
24  led = digitalio.DigitalInOut(board.D13)
25  led.direction = digitalio.Direction.OUTPUT
26  
27  while True:
28      # Store the current time to refer to later.
29      now = time.monotonic()
30      if not led.value:
31          # Is it time to turn on?
32          if now >= LAST_BLINK_TIME + BLINK_OFF_DURATION:
33              led.value = True
34              LAST_BLINK_TIME = now
35      if led.value:
36          # Is it time to turn off?
37          if now >= LAST_BLINK_TIME + BLINK_ON_DURATION:
38              led.value = False
39              LAST_BLINK_TIME = now