【问题标题】:Multiple buttons Raspberry Pi多个按钮 Raspberry Pi
【发布时间】:2017-02-14 09:42:07
【问题描述】:

我对 python 完全陌生,但遇到了问题。我正在使用 Raspberry Pi 为学校开展一个项目,但无法同时读取两个按钮。两个按钮都可以工作,但我不知道如何同时从两个按钮获得输入。我只设法先阅读按钮 1,然后按钮 2 甚至不能再阅读一次。我的问题是:我怎样才能以任意顺序多次阅读它们?

【问题讨论】:

  • 您可以使用单独的线程来读取按钮的状态,也可以将两个 if 嵌套在一起。回家后我可以给你一个解决方案

标签: python button raspberry-pi gpio


【解决方案1】:

我遇到了同样的问题。首先必须声明GPIO,导入相关的GPIO库

import RPi.GPIO as GPIO
import time

#Substitute 24 and 25 for whatever pins your push buttons are connected to.
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(25, GPIO.IN, pull_up_down=GPIO.PUD_UP)

#Then assign these buttons to the variables
Button_1 = GPIO.input(24)
Button_2 = GPIO.input(25)

while True:
    if Button_1 == False and Button_2 == False:
        print('Both buttons are pressed')
        time.sleep(0.2)

此代码有效,所以如果您有任何问题,请提出问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-27
    • 1970-01-01
    相关资源
    最近更新 更多