python - 树莓派运行时错误 : Conflicting edge detection already enabled for this GPIO channel

标签 python python-2.7 raspberry-pi gpio raspberry-pi3

我正在按照此处找到的教程进行操作: https://www.linkedin.com/pulse/prepare-your-raspberry-pi-work-aws-iot-kay-lerch

我什至还没有开始它的互联网部分,因为我在电路方面遇到了问题。我使用我的树莓派 3 连接我的电路,如下图所示。 enter image description here

然后我按照教程中所示编写了以下 python 脚本。

import RPi.GPIO as gpio

gpio.setmode(gpio.BOARD)
gpio.setup(7, gpio.IN, pull_up_down=gpio.PUD_DOWN)

def on_pushdown(channel):
        print "Button Pushed."

while(True):
        gpio.add_event_detect(7, gpio.RISING, callback=on_pushdown, bouncetime=200)
gpio.cleanup()

当我按下按钮时,这应该打印出“Button Pushed”,但我收到以下运行时错误:

Traceback (most recent call last):
  File "button.py", line 10, in <module>
    gpio.add_event_detect(7, gpio.RISING, callback=on_pushdown, bouncetime=200)
RuntimeError: Conflicting edge detection already enabled for this GPIO channel

我有 RPi.GPIO 版本 0.6.2,这是发布这篇文章时的最新版本。如果有人能提供任何帮助,我将不胜感激。

最佳答案

您拥有的代码不断添加事件检测回调(在 while(True) 循环中)。你想要的是添加一次事件检测回调,然后等待边缘。

page有一个很好的例子,你可能想要通过。

或者,您可以尝试以下方法:

import RPi.GPIO as gpio

gpio.setmode(gpio.BOARD)
gpio.setup(7, gpio.IN, pull_up_down=gpio.PUD_DOWN)

def on_pushdown(channel):
    print "Button Pushed."

# only add the detection call once!
gpio.add_event_detect(7, gpio.RISING, callback=on_pushdown, bouncetime=200)

while(True):
    try:
        # do any other processing, while waiting for the edge detection
        sleep(1) # sleep 1 sec
    finally:
        gpio.cleanup()

关于python - 树莓派运行时错误 : Conflicting edge detection already enabled for this GPIO channel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38125047/

相关文章:

拔下 Raspberry Pi 后导入模块时出现 Python EOFerror

Python lxml.html XPath "attribute not equal"运算符未按预期工作

python - Django HttpResponse Excel

python - 绑定(bind)套接字时权限被拒绝

python - 在 python 中使用 getattr 的替代方法?

c++ - 体验在 Raspberry Pi 上为 PCF8575 I/O 扩展器编写 C 代码

python - 你如何在 python 中验证鸭子类型的接口(interface)?

python - 如何使 C 包装器(用 32 位 Python 编写)在运行 64 位 Python 的新机器上工作?

Python 2.7 : Variable "is not defined"

c - 如何最小化程序启动时间