Function Timeout Not working

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! :slight_smile:

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

you more typically see something like this:

uint32_t ts = millis();
while(millis() - ts > TIMEOUT_PERIOD)
{
  doSomething();
}

BulldogLowell:
you more typically see something like this:

uint32_t ts = millis();

while(millis() - ts > TIMEOUT_PERIOD)
{
  doSomething();
}

Allright, here is my modified code:

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

Now, the program does not timeout at all.

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

stupid-questions:
Now, the program does not timeout at all.

this confuses the crap out of me:

while (1 < 2) {

what is it doing there? wont that always be true?

Robin2:
I don’t like the idea of using a WHILE or a FOR for a timeout as they block everything else.

sometimes that is desirable…

for (timeout; timeout > 200000; ttimeout is an int.

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.

BulldogLowell:
this confuses the crap out of me:

while (1 < 2) {

what is it doing there? wont that always be true?

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. :confused:

stupid-questions:
Yes, that's what it does. I thought I needed a while loop to loop the program.

Not when the loop() function is provided to do that for you. The name is kind of a dead giveaway.

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

What do you think?

You did not correct problem #3 at all.

Jiggy-Ninja:
You did not correct problem #3 at all.

How should I correct it? i’m not quite clear on what I need to do to fix #3

Perhaps I should change the middle condition to “timer < 32767” (Or some other number lower than 32767)

How should I correct it? i’m not quite clear on what I need to do to fix #3

Turn the > into a <

AWOL:
Turn the > into a <

OHHHH! I get it. If it were timer > 32767 the condition would always be false.

So you mean like this:

for (timer; timer < 32767; timer++)

No, not like that.
See reply #6