update rainbow
This commit is contained in:
parent
954a52cc4d
commit
cc9e38a65e
1 changed files with 19 additions and 15 deletions
|
@ -1,7 +1,7 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Example RGB to Neopixel LED
|
||||
Example Rainbow Animation for Neopixel LED
|
||||
"""
|
||||
import time
|
||||
from rpi_ws281x import Adafruit_NeoPixel, Color
|
||||
|
@ -9,30 +9,34 @@ from rpi_ws281x import Adafruit_NeoPixel, Color
|
|||
# NeoPixel configuration
|
||||
PIN_NEO_PIXEL = 18
|
||||
NUM_PIXELS = 24
|
||||
DELAY_INTERVAL = 50
|
||||
DELAY_INTERVAL = 0.01
|
||||
|
||||
# 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(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:
|
||||
while True:
|
||||
# Turn on all pixels to green
|
||||
for pixel in range(NUM_PIXELS):
|
||||
NeoPixel.setPixelColor(pixel, Color(0, 255, 0))
|
||||
# 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 / 1000.0)
|
||||
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)
|
||||
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()
|
||||
NeoPixel.show()
|
||||
|
|
Loading…
Reference in a new issue