I am creating an event whenever my Raspberry Pi's GPIO pin has a falling edge. However, I want to disable this event for a certain amount of time (5 seconds for example) after each time it runs. I want the event to be enabled again after that time period.
My first thought was just to use sleep(5)
within the actual event function. But I believe this will not work due to the event being ran in a separate thread.
Can anyone point me in the right direction to what I am trying to accomplish? This is not as straightforward as I imagined it would be.
import RPi.GPIO as GPIO
import time
from time import sleep
# wait 1 second at startup
sleep(1)
# event function
def event(ev=None):
print("Event was triggered! Should not run again for 5 seconds.")
# sleep(5)
# initialize GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
# setup the pin and the event
GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.add_event_detect(21, GPIO.FALLING, callback=event)
while 1:
continue