Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 1585

Python • Wait for 2 Buttons to be pressed.

$
0
0
Hi all,

I have 2 Buttons connected to the GPIOs of the Raspi.
I want the Python script to wait for either one of them being pressed.
Unfortunately i am not able to get it running.
Here is the basic script:
I know, that with this script, I am only able to press Button 1 first, then Button 5.

Code:

from gpiozero import Buttonwhile True:            button1.wait_for_press()        count += 1        print(count)        button1.wait_for_release()            button5.wait_for_press()        count += 5        print(count)        button5.wait_for_release()    

I tried several options with .is_pressed and .value etc.
But non of them worked properly.

When i run this script, i can check if either one or the other is pressed, but it counts multiple times for some reason each time i press the button.

Code:

while True:            if button1.is_pressed == True:              count += 1              print(count)              button1.wait_for_release()            if button5.is_pressed == True:              count += 5              print(count)              button5.wait_for_release()

In the below SC, i only pressed the button only once, and i counted 4 times.

Thank you

Image

Statistics: Posted by DanniDeleto — Mon Mar 24, 2025 3:43 pm



Viewing all articles
Browse latest Browse all 1585

Trending Articles