Interrupting a For Loop

Good evening all

I have built a robot mower, it is controlled by Bluetooth (blutooth electronics app) where it moves in all directions with no hassle, i have recently built a subroutine that makes the mower move in a spiraling motion also known as an expanding square patter, called " Expsqaure()".

To make it work, i have the robot moving forward and right but each time the forward leg would extend by half a second. to achieve this i used a “for” loop. It works as expected. My problem is, once i activate it using Bluetooth ,for some reason, i cannot break out of the loop.

mower.ino (12.8 KB)

for some reason, i cannot break out of the loop

There’s no code to break out of the loop, other than the end condition.
if you want to break out of the loop, you must either meet the end condition (in your case del must be >= 77000) or you need to write more code in the loop to check for a condition, and break out of the loop.
you can use the break command, or you can add an additional check in the for loop, e.g.

  for (del = 1000; del < 77000 || stopLooping == true; del = del + 250)

I like it TRex, i will give it a shot

keep in mind that while the for loop is running, no other code in your program will be running. The arduino is a single threaded processor, so you cannot expect it to monitor bluetooth while you are asking it to perform a very long running function. You will need to be clever about it, and either break the long running loop down into smaller chunks that you can execute in between checking for bluetooth commands, or monitor the bluetooth commands from time to time within your long running loop.

My problem is, once i activate it using Bluetooth ,for some reason, i cannot break out of the loop.

What have you tried ?

As written

void expsquare()
{

  for (del = 1000; del < 77000; del = del + 250)
  {

    digitalWrite(BRAKE_B, LOW);  //  setting againg the brake LOW to disable motor brake
    digitalWrite(DIR_B, LOW);    // now change the direction to backward setting LOW the DIR_B pin
    digitalWrite(BRAKE_A, LOW);  // setting againg the brake LOW to disable motor brake
    digitalWrite(DIR_A, LOW);    // now change the direction to backward setting LOW the DIR_A pin
    analogWrite(PWM_A, 250);     // Set the speed of the motor
    analogWrite(PWM_B, 250);     // Set the speed of the motor
    delay(del);

    for (j = 0; j < 150 ; j++)
    {
      right();
    }

  }
}

There is no check inside the for loops for Bluetooth input and hence the loops will run to completion.
You would probably be better to use while loops with conditions dependant on what, if anything, had been received as well as the limits of values as used in the for loops.

i tried putting a code in the expsquare() loop that checks for bluetooth signal but that didn't work.

i tried putting a code in the expsquare() loop that checks for bluetooth signal but that didn't work.

Where? What actually happened?

"that didn't work" is just too lame for words.