I have a function PlaySequence() which plays a sequence of 'LED on' and 'LED off'. There are two LEDs and the times when each LED must be on or off are defined. The function may look like this, but could be much longer.
def PlaySequence(): GPIO.output(LED1, True) time.sleep(5) GPIO.output(LED2, True) time.sleep(1) GPIO.output(LED1, False) time.sleep(3) GPIO.output(LED1, True) GPIO.output(LED2, True) time.sleep(7) GPIO.output(LED2, False) time.sleep(12) GPIO.output(LED1, True) time.sleep(2) ...
I want to run this sequence only while a button is pressed. When the button is released, I want the sequence to stop. If the button is pressed again, I want it to start from the beginning. First, I tried to do this:
GPIO.add_event_detect(button, GPIO.RISING, callback=PlaySequence)
The problem with this is that PlaySequence() will continue to run even when the button is released.
How can I interrupt or stop a running function when an event (like falling edge) happens?