Reputation: 539
I have a Raspberry Pi running Raspbian via NOOBS. I have a button wired to pins 1 and 11. I'm attempting to use GPIO's .add_event_detect and RPIO.RISING to call a function upon the button press. (The callback turns on an led for 2 seconds, and then turns it off.)
I'm finding that the RPIO.RISING function is calling the callback on both the button press (pin 11 goes from 0 to 1) AND the button release (pin 11 goes from 1 to 0). The light is being turned on twice, exactly as it would if I were using RPIO.BOTH.
I don't think that this is a hysteresis / noisy signal issue, because I can depress the button for many seconds, and then let go and see the callback called again.
Here is the example code:
import RPi.GPIO as GPIO ## Import GPIO library
import time
#configure all of the inputs / outputs properly
def config():
#initalize the GPIO pin numbering
GPIO.setmode(GPIO.BOARD) ## Use board pin numbering
#setup output pins
GPIO.setup(8, GPIO.OUT) ## Setup GPIO Pin 7 to OUT
GPIO.setup(10,GPIO.OUT)
GPIO.setup(12,GPIO.OUT)
#initialize the inputs for the button
GPIO.setup(11, GPIO.IN)
#create the button-watching function
GPIO.add_event_detect(11, GPIO.RISING, callback=execute_lights, bouncetime=800)
#the light-turning-on function. One press turns yellow. Second press turns green, then off.
def execute_lights(channel):
print "executing lights: "
#Turn on the light we want
GPIO.output(8,True)
#turn green off after 2 seconds
time.sleep(2)
GPIO.output(8,False)
Is there a software workaround that I can use to address this issue?
Upvotes: 1
Views: 994
Reputation: 1
For whatever reason the implementation of bounctime is very strange. If you hold and release your button WITHIN your set bounce time of 800ms, it should work OK. If you hold it longer, then you will get triggering on the release, sometimes. I had the same issue, thinking 'bounctime' was the time that the 'system' ignored all other inputs...like the 'settling' time for a switch. It's not. So as long as u press and release your button WITHIN your set bouncetime, you should fine it works OK.
Nick
Upvotes: 0