code.py
 1  # SPDX-FileCopyrightText: 2017 Mikey Sklar for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  import time
 6  
 7  import board
 8  import neopixel
 9  
10  numpix = 22  # Number of NeoPixels
11  pixpin = board.D1  # NeoPixels pin. For Gemma M0 = D1, Trinket M0 = D4
12  strip = neopixel.NeoPixel(pixpin, numpix, brightness=1, auto_write=False)
13  pos = 0  # position
14  direction = 1  # direction of "eye"
15  
16  while True:
17      strip[pos - 2] = ([16, 0, 0])  # Dark red
18      strip[pos - 1] = ([128, 0, 0])  # Medium red
19      strip[pos] = ([255, 48, 0])  # brightest
20      strip[pos + 1] = ([128, 0, 0])  # Medium red
21  
22      if (pos + 2) < numpix:
23          # Dark red, do not exceed number of pixels
24          strip[pos + 2] = ([16, 0, 0])
25  
26      strip.write()
27      time.sleep(0.03)
28  
29      # Rather than being sneaky and erasing just the tail pixel,
30      # it's easier to erase it all and draw a new one next time.
31      for j in range(-2, 2):
32          strip[pos + j] = (0, 0, 0)
33          if (pos + 2) < numpix:
34              strip[pos + 2] = (0, 0, 0)
35  
36      # Bounce off ends of strip
37      pos += direction
38      if pos < 0:
39          pos = 1
40          direction = -direction
41      elif pos >= (numpix - 1):
42          pos = numpix - 2
43          direction = -direction