import machine
import time
from neopixel import NeoPixel
import math
#---setup neopixels---
NUMS_LEDS = 84
pin = machine.Pin(0, machine.Pin.OUT)
np = NeoPixel(pin, NUMS_LEDS)
#---setup pocket switch---
button = machine.Pin(16, machine.Pin.IN, machine.Pin.PULL_UP)
#---state variables---
current_mode = 0
TOTAL_MODES = 4 #0 off, 1 breathing, 2 solid, 3 chase
light_button_state = 1
brightness = 150
fade_direction = 2
def turn_off():
for i in range(NUMS_LEDS):
np[i] = (0, 0, 0)
np.write()
#---effects/modes---
def comet_chase(color=(255, 7, 58), tail_length=8, delay=0.03):
r, g, b = color
#forward pass
for i in range(NUMS_LEDS + tail_length):
np.fill((0, 0, 0))
for j in range(tail_length):
pos = i - j
if 0 <= pos < NUMS_LEDS:
#fade
fade = (tail_length - j) / tail_length
np[pos] = (int(r * fade), int(g * fade), int(b * fade))
np.write()
time.sleep(delay)
#reverse pass
for i in range(NUMS_LEDS + tail_length -1, -1, -1):
np.fill((0, 0, 0))
for j in range(tail_length):
pos = i - j
if 0 <= pos < NUMS_LEDS:
fade - (tail_length - j) / tail_length
np[pos] = (int(r * fade), int(g * fade), int(b * fade))
np.write()
time.sleep(delay)
def chase_then_hold(delay=0.08, hold_seconds=1.0, tail_length=6):
global current_mode
#single pixel chase over entire strip, when done whole light up for x seconds, repeat
#part 1
for head in range(NUMS_LEDS + tail_length):
if button.value() == 0:
current_mode = (current_mode + 1) % 5
#wait for release
while button.value() == 0:
time.sleep(0.02)
return
#clear strip for current frame
for i in range(NUMS_LEDS):
np[i] = (0, 0, 0)
#moving chase tail
for t in range(tail_length):
pixel_pos = head - t
if 0 <= pixel_pos < NUMS_LEDS:
brightness = max(10, 200 // (t + 1))
np[pixel_pos] = (brightness, 0, 0)
np.write()
time.sleep(delay)
#part 2 after chase loop, light everything
for i in range(NUMS_LEDS):
np[i] = (200, 0, 0)
np.write()
#part3 hold full-lit for x time
start_time = time.time()
while time.time() - start_time < hold_seconds:
if button.value() == 0:
current_mode = (current_mode + 1) % 5
while button.value() == 0:
time.sleep(0.02)
return
time.sleep(0.08)
def breathing_effect():
#loop through full sine wave cycle (0 to 2*pi)
#step by 0.05
step = 0.0
while step < 2 * math.pi:
if button.value() == 0:
return
brightness = int(100 * (math.sin(step) + 1))
for i in range(NUMS_LEDS):
np[i] = (brightness, 0, 0)
np.write()
step += 0.05
time.sleep(0.01)
turn_off()
while True:
current_button_state = button.value()
print("Button value:", current_button_state)
#detect button press(high to low)
if light_button_state == 1 and current_button_state == 0:
current_mode = (current_mode + 1) % TOTAL_MODES
time.sleep(0.2) #debounce delay
light_button_state = current_button_state
if current_mode == 0:
turn_off()
brightness = 0
fade_direction = 2
time.sleep(0.1)
elif current_mode == 1:
#mode 1: breathing
breathing_effect()
elif current_mode == 2:
#mode 2: solid, full brightness
for i in range(NUMS_LEDS):
np[i] = (220, 20, 60) #adjust rgb
np.write()
time.sleep(0.1)
elif current_mode == 3:
#mode 3: chase
comet_chase()