/ NeoPixel_Tiara / code.py
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  try:
11      import urandom as random
12  except ImportError:
13      import random
14  
15  numpix = 7  # Number of NeoPixels
16  pixpin = board.D1  # Pin where NeoPixels are connected
17  strip = neopixel.NeoPixel(pixpin, numpix, brightness=1, auto_write=True)
18  colors = [
19      [232, 100, 255],  # Purple
20      [200, 200, 20],  # Yellow
21      [30, 200, 200],  # Blue
22  ]
23  
24  
25  def flash_random(wait, howmany):
26      for _ in range(howmany):
27  
28          c = random.randint(0, len(colors) - 1)  # Choose random color index
29          j = random.randint(0, numpix - 1)  # Choose random pixel
30          strip[j] = colors[c]  # Set pixel to color
31  
32          for i in range(1, 5):
33              strip.brightness = i / 5.0  # Ramp up brightness
34              time.sleep(wait)
35  
36          for i in range(5, 0, -1):
37              strip.brightness = i / 5.0  # Ramp down brightness
38              strip[j] = [0, 0, 0]  # Set pixel to 'off'
39              time.sleep(wait)
40  
41  
42  while True:
43      # first number is 'wait' delay, shorter num == shorter twinkle
44      flash_random(.01, 1)
45      # second number is how many neopixels to simultaneously light up
46      flash_random(.01, 3)
47      flash_random(.01, 2)