Running Python on a Raspberry Pi Zero. I have one button connected to GPIO26, set for input and pulled up.
I setup my interrupt like this:
GPIO.add_event_detect(26, GPIO.FALLING, callback=myInterrupt, bouncetime=500)
And my interrupt handler is:
def myInterrupt(channel): global buttonStatus start_time = time.time() while GPIO.input(channel) == 0: # Wait for the button up pass buttonTime = time.time() - start_time # How long was the button down? if buttonTime >= .1: # Ignore noise buttonStatus = 1 # 1= brief push if buttonTime >= 2: buttonStatus = 2 # 2= Long push if buttonTime >= 4: buttonStatus = 3 # 3= really long push
In the main I test buttonStatus to select an action whether the button is pressed briefly, for two seconds or more than three. ButtonStatus 1 and 2 are pretty reliable, but I rarely can get a buttonStatus of 3. I'll hold the button for 6 to 10 seconds, but the function most often returns a buttonStatus of 2.
Any guess why, or is there a better method to determine how long the button is being pressed? Am I getting an interrupt recursion from a dirty button?