Move forward until ultrasonic distance detection


I’m thinking this program should go forward until the sensor detected distance is smaller than 50cm but it doesn’t. It just goes forward forever and cannot even be stopped by the stop button as reported in my previous post.

This one does work but I prefer the syntax of the first one.


How come the first program always go forward?


Oh I see.

My intuition was that when the program exits it should stop doing whatever it is doing but it seems that it is not the case.

This program does work as I was first expecting:

Wouldn’t it make more sense to stop moving forward when the program ends?


Hi fgervais,

Keep in mind that the mBot executes the instructions stupidly. When the instruction: Move forward at power 50% is received, it is transmitted to the motors and repeated until the ultrasonic sensor is smaller than 50. The motors receive the instruction at each pass through the loop but when you exit the loop, the motors no longer receive any instruction and continue with the last instruction which is to move forward. So you have to do as you did: Send the stop instruction.


Me, when the condition is simple and there is no processing in the loop, I like this syntax.


This syntax looks the best to me too, thank you!


This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.