import time
from machine import Pin
from neopixel import Neopixel
# Configure the number of WS2812 LEDs and the GPIO pin
NUM_LEDS = 16
PIN_NUM = 16
# Initialize the NeoPixel strip
np = Neopixel(NUM_LEDS, 0, PIN_NUM, mode="RGB")
# Function to generate color from a position
def wheel(pos):
if pos < 85:
return (pos * 3, 255 - pos * 3, 0) # Transition from green to red
elif pos < 170:
pos -= 85
return (255 - pos * 3, 0, pos * 3) # Transition from red to blue
else:
pos -= 170
return (0, pos * 3, 255 - pos * 3) # Transition from blue to green
# Function to create a rainbow cycle effect on the LEDs
def rainbow_cycle(wait):
for j in range(255): # Loop through each color position
for i in range(NUM_LEDS):
rc_index = (i * 256 // NUM_LEDS) + j # Calculate color position for each LED
np.set_pixel(i, wheel(rc_index & 255)) # Set the color of the LED
np.show() # Update the LED strip
time.sleep_ms(wait) # Wait before updating again
# Main loop to continuously run the rainbow cycle
while True:
rainbow_cycle(10) # Set the delay for the rainbow effect