Quantcast
Viewing all articles
Browse latest Browse all 1264

Python • Reading button state at program start

I am using the gpiozero library on my RPI 5 to read the state of a button. The button is part of a robotics application and is pressed when a mechanical part has reached its extreme position. Essentially, I use the following code:

Code:

from gpiozero import Buttonbtn = Button(12, bounce_time=0.05, hold_time=0)def pressed(btn):    print('Pressed')def held(btn):    print('Held')    def released(btn):    print('Released')    btn.when_pressed = pressedbtn.when_released = releasedbtn.when_held = heldinput('Press Enter to quit')
This works fine as long as the program runs. My problem occurs when this program starts while the button is already in a pressed state (indicating the mechanical part is in its extreme position). The above code (i.c. the held-event) does not detect this, as apparently it is only triggered by state transitions.

My question is whether it is possible to detect the pressed (held) state at program start

Statistics: Posted by johnpool — Wed Nov 20, 2024 3:05 pm



Viewing all articles
Browse latest Browse all 1264

Trending Articles