I'm trying to build a program that has 2 inputs connected to 2 limit switches and each of these needs to be first opened and then closed before the function respective to each button begins. As a safety feature, I would like to block the code until the button is closed again. After looking around online I found out that the best library is gpiozero but I can't make it work :(
The code gets stuck on the block wait_for_release()
and nothing happens when releasing the button.
I don't understand why if I put btn1.wait_for_press()
it goes through that but not on wait_for_release
.
#btn1.wait_for_press()
#print('If I uncomment wait_for_press it comes here but get stuck on the release anyway')
Is there a way to make this work? Even another method is appreciated!
Doing wait_for_press()
and then wait_for_release()
doesn't work for me because I need either 1 of the 2 inputs to be pressed.
Thanks in advance, here is the code:
from gpiozero import Button
from signal import pause
btn1 = Button(12, bounce_time=0.2)
btn2= Button(20, bounce_time=0.2)
def release_btn_1():
print('Btn1 pressed')
#btn1.wait_for_press()
#print('Wait for press works')
btn1.wait_for_release()
print('Btn1 released')
#function 1
def release_btn_2():
print('Btn2 pressed')
btn2.wait_for_release()
print('Btn2 released')
#function 2
btn1.when_pressed = release_btn_1
btn2.when_pressed = release_btn_2
pause()