I'm trying to start the code on a raspberry Pi with a button and as soon another button is pressed I want the code to stop what is doing and move to the other function.
I've been scratching my head all day as I'm trying to break this loop instantly:
# Import RPi GPIO module
from gpiozero import Button
#Assign name to buttons GPIO pins
button1 = Button(2)
button2 = Button(4)
def f1():
while True:
if button2.is_pressed:
break
input = float(input('Select grade: '))
if input < 10:
print('Less than 10')
def f2():
while True:
if button1.is_pressed:
break
input= float(input('Select grade: '))
if input < 10:
print('Less than 10')
while True:
if button1.is_pressed:
f1()
elif button2.is_pressed:
f2()
It also happens if I set the input to be:
serial = serial.Serial("/dev/ttyACM1",115200)
serial_value = serial.readline().decode('utf8').rstrip()
scale_input = float(serial_value)
input = scale_input
The input works fine in both cases and the code flows well although it only breaks if I hold the button until the serial or input() have got their values, unfortunately this is not correct, what I need is to kill for instance the function c1() to switch to c2() instantly regardless whether the code has received the input or not.
I have tried to play around with some threading running the "input" part of the code on a separate function and calling it with threading but the code goes berzerk.
I have a feeling that threading is the way to go but I'm not sure how to approach it, obviously the code is getting stuck at the input and the code doesn't move until a value is received not making the code reach to the break before the input is received.