1

This is the code:

def check_ultra():
    global arduinoSerialData, y, i, x
    while True:
        if arduinoSerialData.inWaiting() > 1:
            myData = arduinoSerialData.readline()
            myData = str(myData)
            myData = myData.replace("b'", '')
            myData = myData.replace("\\r\\n'", str(0))
            myData = myData.replace("\\r00.000", str(0))
            myData = myData.replace("\\r00.000", str(0))
            if "c" in myData:
                myData = myData.replace("c", str(0))

                if y == 1:
                    y = 3
                    if float(myData) > 15:
                        x = 0
                    return y, x
                else:
                    if float(myData) < 15 and float(myData) > 1:
                        y = 1
                        x = 0
                        return y, x
            elif "a" in myData:
                myData = myData.replace("a", str(0))
                if y == 2:
                    y = 3
                    if float(myData) > 15:
                        x = 0
                    return y, x
                else:
                    if float(myData) < 15 and float(myData) > 1:
                        y = 2
                        x = 0
                        return y, x
            else:
                y = 0
                return y


set_servo1_angle(0)
set_servo2_angle(90)
go_out_count = 0
m1 = 0
count = 3
y = 0
i = 0
x = 0
while y == None or y == 0 or x == 1:
    # i = 0
    # y = None
    check_ultra()
    if y == 1 and x == 0:
        print("1")
        sleep(2)
    elif y == 2 and x == 0:
        print('2')
        sleep(2)

What this currently does is it prints 1 every time sensor 1 is blocked, but then it ends the code. I want to make it such that every time I block a sensor, it prints the corresponding print statement, no matter how many times I block the sensor. I have tried adding many variables to help but they didnt work. How do I make the loop keep repeating?

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
strilz
  • 63
  • 7
  • So the final loop ends too soon? Looks to me like as soon as `y==1` and `x==0` the loop will print and then the while conditions will be all false, so the program exits. Make that final loop more like `while True:` if you want it to keep looping. – DisappointedByUnaccountableMod Aug 09 '20 at 18:34
  • If I make it while true, it would just go on forever, even if the sensors are not triggered – strilz Aug 10 '20 at 01:46
  • The logic of what you want is not at all clear - please edit your question to explain what it is that you actually want to happen, being very specific and clear about the condition for whan the program should exit - and if it doesn’t exit it will have to loop or do something else. – DisappointedByUnaccountableMod Aug 10 '20 at 06:40

2 Answers2

0

Your outer-most loop has the following condition: y==None or y==0 or x==1. The if statements in it are only going to print if all of those conditions are False. That means the loop is guaranteed to terminate on the first printout.

If you want the loop to keep going indefinitely, ignore everything and keep going. Replace the loop with

while True:
    check_ultra()
    ...

On an unrelated note, try to avoid globals. They may have unpredictable side effects when you set them in long-forgotten places, and generally make the code harder to maintain. Use arguments and return values instead.

For example:

def check_ultra(arduinoSerialData, x, y):
    while True:
        if arduinoSerialData.inWaiting() > 1:
            myData = arduinoSerialData.readline()
            myData = str(myData)
            myData = myData.replace("b'", '')
            myData = myData.replace("\\r\\n'", str(0))
            myData = myData.replace("\\r00.000", str(0))
            myData = myData.replace("\\r00.000", str(0))
            if "c" in myData:
                myData = float(myData.replace("c", "0"))

                if y == 1:
                    if myData > 15:
                        return 0, 3
                    return x, 3
                elif 1 < myData < 15:
                        return 0, 1
                return x, y
            elif "a" in myData:
                myData = float(myData.replace("a", "0"))
                if y == 2:
                    if myData > 15:
                    return 0, 3
                elif 1 < myData < 15:
                    return 0, 2
                return x, y
            else:
                return x, 0

You would call it as

x, y = check_ultra(arduinoSerialData, x, y)
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
-1

'break' statement ends the while loop, so try replacing it with 'pass', like this:

elif y == 2:
   print('2')
   pass
Stefan J.
  • 15
  • 3