0

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.

flxx
  • 49
  • 6
  • Yes, it's because `input` is blocking. I assume you're using Raspberry Pie? I don't know what it supports but most embedded devices doesn't support threading, but even if it does you probably don't want to run code in parallel in your situation. The most proper solutions is using a non-blocking input function or setting interrupts on your buttons. – Ted Klein Bergman Dec 26 '21 at 11:19
  • @TedKleinBergman Yes you are correct, I forgot to mention it, it is a Raspberry Pi I will edit it now. How could I setup the code to receive serial data with a non-blocking input? I have just started with coding few weeks ago and any help will be greatly appreciated. – flxx Dec 26 '21 at 11:23
  • You have to look it. I don't know Raspberry Pie or what OS you're running on it, or if you're even running an OS on it, and even less how to do it in python. [This](https://stackoverflow.com/a/32382950/6486738) seemed to be a linux specific solution in python, but it's rather complicated. I think it's easier to set up an interrupt, but your library doesn't seem to have it (only did a very quick googling). – Ted Klein Bergman Dec 26 '21 at 11:31
  • I am running Debian but anyway I will look into the button interrupt, I know that there are more libraries available for RPi. – flxx Dec 26 '21 at 11:35

0 Answers0