Logic issue trying to break a loop


I’m trying to break out of a move forward loop if an object is detected by the ultrasonic sensor. The problem is, it is detected but the robot keeps moving forward and doesn’t break the loop. I have another block that runs if investigateVar is 1 and it properly executes. Can someone help me with what might be wrong with this code?

43 AM


Just some quick thoughts.

  1. I don’t see where you are initializing the investigate value. Not sure that is an issue, but best practice is to always initialize variables before their use.
  2. Not sure where/how the Patrol block is being called. Might be helpful to post a screenshot of all of the code.
  3. Under the covers, the code uses floating point variables so you would really be better of structuring the timerVar = 40 to something like timerVar > 39 just to be safe. This is common practice when comparing values that may be floating point versus integer (and no, you can’t specify the value to be an integer in Scratch as far as I know).
  4. It might also be useful to set a variable named false to zero, and then test for not(false) to see if that makes a difference (see #3 for potential issue with comparing floating point numbers).
  5. I would suggest dropping the last if-then statement and debug the loop first. That’s likely where the issue resides.


Hi Chuck, thanks for replying. I’m going to post the rest of the code. My thought was that the more simple the code was that I posted, the more likely the response. I stripped my code all the way down to just breaking the loop and stopping though, and that was successful. So, that obviously isn’t my problem.

With the code you see below, when I put an object in front of the bot, the lights turn on but it simply keeps moving forward. It actually stops turning at that point and will just keep moving towards the object as long as I keep it in front of it.

I put the if statement in the turn, because in a previous revision of my code, an object in front would break the loop, but it would immediately turn. I want it to come to a complete stop when an object is in front.

The timerVar is just an arbitrary value. I started using that after reading somewhere on here that using wait is bad because it stops everythinig. The advice was to use the milli() ?? function, but I am trying to stay out of the code completely and do this in mBlock, so incrementing a variable was the next best thing I could come up with.

55 PM


You might want to take a look at the programs here from my book A Gentle Introduction to Robotics. The book is all Scratch code. If you’re interested in getting a copy, the proceeds go toward funding kids robotics. The purchase link is here.


Thanks for that code. When it executes avoidObstacle, all of those movements are executed. Is it supposed to do that if the obstacle is only in front of it?

Basically the same behavior I am currently experiencing with my code, where it doesn’t break the loop even when the repeat until condition is met.

I do like the turn logic. I had problems getting it to consistently make a 90 degree turn and this works better than my code.


Hi chuckmcknight,
I haven’t been on the forums for a while, and was happy to see you’ve completed your book! I excitedly downloaded it. It looks fantastic. Thanks for all your efforts, including all the helpful input on the forums.


Hi @CoolPink,

I’m working on the follow-up volume to program the mBot using the Arduino environment now. It’s been somewhat slow going because I got drafted to coach my son’s robotics team for the FIRST Tech Challenge the last couple of years and that’s been almost a second full-time job. :-/

Let me know if you have any questions!

Best regards,