I wrote a small python script to have a light travel down a piece of ws2812b led strip that is triggered when I press the spacebar on the keyboard attached to the raspberry pi. The script works well, but I can only press the spacebar once and then have to wait for the light effect to travel down the strip. I'd like a way for every time I press space, it sends another light racing down the strip.
import keyboard #using keyboard module to get key presses
import time #to wait a certain amount of time
import neopixel
import board
# LED strip configuration:
LED_COUNT = 48 # Number of LED pixels.
LED_PIN = board.D18 # GPIO pin connected to the pixels (18 uses PWM!)
ORDER = neopixel.GRB #or RGB
CHASER_SIZE = 10
# Create NeoPixel object with appropriate configuration.
strip = neopixel.NeoPixel(
LED_PIN, LED_COUNT, brightness=1.0, auto_write=False, pixel_order=ORDER
)
#Clear the strip
strip.fill(0)
strip.show()
def chaser(GREEN=255, RED=255, BLUE=255, SPEED=.01):
for x in range(LED_COUNT+CHASER_SIZE):
for y in range(CHASER_SIZE):
if (x>LED_COUNT-1):
#print("X: ", y, " Y: ", y) #debug
strip[x-CHASER_SIZE]=(0)
else:
if (x-y>=0):
strip[x-y]=(GREEN/(y+1), RED/(y+1), BLUE/(y+1))
strip[x-CHASER_SIZE]=(0)
strip.show()
time.sleep(SPEED)
return
if __name__ == '__main__':
while True: #loop while running
if keyboard.is_pressed('esc'): #check if "Esc" was pressed
print("pressed esc")
#Clear the strip
strip.fill(0)
strip.show()
exit() #Quit program
if keyboard.is_pressed('space'):
chaser()