多个按钮 Raspberry Pi

Multiple buttons Raspberry Pi

我是 python 的新手,遇到了问题。我正在使用 Raspberry Pi 为学校开展一个项目,但无法同时读取两个按钮。两个按钮都有效,但我不知道如何同时从两个按钮获得输入。我只设法先阅读按钮 1,然后按钮 2 甚至无法阅读更多。我的问题是:如何以任意顺序多次阅读它们?

我遇到了同样的问题。首先必须声明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)

此代码有效,如有问题欢迎提问。