update rainbow

This commit is contained in:
L3D 2024-03-24 23:18:03 +01:00
parent 954a52cc4d
commit cc9e38a65e
No known key found for this signature in database
GPG key ID: AD65B920933B4B20

View file

@ -1,7 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
""" """
Example RGB to Neopixel LED Example Rainbow Animation for Neopixel LED
""" """
import time import time
from rpi_ws281x import Adafruit_NeoPixel, Color from rpi_ws281x import Adafruit_NeoPixel, Color
@ -9,27 +9,31 @@ from rpi_ws281x import Adafruit_NeoPixel, Color
# NeoPixel configuration # NeoPixel configuration
PIN_NEO_PIXEL = 18 PIN_NEO_PIXEL = 18
NUM_PIXELS = 24 NUM_PIXELS = 24
DELAY_INTERVAL = 50 DELAY_INTERVAL = 0.01
# Create NeoPixel object # Create NeoPixel object
NeoPixel = Adafruit_NeoPixel(NUM_PIXELS, PIN_NEO_PIXEL) NeoPixel = Adafruit_NeoPixel(NUM_PIXELS, PIN_NEO_PIXEL)
NeoPixel.begin() # Initialize NeoPixel strip object 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(int(pos * 3), int(255 - pos * 3), 0)
if pos < 170:
pos -= 85
return Color(int(255 - pos * 3), 0, int(pos * 3))
return Color(0, int(pos * 3), int(255 - pos * 3))
try: try:
while True: while True:
# Turn on all pixels to green # Rainbow cycle animation
for j in range(255):
for pixel in range(NUM_PIXELS): for pixel in range(NUM_PIXELS):
NeoPixel.setPixelColor(pixel, Color(0, 255, 0)) color = wheel((pixel * 256 // NUM_PIXELS) + j)
NeoPixel.setPixelColor(pixel, color)
NeoPixel.show() NeoPixel.show()
time.sleep(DELAY_INTERVAL / 1000.0) time.sleep(DELAY_INTERVAL)
time.sleep(2)
# Turn on all pixels to red
for pixel in range(NUM_PIXELS):
NeoPixel.setPixelColor(pixel, Color(255, 0, 0))
NeoPixel.show()
time.sleep(DELAY_INTERVAL / 1000.0)
time.sleep(2)
except KeyboardInterrupt: except KeyboardInterrupt:
# Clean up code before exiting the script # Clean up code before exiting the script