简体   繁体   中英

Multiple buttons Raspberry Pi

I'm completely new to python and have a problem. I'm working on a project for school with the Raspberry Pi and have trouble reading two buttons at once. 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. 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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