简体   繁体   English

多个按钮Raspberry Pi

[英]Multiple buttons Raspberry Pi

I'm completely new to python and have a problem. 我是python的新手,有问题。 I'm working on a project for school with the Raspberry Pi and have trouble reading two buttons at once. 我正在使用Raspberry Pi进行学校项目,无法一次读取两个按钮。 Both buttons work but I dont know how I can get input from both at the same time. 两个按钮都可以,但是我不知道如何同时从两个按钮获得输入。 I only managed to read button 1 first and then button 2 couldn't even read them more then once. 我只设法先阅读了按钮1,然后按钮2再也读不到一次。 My question is: How can I manage to read them in any order and multiple times? 我的问题是:如何才能以任何顺序多次读取它们?

I had the same problem. 我有同样的问题。 First you must declare the GPIO, importing relevant GPIO library 首先,您必须声明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)

This code works, so please ask questions if you have any problems. 该代码有效,因此如果您有任何问题,请提出问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM