2

I'm currently looking at adding another button to my script. Currently, it's showing the output of my video (camera) fullscreen until the button press is detected, to then which it will do something. This looks like thisI have done this so:

while True:
        camera.preview_fullscreen = True
        camera.preview_alpha = 128
        camera.start_preview()
        GPIO.wait_for_edge(picture_pin, GPIO.FALLING)
        action()

However, I'm at the stage I want to introduce another button which does a different action. As such, I was thinking of putting in this:

import RPi.GPIO as GPIO

actionpin1 = 23
actionpin2 = 24

GPIO.setmode(GPIO.BCM)
GPIO.setup(actionpin1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(actionpin2, GPIO.IN, pull_up_down=GPIO.PUD_UP)

def action1():
        print "button pressed 1"

def action2():
        print "button pressed 2"

while True:
        print "waiting for button"
        GPIO.add_event_detect(actionpin1, GPIO.BOTH, callback=action1, bouncetime=800)
        GPIO.add_event_detect(actionpin2, GPIO.BOTH, callback=action2, bouncetime=800)

However, it's giving me this error:

Traceback (most recent call last): File "test.py", line 19, in GPIO.add_event_detect(actionpin1, GPIO.BOTH, callback=action1, bouncetime=800) RuntimeError: Conflicting edge detection already enabled for this GPIO channel

This error conflicts with what I've read on this function, so I'm not sure why it's giving me this error. Can anyone point me in the right direction?

4
  • 1
    What is actionpin1 and actionpin2 ? Do you have any GPIO stuff elsewhere in your code ? Commented Nov 21, 2015 at 19:30
  • Since the error message tells you the event-binding was already done for this GPIO channel, you should check which channel this is and whether you assigned edge detection to it somewhere before. Commented Nov 21, 2015 at 19:33
  • Eddited, perhaps this paints a better picture? Commented Nov 21, 2015 at 19:40
  • @Kagetaze Is my answer useful for you ? Commented Nov 22, 2015 at 11:04

1 Answer 1

3

Put the add_event_detect method outside the While loop which is not useful since callback function is called when event is detected :

import RPi.GPIO as GPIO

actionpin1 = 23
actionpin2 = 24

GPIO.setmode(GPIO.BCM)
GPIO.setup(actionpin1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(actionpin2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(actionpin1, GPIO.BOTH, callback=action1,bouncetime=800)
GPIO.add_event_detect(actionpin2, GPIO.BOTH, callback=action2, bouncetime=800)

def action1():
    print "button pressed 1"

def action2():
    print "button pressed 2"

while True:
    print "waiting for button"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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