import _thread
import time
import math
import neopixel
import random
from machine import Pin,ADC
time.sleep(0.1)
# --- Hardware Setup ---
pin0 = Pin(0, Pin.OUT)
adc = ADC(Pin(26))
NUM_LEDS = 30
np1 = neopixel.NeoPixel(pin0, NUM_LEDS)
# Track the rainbow color progression (0 to 360 degrees)
hue = 0.0
# --- Color Conversion Helper ---
def hsv(h, s=1.0, v=0.3):
#Converts Hue (0-360), Saturation (0-1), and Value/Brightness (0-1) to RGB.
#Brightness is kept by default at 0.3 (30%) for comfortable viewing and low power!
c = v * s
x = c * (1 - abs(((h / 60) % 2) - 1))
m = v - c
if 0 <= h < 60:
r, g, b = c, x, 0
elif 60 <= h < 120:
r, g, b = x, c, 0
elif 120 <= h < 180:
r, g, b = 0, c, x
elif 180 <= h < 240:
r, g, b = 0, x, c
elif 240 <= h < 300:
r, g, b = x, 0, c
else:
r, g, b = c, 0, x
return (int((r + m) * 255), int((g + m) * 255), int((b + m) * 255))
# --- Fade Subfunction ---
def fade(strip, ratio):
for i in range(len(strip)):
r, g, b = strip[i]
strip[i] = (int(r * ratio), int(g * ratio), int(b * ratio))
# --- Shooting star subfunction making any leading leds go forward by one
def shoot(lights):
if (lights[len(lights)-1][0]==True):
lights[len(lights)-1][0]=False
for i in range(len(lights) - 1, 0, -1):
if lights[i - 1][0]:
# Move the light to the next pixel
lights[i][0] = True
lights[i][1] = lights[i - 1][1]
# Clear the old pixel position
lights[i - 1][0] = False
# Start with a clean slate
np1.fill((0, 0, 0))
#List representing the LED assignement
lights = [[False, (0, 0, 0)] for _ in range(30)]
hue=0
# --- Main Animation Loop ---
while True:
#Inject at random time
if (random.random()<0.05):
lights[0][0]=True
lights[0][1]=hsv(hue,1,0.6)
hue=hue+5
#lights[0][1]=hsv(random.uniform(0,360),1,0.4)
#Show leading lights at new places
for i in range(len(np1)):
if (lights[i][0]==True):
np1[i]=lights[i][1]
np1.write()
fade(np1,0.8)
#Move leading lights to next place (for next loop)
shoot(lights)
for i in range(NUM_LEDS):
# 1. Get current rainbow color
current_color = hsv(hue,1,0.6)
# 2. Fade trail, then ignite the leading pixel
fade(np1, 0.6)
np1[i] = current_color
np1.write()
# 3. Advance the rainbow color slightly (rotates through 360 degrees)
hue = (hue + 4) % 360
time.sleep(0.01)