code.py
 1  # SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  import time
 6  import board
 7  import neopixel
 8  
 9  # On CircuitPlayground Express, and boards with built in status NeoPixel -> board.NEOPIXEL
10  # Otherwise choose an open pin connected to the Data In of the NeoPixel strip, i.e. board.D1
11  pixel_pin = board.NEOPIXEL
12  
13  # On a Raspberry pi, use this instead, not all pins are supported
14  # pixel_pin = board.D18
15  
16  # The number of NeoPixels
17  num_pixels = 10
18  
19  # Increase or decrease between 0 and 1 to increase or decrease the brightness of the LEDs
20  brightness = 0.6
21  
22  # The order of the pixel colors - RGB or GRB. Some NeoPixels have red and green reversed!
23  # For RGBW NeoPixels, simply change the ORDER to RGBW or GRBW.
24  ORDER = neopixel.GRB
25  
26  pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=brightness, auto_write=False,
27                             pixel_order=ORDER)
28  
29  
30  def colorwheel(pos):
31      # Input a value 0 to 255 to get a color value.
32      # The colours are a transition r - g - b - back to r.
33      # Works with RGB or RGBW LEDs.
34      if pos < 0 or pos > 255:
35          r = g = b = 0
36      elif pos < 85:
37          r = int(pos * 3)
38          g = int(255 - pos * 3)
39          b = 0
40      elif pos < 170:
41          pos -= 85
42          r = int(255 - pos * 3)
43          g = 0
44          b = int(pos * 3)
45      else:
46          pos -= 170
47          r = 0
48          g = int(pos * 3)
49          b = int(255 - pos * 3)
50      return (r, g, b) if ORDER in (neopixel.RGB, neopixel.GRB) else (r, g, b, 0)
51  
52  
53  def rainbow_swirl(wait):
54      for j in range(255):
55          for i in range(num_pixels):
56              pixel_index = (i * 256 // num_pixels) + j
57              pixels[i] = colorwheel(pixel_index & 255)
58          pixels.show()
59          time.sleep(wait)
60  
61  
62  def rainbow_fill(wait):
63      for j in range(255):
64          for i in range(num_pixels):
65              pixel_index = int(i + j)
66              pixels[i] = colorwheel(pixel_index & 255)
67          pixels.show()
68          time.sleep(wait)
69  
70  
71  def christmas_flash(duration):
72      pixels.fill((255, 0, 0))
73      pixels.show()
74      time.sleep(duration)
75      pixels.fill((255, 255, 255))
76      pixels.show()
77      time.sleep(duration)
78  
79  
80  while True:
81      for _ in range(5):
82          christmas_flash(0.5)
83  
84      for _ in range(5):
85          christmas_flash(0.1)
86  
87      pixels.fill((255, 0, 0))
88      pixels.show()
89      time.sleep(1)
90  
91      rainbow_fill(0.001)  # Increase the number to slow down the rainbow
92  
93      pixels.fill((0, 0, 0))
94      pixels.show()
95      time.sleep(1)
96      rainbow_swirl(0.001)  # Increase the number to slow down the rainbow