47 lines
966 B
Python
47 lines
966 B
Python
import machine
|
|
import neopixel
|
|
import time
|
|
|
|
# NeoPixel setup
|
|
NUM_LEDS = 8
|
|
np = neopixel.NeoPixel(machine.Pin(17), NUM_LEDS) # 28
|
|
|
|
# Button on GP22 (active LOW)
|
|
button = machine.Pin(22, machine.Pin.IN, machine.Pin.PULL_UP)
|
|
|
|
# Color list (R, G, B)
|
|
colors = [
|
|
(255, 0, 0), # Red
|
|
(0, 255, 0), # Green
|
|
(0, 0, 255), # Blue
|
|
(255, 255, 0), # Yellow
|
|
(255, 0, 255), # Magenta
|
|
(0, 255, 255), # Cyan
|
|
(255, 255, 255) # White
|
|
]
|
|
|
|
current_color = 0
|
|
last_state = 1 # button not pressed
|
|
|
|
def show_color(color):
|
|
for i in range(NUM_LEDS):
|
|
np[i] = color
|
|
np.write()
|
|
|
|
# Initial color
|
|
show_color(colors[current_color])
|
|
|
|
while True:
|
|
state = button.value()
|
|
|
|
# Detect button press (HIGH → LOW)
|
|
if last_state == 1 and state == 0:
|
|
current_color = (current_color + 1) % len(colors)
|
|
show_color(colors[current_color])
|
|
time.sleep_ms(200) # Debounce delay
|
|
|
|
last_state = state
|
|
time.sleep_ms(10)
|
|
|
|
|