code.py
 1  # SPDX-FileCopyrightText: 2020 FoamyGuy for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  """
 6  This example script shows how to blink multiple LEDs at different
 7  rates simultaneously without each affecting the others.
 8  """
 9  
10  import time
11  import board
12  import digitalio
13  
14  BLINK_LIST = [
15      {
16          "ON": 0.25,
17          "OFF": 0.25,
18          "PREV_TIME": -1,
19          "PIN": board.D5,
20      },
21      {
22          "ON": 0.5,
23          "OFF": 0.5,
24          "PREV_TIME": -1,
25          "PIN": board.D6,
26      },
27      {
28          "ON": 1.0,
29          "OFF": 1.0,
30          "PREV_TIME": -1,
31          "PIN": board.D9,
32      },
33      {
34          "ON": 2.0,
35          "OFF": 2.0,
36          "PREV_TIME": -1,
37          "PIN": board.D10,
38      }
39  ]
40  
41  for led in BLINK_LIST:
42      led["PIN"] = digitalio.DigitalInOut(led["PIN"])
43      led["PIN"].direction = digitalio.Direction.OUTPUT
44  
45  while True:
46      # Store the current time to refer to later.
47      now = time.monotonic()
48  
49      for led in BLINK_LIST:
50          if led["PIN"].value is False:
51              if now >= led["PREV_TIME"] + led["OFF"]:
52                  led["PREV_TIME"] = now
53                  led["PIN"].value = True
54          if led["PIN"].value is True:
55              if now >= led["PREV_TIME"] + led["ON"]:
56                  led["PREV_TIME"] = now
57                  led["PIN"].value = False