Solved||Self driving car to stop every tym obstacle


Please bare me have least knowledge on programming.
Trying to replicate and want to stop on every time obstacle.

def GoToTurn():
        while < 50:
        while not  my_linefollower.read_position() == "black" and > 51:
                if my_linefollower.read_position() == 'center' and > 51:
                if my_linefollower.read_position() == 'left' and > 51:
                if my_linefollower.read_position() == 'right' and > 51:

tried some basic and but seems not working.

original code (1.4 KB)



It would help knowing what you’re trying to achieve. My guess is you want to have the robot stop even when an obstacle appears out of nowhere, is that it ?

I haven’t tested this code but I would try it this way:

def GoToTurn():
    while < 20:
    while not  my_linefollower.read_position() == "black":
        if < 20:
        elif my_linefollower.read_position() == 'center':
        elif my_linefollower.read_position() == 'left':
        elif my_linefollower.read_position() == 'right':



@cleoqc will try this

Yes Exactly!!


@cleoqc thanks for the recommendation tried but not stable as original.
Movement is not precise with line follow.
Some time leaves the line or does not detect T intersections.

below video with original code only if statements


this sounds more like the lighting may have changed in the room, and you would need to calibrate the line follower again.
Have you tried recalibrating the line follower?



@cleoqc Yes I did the calibration seems elif statements take tym in loops and decide.

Where If statements are working seamless.
Anything with threading ?? or mutex can help


Did you try only adding the obstacle detecting “if”, without changing the three line following “if” statements to “elif”? (BTW, this is the “two line solution” I was hoping you would come up with yourself in the very beginning of your adventure.)

(Perhaps the sequence of commanding right, immediately followed by a test for position right is critical, such that the change to elif means only one adjustment per cycle?

def GoToTurn():
    while < 20:       # test and wait for no obstacle
    gpg.forward()                                                    # start moving
    while not  my_linefollower.read_position() == "black":     # loop until reach T or +
        while < 20:                        # test and wait for no obstacle
        if my_linefollower.read_position() == 'center':      # continue (in dir pointed) if on line
        if my_linefollower.read_position() == 'left':           # adjust if wandering left
        if my_linefollower.read_position() == 'right':        # adjust if wandering right

    # continue here if found T or +

Moving the obstacle checks to a separate thread is a very advanced topic that may or may not result in tighter directional control.

As it is, you do not know how often the loop is being executed, and do not know if multiple corrections in a single loop are occurring. Doctoral students have spent many years on this very subject of control (forward, left, right commands) with feedback (sensor readings).

Another thing to try might be to test for obstacles only if the position is center, not every loop. If the test for obstacles has introduced a control delay, it might be acceptable when centered, but not acceptable when making a correction. (This means moving the two line distance check. Where is left to the reader.)

There is even an optimization you could try by only reading position once each loop (save reading to a variable), and testing the variable. Since you don’t have any timings, this might improve control, or might not. Perhaps this is too advanced coding, so try the other two ideas first.


@cyclicalobsessive appreciate the code with explanation.

I tried above code early but not working as expected it don’t stop at all even obstacle present.


right, the distance if needs to be a while when the following are if.


yes that’s a challenge for me … looking for threading option if can be applicable.

  1. Could you explain “that” in “that’s a challenge for me”
  2. You certainly will not get a threaded solution from me.

I mean line follow n distance that’s a challenge
And will manage solution.