3

I have several lever switches that represent scores. I want a single callback function when pressed that acts on the value of that lever. I have this working except it seems to call the function twice. This stops happening if I remove the second add_event_detect for the other channel

GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
def buttonPressed(value):
 print('Scored! %s'%value)
 print(time.time())
GPIO.add_event_detect(17, GPIO.FALLING, callback=lambda x: buttonPressed(50), bouncetime=2000)
GPIO.add_event_detect(27, GPIO.FALLING, callback=lambda x: buttonPressed(150), bouncetime=2000)

When I push the switch once on channel 17 I get outputted:

 Scored! 50
 1374739135.76
 Scored! 50
 1374739135.76

Note the extra space on the third line, not sure if that has any importance to the issue.

How can I make sure it only gets called once when the lever is pressed and why is adding the second add_event_detect causing the callback function to be fired?

asked Jul 24, 2013 at 17:42
2
  • and your question is? Commented Jul 24, 2013 at 21:12
  • sorry if it wasn't clear in the intro text, I've added it again at the end Commented Jul 24, 2013 at 21:21

1 Answer 1

2

I (削除) have (削除ここまで) had no idea what that lamda is supposed to do, but this is what I would use:

GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
def buttonPressed(channel):
 if channel==17:
 value=50
 elif channel==27:
 value=150
 print('Scored! %s'%value)
 print(time.time())
GPIO.add_event_detect(17, GPIO.FALLING, callback=buttonPressed, bouncetime=2000)
GPIO.add_event_detect(27, GPIO.FALLING, callback=buttonPressed, bouncetime=2000)
answered Jul 25, 2013 at 15:44

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.