Robot Timeout

Hello. I'm making a robot. I'm trying to find a way to make the robot get "bored" when it has been inactive for too long. In essence, a timeout. I can't use delay() for obvious reasons, and I'm wondering what the best way to do this is. I have an idea/theory that I haven't been able to test yet. It's something like this:

unsigned long currentMillis = millis();

if (currentMillis - previousMillis >= interval) {
  bored = true;
  previousMillis = currentMillis;
  interval = random(1000, 5000);
}
else{bored = false;}

}

Could that work
I want the time it takes before "timeout" to be different each time.

Thanks!
SQ

Wrong because the else clause will immediately reset the bored flag the next time through loop(). It will only be bored for a few microseconds.

Under what condition do you want it to stop being bored?

aarg:
Wrong because the else clause will immediately reset the bored flag the next time through loop(). It will only be bored for a few microseconds.

Under what condition do you want it to stop being bored?

Well the idea is this:

  1. It does something
  2. It finished doing that thing
  3. After a semi-random timeout time, it gets bored
  4. Once bored, it does a thing
    5.Now it's no longer bored

Does it do anything when it's bored?

So instead of setting bored back to false in the else, set another if to test for bored being true and in that do your thing and set bored back to false.

aarg:
Does it do anything when it's bored?

Yes. It gets bored, and while it's bored it does something (runs a subroutine kinda thing) then at the end of the subroutine it sets "bored" as false.

Delta_G:
So instead of setting bored back to false in the else, set another if to test for bored being true and in that do your thing and set bored back to false.

Like this?

unsigned long currentMillis = millis();

if (currentMillis - previousMillis >= interval) {
  bored = true;
  previousMillis = currentMillis;
  interval = random(1000, 5000);
}
else{

if (bored = true) {
bored = false;
}

}

}

No. That will also immediately reset the bored condition. You need to do it after you perform the boredom subroutine (which you haven't shown).

For the sake of this discussion, you could include it as something like boredActivity(). You don't have to specify what it does. But your code examples can't be verified without it. The last line of boredActivity() should reset the bored condition.

aarg:
No. That will also immediately reset the bored condition. You need to do it after you perform the boredom subroutine (which you haven't shown).

For the sake of this discussion, you could include it as something like boredActivity(). You don't have to specify what it does. But your code examples can't be verified without it. The last line of boredActivity() should reset the bored condition.

Like... this?

unsigned long currentMillis = millis();

if (currentMillis - previousMillis >= interval) {
  bored = true;
  previousMillis = currentMillis;
  interval = random(1000, 5000);

boredActivity();
bored = false;
}

}

Will the above code work? (bump... hehe)

stupid-questions:
Will the above code work? (bump... hehe)

That matches your user id.

Try it!

PaulS:
That matches your user id.

Try it!

Just tried it... there was no delay between "boredActivity"
:frowning: