code.py
 1  # SPDX-FileCopyrightText: 2020 FoamyGuy for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  """
 6  Blinking multiple LEDs at different rates.
 7  
 8  Circuit Playground Neopixels.
 9  """
10  import time
11  from adafruit_circuitplayground import cp
12  
13  BLINK_MAP = {
14      "RED": {
15          "ON": 0.25,
16          "OFF": 0.25,
17          "PREV_TIME": -1,
18          "INDEX": 1,
19          "COLOR": (255, 0, 0)
20      },
21      "GREEN": {
22          "ON": 0.5,
23          "OFF": 0.5,
24          "PREV_TIME": -1,
25          "INDEX": 3,
26          "COLOR": (0, 255, 0)
27      },
28      "BLUE": {
29          "ON": 1.0,
30          "OFF": 1.0,
31          "PREV_TIME": -1,
32          "INDEX": 6,
33          "COLOR": (0, 0, 255)
34      },
35      "YELLOW": {
36          "ON": 2.0,
37          "OFF": 2.0,
38          "PREV_TIME": -1,
39          "INDEX": 8,
40          "COLOR": (255, 255, 0)
41      }
42  }
43  
44  cp.pixels.brightness = 0.02
45  
46  while True:
47      # Store the current time to refer to later.
48      now = time.monotonic()
49  
50      for color in BLINK_MAP.keys(): # pylint: disable=consider-iterating-dictionary
51  
52          # Is LED currently OFF?
53          if cp.pixels[BLINK_MAP[color]["INDEX"]] == (0, 0, 0):
54              # Is it time to turn ON?
55              if now >= BLINK_MAP[color]["PREV_TIME"] + BLINK_MAP[color]["OFF"]:
56                  cp.pixels[BLINK_MAP[color]["INDEX"]] = BLINK_MAP[color]["COLOR"]
57                  BLINK_MAP[color]["PREV_TIME"] = now
58          else:  # LED is ON:
59              # Is it time to turn OFF?
60              if now >= BLINK_MAP[color]["PREV_TIME"] + BLINK_MAP[color]["ON"]:
61                  cp.pixels[BLINK_MAP[color]["INDEX"]] = (0, 0, 0)
62                  BLINK_MAP[color]["PREV_TIME"] = now