Clearing Timer in Pros

Currently for my autonomous, I want to exit my P loop with a timer that lets the robot oscillate/adjust itself within a period of time. Is there a function or a way that allows me to clear the timer in pros?

bool timerClear = true; // continues to clear timer until error is small

if(error1 < 7 && error2 < 7 && error3 < 7 && error4 < 7) {
  timerClear = !timerClear;
}

if(timerClear) {
  // clears timer
}

You can use pros::millis to to create your own timer.
https://pros.cs.purdue.edu/v5/api/cpp/rtos.html#millis

Would this be able to reset the timer?

bool timeClear = true;
int now = pros::millis();

if(error > 10) {// if the robot isn't close to desired target -> clear timer
  timeClear = true;
}

if(timeClear) {
  now = 0;
}

if(error < 10) {// if the robot is close to desired target -> start timer
  timeClear = !timeClear;
}

OK. I think I know understand what you want.

This is not the best implementation of a state machine but for two states that are within other code running its at least readable:

// pseudo code
start_time = 0;
state_wait_until_close = 1;
state_timeout_when_close=0;
error = <set point> - <current point>

while ( abs(error) > ERROR_MAX_ALLOWED) {
  
  // when error is close, set the current time for use with the timeout 
  if ( state_wait_until_close &&  (abs(error) < ERROR_GETTING_CLOSE) ) {
    start_time = pros::millis(); 
    state_wait_until_close = 0;
    state_timeout_when_close = 1; 
  }

  // timeout if PID is taking too long and is close enough
  if ( state_timeout_when_close && pros::millis() > start_time + CONST_TIME_LIMIT) {
    state_wait_until_close = 1;            // may or may not be needed based
    state_timeout_when_close = 0;  // on if you use this code again w/ init vars
    break;
  }

  <PID STUFF>
  pros::delay(20); 
  error = <set point> - <current point>
}
2 Likes