我已经阅读了 RPi.GPIO 的文档,并搜索了 Google 以及 SO,但找不到可能是一个非常愚蠢的问题的解决方案。我试图只检测按下按钮的边缘。但无论我是否指定寻找“下降”或“上升”边缘,Pi 都会在按下和释放按钮时执行命令。有时它会多次执行代码。我的代码:
import RPi.GPIO as GPIO
buttonPin = 4 # this is the pin for the button
GPIO.setmode(GPIO.BCM) # pinmode
GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) #setting up my pin to be input w/ pullup resistor
if __name__ == '__main__':
while True: # loop
GPIO.wait_for_edge(buttonPin,GPIO.RISING) # looking for a rising edge
print('Edge detected') # this happens regardless of my button being pressed or released
很确定我在这里遗漏了一些基本的东西,非常感谢任何帮助。
最佳答案
您可以使用参数bouncetime
以编程方式解决它,但是,您必须使用
GPIO.add_event_detect( channel ,GPIO.RISING,callback=my_callback,bouncetime=200)
或
GPIO.add_event_callback( channel ,my_callback,bouncetime=200)
而不是GPIO.wait_for_edge(channel,GPIO.RISING)
或者使用额外的硬件:在开关上添加一个 0.1uF 电容器,
或者您可以结合使用两者。
更多内容请参见 Documentation
和平
关于python - RPi.GPIO.wait_for_edge(4, GPIO.FALLING) 检测按钮的按下和释放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53623359/