Hello all! The following is a snippet of code, which is supposed to Self-Exit after a certain amount of time using a for loop. (Time out)
However, when that function runs, the Timeout seems to skip (or is very short) and it goes straight to "Program Complete"
I haven't really used For-Loops yet. Any help would be awesome!
void program1() { //Just one piece of code
 Serial.println(">Running Program 1");
 Serial.println("All data lines closed during this operation");
 Serial.println("This program has a timeout, not an exit");
 int timeout;
 AttachFeet(); //This attaches 2 servos
 delay(500);
 for (timeout; timeout > 200000; timeout++) { //The program is supposed to exit after a certain amount of time
  StateChange(); // This function determins if there is an obstacle w/ HC-SR04 sensor
  if (ForwardObstacle == false) {
   WalkForward();
  }
  else {
   Stop();
   delay(50);
   WalkLeft();
   delay(500);
   Stop();
   delay(50);
  }
  delay(10);
 }
 Serial.println(">Program Complete");
}
void program1() { //Just one piece of code
Serial.println(">Running Program 1");
Serial.println("All data lines closed during this operation");
Serial.println("This program has a timeout, not an exit");
int TIMEOUT_PERIOD = 20;
AttachFeet(); //This attaches 2 servos
delay(500);
while (1 < 2) {
uint32_t ts = millis();
while (millis() - ts > TIMEOUT_PERIOD) {
StateChange(); // This function determins if there is an obstacle w/ HC-SR04 sensor
if (ForwardObstacle == false) {
WalkForward();
}
else {
Stop();
delay(50);
WalkLeft();
delay(500);
Stop();
delay(50);
}
}
}
Serial.println(">Program Complete");
}
Yes, that's what it does. I thought I needed a while loop to loop the program. But wait, isn't the point of a for-loop to loop anyways? Doesn't that mean I'm while-looping a for-loop?
I think I just created "Loop-ception." I guess I should get rid of that... (?)
Robin2:
I don't like the idea of using a WHILE or a FOR for a timeout as they block everything else.
Just record the value of millis() when the function starts and subsequently check
if (millis() - startMillis >= timeoutMillis) {
// time is up folks
}
...R
Actually, I kind of want the rest of the program to hang. When this function (Program1) runs, I want it to be the only thing running at the time. So..... I see what you're saying, but yeah.
aarg:
Not when the loop() function is provided to do that for you. The name is kind of a dead giveaway.
Yeah. IDK why I thought that. The name is a dead give away:
#loopception <--- new trend
Jiggy-Ninja:
int timeout;
for (timeout; timeout > 200000; timeout++)
So there's 3 problems with this awful snippet:
1) timeout is an int (h/t: AWOL). This means it's maximum value is 32,767. There is no way it will ever reach 200,000.
2) timeout is *uninitialized*. It's starting value is unknown.
3) The for loop executes for as long as the middle condition is **true**. Yours will be immediately (and always) **false**.
Allright. I THINK I corrected those three problems. Here's the code: (I commented where I did corrections)
void program1() { //Just one piece of code
Serial.println(">Running Program 1");
Serial.println("All data lines closed during this operation");
Serial.println("This program has a timeout, not an exit");
AttachFeet();
delay(500);
//The While loop is gone
int timeout = 0; //Timeout is initialized now
for (timeout; timeout >= 32767; timeout++) { //The number is now within the range of an int. Therefore, it can be true.
StateChange();
if (ForwardObstacle == false) {
WalkForward();
}
else {
Stop();
delay(50);
WalkLeft();
delay(500);
Stop();
delay(50);
}
}
Serial.println(">Program Complete");
}