rpi_zero_neopixel_buzzer/rgb_rainbow.py
2024-03-24 23:36:02 +01:00

43 lines
1.2 KiB
Python
Executable file

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Example Rainbow Animation for Neopixel LED
"""
import time
from rpi_ws281x import Adafruit_NeoPixel, Color
# NeoPixel configuration
PIN_NEO_PIXEL = 18
NUM_PIXELS = 24
DELAY_INTERVAL = 0.0042
# Create NeoPixel object
NeoPixel = Adafruit_NeoPixel(NUM_PIXELS, PIN_NEO_PIXEL)
NeoPixel.begin() # Initialize NeoPixel strip object
def wheel(pos):
"""Generate rainbow colors across 0-255 positions."""
pos = pos % 256 # Ensure pos stays within 0-255 range
if pos < 85:
return Color(pos * 3, 255 - pos * 3, 0)
pos -= 85
if pos < 85:
return Color(255 - pos * 3, 0, pos * 3)
pos -= 85
return Color(0, pos * 3, 255 - pos * 3)
try:
while True:
# Rainbow cycle animation
for j in range(255):
for pixel in range(NUM_PIXELS):
color = wheel((pixel * 256 // NUM_PIXELS) + j)
NeoPixel.setPixelColor(pixel, color)
NeoPixel.show()
time.sleep(DELAY_INTERVAL)
except KeyboardInterrupt:
# Clean up code before exiting the script
for pixel in range(NUM_PIXELS):
NeoPixel.setPixelColor(pixel, Color(0, 0, 0))
NeoPixel.show()