Exiting a While Loop

hi guys i had a question

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);
}
}

thankyou

Add a


return;

where you want it to exit

wait what?

Why do you have the whole boolean thing? Could you just use the following instead?


while (SensorValue[motorencoder] < target && fabs(error) > 5)

If you did this, then you’d get rid of the variable bContinue as well as the if and else statements near the bottom of the loop.

Or am I missing something about what you’re trying to do?

Use timers.


ClearTimer(T1);
while(time1[T1] < time_you_want) {
    // Do stuff
}

@Sgarg14


while(true){
    if (thing that makes you want to exit){
        break;
    }
}

The break command exits the current loop

If you want to exit the entire function, replace the break with


return

+1

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.

Thanks for the advice guys but i am looking for something like


ClearTimer(T1);
while(time1[T1] < time_you_want) {
    // Do stuff
}

If you guys have any other advice like this please do tell me

Thanks

Here is some code from an old (Sack Attack) robot that shows how I have done this type of thing in the past.

/*-----------------------------------------------------------------------------*/
/*  Drive forwards until encoder count has elapsed or timeout reached          */
/*  optionaly stop motors at end                                               */
/*-----------------------------------------------------------------------------*/

void
DriveForwardsEncoder( int distance, bool stopdrive = true )
{
    long    cur_position;
    long    target_position;
    long    currentTime;

    cur_position = nMotorEncoder MotorLF ];

    target_position = cur_position + distance;

    // Start driving - bias to left
    if(distance > 0)
        DriveSystemDrive( DRIVE_FORWARD_SPEED, 0,  0 );
    else
        DriveSystemDrive( -DRIVE_FORWARD_SPEED, 0, 0 );

    // wait for end with timeout
    for( currentTime = nSysTime; currentTime > (nSysTime - DEFAULT_DRIVE_TIMEOUT);)
        {
        cur_position = nMotorEncoder MotorLF ];
        if( distance >= 0)
            {
            if( cur_position >= target_position )
                break;
            }
        else
            {
            if( cur_position <= target_position )
                break;
            }

        wait1Msec(10);
        }

    // Stop driving
    if( stopdrive )
        DriveSystemDrive( 0, 0, 0 );
}

DEFAULT_DRIVE_TIMEOUT was defined as 2.5 Seconds in that particular program.


#define DRIVE_FORWARD_SPEED     127
#define DEFAULT_DRIVE_TIMEOUT  2500

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.