I want a LED to flash, while some work is beeing done on my Raspberry. I am using a Thread for the LED in my Python script.
The initial code:
import RPi.GPIO
import time
import threading
pinLED = 10
pinButton = 12
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pinLED, GPIO.OUT)
GPIO.setup(pinButton, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.output(pinLED, 0)
Method for the Thread:
working = False
def flash():
status = 0
while working:
time.sleep(0.5)
if status == 0:
GPIO.output(pinLED, 1)
status = 1
else:
GPIO.output(pinLED, 0)
status = 0|
GPIO.output(pinLED, 0)
Logic:
try:
while True:
time.sleep(0.02) #found out that 0.02 is a good value to get every tiny button push and save resources
if GPIO.input(pinButton) == 1:
t = threading.Thread(target=flash)
working = True
t.start()
time.sleep(5) #work would be here
working = False
t.join()
except Exception as e:
print(e)
finally:
GPIO.cleanup()
When I start the script and press the button the first time, everything is working and the led is flashing. But when i press the button a second time, without restarting the script, the led isn't flashing. I printed some debug messages and found out, that t.start() is called, but for some reason it's doing nothing, also no Exception is thrown. Shouln't the LED start flashing every time I press the button again?