簡體   English   中英

多個按鈕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)

該代碼有效,因此如果您有任何問題,請提出問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM