我有一个Raspberry Pi通过NOOBS运行Raspbian。我有一个连接到引脚1和11的按钮。我正在尝试使用GPIO的.add_event_detect和RPIO.RISING在按下按钮时调用一个函数。 (回调打开指示灯2秒钟,然后将其关闭。)
我发现RPIO.RISING功能在按下按钮时调用回调(引脚11从0变为1)并且按钮释放(引脚11从1变为0)。灯亮了两次,就像我使用RPIO.BOTH一样。
我不认为这是一个滞后/嘈杂的信号问题,因为我可以按下按钮很多秒,然后放开再看看再次调用回调。
以下是示例代码:
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)
是否有可用于解决此问题的软件解决方法?
答案 0 :(得分:0)
无论出于何种原因,bounctime的实现都很奇怪。 如果你在设定的弹跳时间800毫秒内按住并释放按钮,它应该可以正常工作。如果你持续时间更长,那么你有时会触发释放。我有同样的问题,思考&bounctime'那是'系统'忽略了所有其他输入......比如“解决”问题。开关的时间。不是。因此,只要您在设置的弹跳时间内按下并释放按钮,就可以正常工作了。
尼克