Creater start-stop-rainbow

This commit is contained in:
L3D 2024-03-25 00:59:31 +01:00
parent a8a8819307
commit a0adc777c6
No known key found for this signature in database
GPG key ID: AD65B920933B4B20

122
start_stop_animation.py Normal file
View file

@ -0,0 +1,122 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Example Rainbow Animation for Neopixel LED with GPIO Control
"""
import time
import sys
from RPi import GPIO
from rpi_ws281x import Adafruit_NeoPixel, Color
# NeoPixel configuration
PIN_NEO_PIXEL = 18
NUM_PIXELS = 24
DELAY_INTERVAL = 0.0042
GPIO_PIN = 16
MAX_BRIGHTNESS = 255
ANIMATION_TIME = 23
TIME_EXTENDED = 10
DEBUG_MSG = True
# Setup GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(GPIO_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# 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)
def rainbow_animation():
"""
Running Rainbow animation, defined in wheel
"""
print("Start animation") if DEBUG_MSG else None
start_time = time.time()
brightness = 0 # Initial brightness
try:
while True:
# Calculate elapsed time
elapsed_time = time.time() - start_time
print(f"Time: {int(elapsed_time)}") if DEBUG_MSG else None
if elapsed_time >= ANIMATION_TIME:
while True:
elapsed_time = time.time() - start_time
print(f"Time: {int(elapsed_time)} (extended)") if DEBUG_MSG else None
if elapsed_time >= int(ANIMATION_TIME + TIME_EXTENDED):
break
for j in range(255):
for pxl in range(NUM_PIXELS):
color = Color(255, 255, 255)
NeoPixel.setPixelColor(pxl, color)
NeoPixel.show()
# Check if GPIO16 is grounded to reset animation
if GPIO.input(GPIO_PIN) == GPIO.LOW:
return
break # Exit if time elapsed
# Increase brightness gradually
brightness = int((elapsed_time / ANIMATION_TIME) * MAX_BRIGHTNESS)
NeoPixel.setBrightness(brightness)
# 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)
# Check if GPIO16 is grounded to reset animation
if GPIO.input(GPIO_PIN) == GPIO.LOW and int(elapsed_time) >= 5:
return
except KeyboardInterrupt:
print("exit animation") if DEBUG_MSG else None
NeoPixel.setBrightness(0) # Reset brightness
for pixel in range(NUM_PIXELS):
NeoPixel.setPixelColor(pixel, Color(0, 0, 0))
NeoPixel.show()
sys.exit()
finally:
print("end animation") if DEBUG_MSG else None
# Clean up code before exiting the script
NeoPixel.setBrightness(0) # Reset brightness
for pixel in range(NUM_PIXELS):
NeoPixel.setPixelColor(pixel, Color(0, 0, 0))
NeoPixel.show()
try:
print("waiting input") if DEBUG_MSG else None
while True:
# Wait for GPIO16 to be grounded
GPIO.wait_for_edge(GPIO_PIN, GPIO.FALLING)
rainbow_animation()
except KeyboardInterrupt:
print("exit waiting input") if DEBUG_MSG else None
NeoPixel.setBrightness(0) # Reset brightness
for pixel in range(NUM_PIXELS):
NeoPixel.setPixelColor(pixel, Color(0, 0, 0))
NeoPixel.show()
sys.exit()
finally:
NeoPixel.setBrightness(0) # Reset brightness
for pixel in range(NUM_PIXELS):
NeoPixel.setPixelColor(pixel, Color(0, 0, 0))
NeoPixel.show()
GPIO.cleanup()
sys.exit()