Keep in a box

Hi I’m trying to keep my Ardunio in a box using; the IR sensor array with a black border on some paper.

Currently the Ardunio does recognise when there is a black line, but the motors do not stop in time.

My program holds a set of instructions and executes them based on the R01_Logo example sketch.

What happens is the check for a black line happens after an execution for example;
[MOVE FORWARDS] → end → [CHECK FOR BLACK LINE] —> [MOVE FORWARDS]

How could I get it so it stops as soon as the black line has been detected, I would prefer to use either the Robot.motorsStop(); or Robot.waitContinue();.

void forwards()
{
 thingToDo = 1; //move forwards
 int i = 0;
 while (i < 11)
 {
 Robot.motorsWrite(100,100);
                 Robot.updateIR();
                
                
                  if(Robot.IRarray[3] < 400) //using 3rd to test
                  {
                   //react to blackline e.g. stop

                  }
                
 if (i == 10)
 {
 break;
 }
 i++;
 }
 thingToDo = 0; //stop moving
}

To test my program I using the 3rd IR sensor instead of the full array. I have tested the value output using the serial window and the returned numbers are fine.

Any help would be appreciated, thank you.

Other than a 10ms delay there is nothing in that code that blocks. The problem is somewhere else. I suggest you post all of your code.

                   //react to blackline e.g. stop

Comments don't make things happen.

Why are you breaking out of the while loop that way? A smart person would be using a for loop to iterate a fixed number of times.