so i was wondering if you guys coud help with exiting a while loop using time. one of my friends have done it before so i know its a thing. so PLEASE HELP ME MY COMP IS IN A DAY
this is how i exit while loop right now
void drivePD(int target)
{
const float Kp = .6; //must test for this value, I am just making it up
const float Kd = .9;//must test for this value, I am just making it up
int error;
int derivative;
int previousError;
int motorSpeed;
bool bContinue = true;
while(bContinue) //this is what you need in order to keep checking the sensor
{
error = target - SensorValue[motorencoder];
derivative = error - previousError;
previousError = error;
motorSpeed = (error*Kp) + (derivative*Kd);
motor[bothrightmotor] = motorSpeed;
motor[bothleftmotor] = motorSpeed;
if(fabs(error)> 5){
}
else
{
bContinue = false;
}
wait1Msec(20);
}
}
That makes me think “Ewww!” Best practice is not to use break as your regular exit to a loop. If you intend to exit a loop, you really should write in the exit conditions.
As for the practice of using booleans to exit a loop, it can be a good one. The conditions may register as something somewhere in the middle of the loop, but not show up in a check between iterations. A record of that can be kept in a boolean so the boolean is holding the true/false to be checked between iterations.
nSysTime give the current system time in mS, the loop exits when the current system time is greater than the initial system time plus (in this case) 2500mS, although I wrote it a little differently above. You can apply the same technique to your while loop.
There’s a large repo of code from past seasons that you could look at for inspiration here.