code.py
 1  # SPDX-FileCopyrightText: 2021 Liz Clark for Adafruit Industries
 2  # SPDX-License-Identifier: MIT
 3  
 4  import time
 5  import board
 6  import neopixel
 7  from digitalio import DigitalInOut, Direction, Pull
 8  from adafruit_led_animation.animation.rainbow import Rainbow
 9  from adafruit_led_animation.sequence import AnimationSequence
10  from adafruit_led_animation import helper
11  from adafruit_led_animation.color import AMBER
12  
13  #  button setup
14  button = DigitalInOut(board.D1)
15  button.direction = Direction.INPUT
16  button.pull = Pull.UP
17  
18  #  neopixel setup
19  pixel_pin = board.D0
20  pixel_num = 9
21  
22  pixels = neopixel.NeoPixel(pixel_pin, pixel_num, brightness=0.3, auto_write=False)
23  
24  #  variable for number of pixels in the pixelmap helper
25  num = 1
26  
27  #  pixel map helper
28  #  allows you to light each candle up one by one
29  #  begins with one being lit (num)
30  candles = helper.PixelMap.horizontal_lines(
31      pixels, num, 1, helper.horizontal_strip_gridmap(pixel_num, alternating=False)
32  )
33  
34  #  rainbow animation
35  rainbow = Rainbow(candles, speed=0.1, period=5)
36  
37  animations = AnimationSequence(rainbow)
38  
39  #  turn on center candle
40  pixels[4] = AMBER
41  pixels.show()
42  
43  while True:
44  
45      #  if only one candle is lit, don't rewrite center neopixel
46      if num == 1:
47          pass
48      #  otherwise write data to center neopixel
49      else:
50          pixels[4] = AMBER
51          pixels.show()
52      #  animation the rainbow animation
53      animations.animate()
54  
55      #  if you press the button...
56      if not button.value:
57          #  if all of the candles are not lit up yet...
58          if num < 9:
59              #  increase value of num by one
60              num += 1
61          #  skip the center candle so that it stays amber
62          if num == 4:
63              num = 5
64          #  recreate the pixel helper to increase the size of the horizontal grid
65          #  this is how the next neopixel is lit up in the sequence
66          candles = helper.PixelMap.horizontal_lines(
67              pixels, num, 1, helper.horizontal_strip_gridmap(pixel_num, alternating=False)
68          )
69          rainbow = Rainbow(candles, speed=0.1, period=5)
70          animations = AnimationSequence(rainbow)
71          #  quick delay so that everything flows well
72          time.sleep(0.5)