code.py
 1  # SPDX-FileCopyrightText: 2017 Mikey Sklar for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  import time
 6  from rainbowio import colorwheel
 7  import board
 8  import neopixel
 9  from digitalio import DigitalInOut, Direction
10  
11  try:
12      import urandom as random
13  except ImportError:
14      import random
15  
16  pixpin = board.D1
17  numpix = 16
18  
19  led = DigitalInOut(board.D13)
20  led.direction = Direction.OUTPUT
21  
22  strip = neopixel.NeoPixel(pixpin, numpix, brightness=.2, auto_write=True)
23  
24  colors = [
25      [232, 100, 255],  # Purple
26      [200, 200, 20],  # Yellow
27      [30, 200, 200],  # Blue
28  ]
29  
30  
31  # Fill the dots one after the other with a color
32  
33  
34  def colorWipe(color, wait):
35      for j in range(len(strip)):
36          strip[j] = (color)
37          time.sleep(wait)
38  
39  
40  def rainbow(wait):
41      for j in range(255):
42          for i in range(len(strip)):
43              idx = int(i + j)
44              strip[i] = colorwheel(idx & 255)
45          time.sleep(wait)
46  
47  
48  # Slightly different, this makes the rainbow equally distributed throughout
49  
50  
51  def rainbow_cycle(wait):
52      for j in range(255 * 5):
53          for i in range(len(strip)):
54              idx = int((i * 256 / len(strip)) + j)
55              strip[i] = colorwheel(idx & 255)
56          time.sleep(wait)
57  
58  
59  def flash_random(wait, howmany):
60      for _ in range(howmany):
61  
62          c = random.randint(0, len(colors) - 1)  # Choose random color index
63          j = random.randint(0, numpix - 1)  # Choose random pixel
64          strip[j] = colors[c]  # Set pixel to color
65  
66          for i in range(1, 5):
67              strip.brightness = i / 5.0  # Ramp up brightness
68              time.sleep(wait)
69  
70          for i in range(5, 0, -1):
71              strip.brightness = i / 5.0  # Ramp down brightness
72              strip[j] = [0, 0, 0]  # Set pixel to 'off'
73              time.sleep(wait)
74  
75  
76  while True:
77      # first number is 'wait' delay, shorter num == shorter twinkle
78      flash_random(.01, 8)
79      # second number is how many neopixels to simultaneously light up
80      flash_random(.01, 5)
81      flash_random(.01, 11)
82  
83      colorWipe((232, 100, 255), .1)
84      colorWipe((200, 200, 20), .1)
85      colorWipe((30, 200, 200), .1)
86  
87      rainbow_cycle(0.05)