我目前正在考虑向我的脚本中添加另一个按钮。目前,它正在全屏显示我的视频(相机)的输出,直到检测到按下按钮,然后它才会执行某些操作。这看起来像这样我这样做了:
while True:
camera.preview_fullscreen = True
camera.preview_alpha = 128
camera.start_preview()
GPIO.wait_for_edge(picture_pin, GPIO.FALLING)
action()
但是,我现在想介绍另一个执行不同操作的按钮。因此,我想加入这个:
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)
但是,它给了我这个错误:
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
这个错误与我读到的关于这个函数的内容冲突,所以我不确定为什么它会给我这个错误。谁能指出我正确的方向?
最佳答案
将 add_event_detect
方法放在 While 循环之外,这没有用,因为在检测到事件时会调用回调函数:
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"
关于Python等待按下按钮(多个输入),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33847603/