I need a little help if possible.
I'm trying to make a threaded function which will make my LEDs blink randomly as long as the button is pushed. when it is released it should exit the function and turn all LEDs off.
When I run the script below I get indent errors, but have no clue on where it goes wrong. All threaded function examples I find are either far more complex or not related to changing input/interaction.
Can anyone tell me where my indent goes wrong?
from threading import Thread
from gpiozero import Button, LEDBoard
import time
import random
leds = LEDBoard(yellow=16, green=19, red=20, blue=21)
button = Button(12)
def ledrandom(self):
while True:
x = random.randint(0,3)
y = random.randint(0,3)
if (x != y):
leds[x].on()
leds[y].on()
time.sleep(.1)
leds[x].off()
leds[y].off()
def pushed():
print("pushed")
t = Thread(target=ledrandom) # Create thread
t.start() # Start thread
def released():
print("released")
leds.off()
while True:
button.when_pressed = pushed
button.when_released = released