From cc9e38a65e099e9b869a2bdb8b130ddd6ded4861 Mon Sep 17 00:00:00 2001 From: L3D Date: Sun, 24 Mar 2024 23:18:03 +0100 Subject: [PATCH] update rainbow --- rgb_rainbow.py | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/rgb_rainbow.py b/rgb_rainbow.py index 48a036b..f7dc7eb 100755 --- a/rgb_rainbow.py +++ b/rgb_rainbow.py @@ -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()