How to get 2 while loops running together?

Our robot has 2 motors with IMEs on our drive. We want to use them to move a certain distance while using PID control to make sure that they move at the same speed. I am thinking about using while loops for this, but I wouldn’t know how to make sure they both travel the same distance before they stop, in just one while loop.

(Please note: I haven’t tried the attached code, but I’m assuming by the way loops work that these statements wouldn’t run at the same time. Also, the encoder values are just random, it’s the concept that matters to my question.)

task main()
{
	while (nMotorEncoder[BRight] < 1234)
	{
		motor[BRight] = 100;
		motor[FRight] = motor[BRight];
	}
	while (nMotorEncoder[BLeft] < 1234)
	{
		motor[BLeft] = 100;
		motor[FLeft] = motor[BLeft];
	}
	motor[FRight] = 0;
	motor[BRight] = 0;
	motor[FLeft] = 0;
	motor[BLeft] = 0;



}
  1. You could try using tasks
  2. If you really don’t want to learn tasks:

bool loopA = true;
bool loopB = true;
while(loopA || loopB)
{
  if(/*loopA exit condition*/)
    loopA = false;
  if(/*loopB exit condition*/)
    loopB = false;
  if(loopA)
  {
    //Stuff in loop a
  }
  if(loopB)
  {
    //Stuff in loop b
  }
}

The code will end when both loops end.

Can I use “or” paramterers?

Something like this;


task main()
{
	while (nMotorEncoder[BRight] < 1234 || nMotorEncoder[BLeft] < 1234)
	{
		while (nMotorEncoder[BRight] < 1234)
		{
			motor[BRight] = 100;
			motor[FRight] = motor[BRight];
		}
		motor[FRight] = 0;
		motor[BRight] = 0;
		while (nMotorEncoder[BLeft] < 1234)
		{
			motor[BLeft] = 100;
			motor[FLeft] = motor[BLeft];
		}
	}

}

Also, the front 2 drive motors aren’t using PID control because they don’t have IMEs. Am I going the right way about changing their values to keep them in line with the PID on the back motors?

We had a very similar problem about whether or not we should use tasks or not.
In this link, we were almost forced to use tasks until somebody pointed out something very obvious.
We changed all the while loops to if statements and put them in one big while loop, as follows:

task main()
{
	while (true) {
		
		if (nMotorEncoder[BRight] < 1234)
		{
			motor[BRight] = 100;
			motor[FRight] = motor[BRight];
		}
		
		if (nMotorEncoder[BLeft] < 1234)
		{
			motor[BLeft] = 100;
			motor[FLeft] = motor[BLeft];
		}
			
	}

}

We tested it, and it worked for us! Since the if statements are in a while loop, it’s constantly testing them as if they were normal while loops, AND it runs through both of them!

In addition, In your code, if you were trying to say "Once nMotorEncoder[BLeft]/[BRight] is equal to 1234, then STOP the motors " , just do this:

task main()
{
	while (true) {

		if (nMotorEncoder[BRight] < 1234)
		{
			motor[BRight] = 100;
			motor[FRight] = motor[BRight];
		}

		if (nMotorEncoder[BLeft] < 1234)
		{
			motor[BLeft] = 100;
			motor[FLeft] = motor[BLeft];
		}


	}
	if (nMotorEncoder[BRight] == 1234 || nMotorEncoder[BRight] > 1234 || nMotorEncoder[BLeft] == 1234 || nMotorEncoder[BLeft] > 1234) {
		motor[FRight] = 0;
		motor[BRight] = 0;
		motor[FLeft] = 0;
		motor[BLeft] = 0;

	}



}

The code won’t work because it will wait for the first loop to end before even starting the second one.
And yeah I think changing the values is fine.

It might work for some situations, but there is a difference. If you use tasks, once motor[BRight] is greater than or equal to 1234, the loop controlling the right side will stop, so nothing will happen if motor[BRight] becomes less than 1234. However, for the big while loop you are suggesting, the “smaller loops” will never stop.

There are no “smaller loops”. One while loop, two if statements.

Wouldn’t this be better for the other person’s code?


task main()
{
	while (true) {

		if (nMotorEncoder[BRight] < 1234)
		{
			motor[BRight] = 100;
			motor[FRight] = motor[BRight];
		}
		else
		{
			motor[FRight] = 0;
			motor[BRight] = 0;
		}

		if (nMotorEncoder[BLeft] < 1234)
		{
			motor[BLeft] = 100;
			motor[FLeft] = motor[BLeft];
		}
		else
		{
			motor[FLeft] = 0;
			motor[BLeft] = 0;
		}


	}

If by other person you mean me, then yes. I haven’t seen your robot, so I didn’t know which motors you wanted stopped and when.

Okay. Thanks!

If and when you find that multitasking (tasks) could be of assistance, have a read of this article. Multitasking is a rabbit hole of complex issues and crashed dreams when done incorrectly however, so be careful.

That’s why I put them in quotes. The code is trying to simulate 2 smaller loops with one big loop.