from machine import Pin
import neopixel
import time
# Konfiguration
NUM_LEDS = 8
NUM_STRIPS = 6
STRIP_PINS = [8, 9, 10, 11, 12, 13]
EFFECT_BUTTON_PIN = 14
WHITE_TRIGGER_PIN = 15
# Setup der NeoPixel-Streifen
strips = [neopixel.NeoPixel(Pin(pin), NUM_LEDS) for pin in STRIP_PINS]
# Pins
button_pin = Pin(EFFECT_BUTTON_PIN, Pin.IN, Pin.PULL_UP)
white_input = Pin(WHITE_TRIGGER_PIN, Pin.IN, Pin.PULL_UP)
# Modusverwaltung
mode = 0
num_modes = 6 # 0=aus, 1=blinken, 2=lauflicht, 3=regenbogen, 4=random color, 5=color wave
# Debounce
last_button_state = 1
last_debounce_time = time.ticks_ms()
debounce_delay = 200
# Funktionen
def set_all(r, g, b):
for strip in strips:
for i in range(NUM_LEDS):
strip[i] = (r, g, b)
strip.write()
def effect_white():
set_all(255, 255, 255)
def effect_off():
set_all(0, 0, 0)
def effect_blink():
set_all(255, 255, 255)
time.sleep(0.3)
set_all(0, 0, 0)
time.sleep(0.3)
def effect_chase():
for i in range(NUM_LEDS):
for strip in strips:
for j in range(NUM_LEDS):
strip[j] = (255, 255, 255) if j == i else (0, 0, 0)
strip.write()
time.sleep(0.05)
def wheel(pos):
if pos < 85:
return (pos * 3, 255 - pos * 3, 0)
elif pos < 170:
pos -= 85
return (255 - pos * 3, 0, pos * 3)
else:
pos -= 170
return (0, pos * 3, 255 - pos * 3)
def effect_rainbow():
for j in range(NUM_LEDS):
color = wheel((j * 256 // NUM_LEDS) % 256)
for strip in strips:
strip[j] = color
strip.write()
time.sleep(0.05)
def effect_random_colors():
import urandom
for strip in strips:
for i in range(NUM_LEDS):
r = urandom.getrandbits(8)
g = urandom.getrandbits(8)
b = urandom.getrandbits(8)
strip[i] = (r, g, b)
strip.write()
time.sleep(0.3)
def effect_color_wave():
for shift in range(256):
for j in range(NUM_LEDS):
color = wheel((j * 10 + shift) % 256)
for strip in strips:
strip[j] = color
for strip in strips:
strip.write()
time.sleep(0.03)
# Hauptschleife
while True:
# GND an Pin 9 aktiviert Weißlicht
if white_input.value() == 0:
effect_white()
continue # andere Effekte ignorieren solange Pin 9 aktiv
# Effekte abhängig vom Modus
if mode == 0:
effect_off()
time.sleep(0.05)
elif mode == 1:
effect_blink()
elif mode == 2:
effect_chase()
elif mode == 3:
effect_rainbow()
elif mode == 4:
effect_random_colors()
#elif mode == 5:
#effect_color_wave()
# Taster für Effektwechsel
current_state = button_pin.value()
if current_state == 0 and last_button_state == 1:
if time.ticks_diff(time.ticks_ms(), last_debounce_time) > debounce_delay:
mode = (mode + 1) % (num_modes + 1) # 0 bis 5
last_debounce_time = time.ticks_ms()
last_button_state = current_state