Millis timer question(s)

Hey everyone,

messy code I’m sure but if say “DownButton” is pressed while the millis has already started counting, because it’s already inside the If statement, will it cancel or just ignore the input and continue onto turning off the backlight?

Thanks :slight_smile:

///////////////////////////////////////////////////////////
if(DownButton == LOW){
  if(UpButton == LOW){
    if(RightButton == LOW){
      if(LeftButton == LOW){
          if(millis() > time_now + timeout){
                  time_now = millis();
                  //Serial.println("Hello");                //Check if any activity, if none, start a 1 minute timer to turn off backlight.
                          lcd.noBacklight();

        }
      }
    }
  }
}
///////////////////////////////////////////////////////////

Sorry, but I can't make sense of your question.

Please post a complete program.

if(millis() > time_now + timeout) will not execute when the previous ifs are false.

Learn how to use &&

Sorry I’m bad at wording questions.

For example, if all my buttons are low, the IF can run because its requirements are met.

what then happens if I press a button and set it high? Does the IF stop and the milli timer stop. Or does it continue to run as the IF requirements are already met?

int UpButton = digitalRead(22);
int DownButton = digitalRead(23);
int LeftButton = digitalRead(24);
int RightButton = digitalRead(25);
const long timeout = 60000; //1 minute timer
int long time_now = 0;


if(DownButton == LOW){
  if(UpButton == LOW){
    if(RightButton == LOW){
      if(LeftButton == LOW){
          if(millis() > time_now + timeout){
                  time_now = millis();
                  //Serial.println("Hello");                //Check if any activity, if none, start a 1 minute timer to turn off backlight.
                          lcd.noBacklight();

        }
      }
    }
  }
}

what then happens if I press a button and set it high? Does the IF stop and the milli timer stop. Or does it continue to run as the IF requirements are already met?

When an input goes HIGH, if(millis() > time_now + timeout) will not execute.
However, millis will still continue to increment every one millisecond.

Don't use addition when working with millis() times, use subtraction. This construct will fail if the addition causes an unsigned long rollover:

if(millis() > time_now + timeout){

This will continue to work even if millis() rolls over after time_now was set:

if(millis() - time_now > timeout){

if(DownButton == HIGH)...can be written as...if(DownButton)

if(DownButton == LOW)...can be written as...if(!DownButton)
Note the exclamation mark.

All those separate if() startements can be inside one big if() statement, glued together with AND (&&).

if(!DownButton && !UpButton && !RightButton && !LeftButton && millis() - time_now >= timeout) {
// code here
}

Conditions are tested in that order. If one is not true, then the rest is not tested.
Leo..

gfvalvo:
Don't use addition when working with millis() times, use subtraction.

Missed it by one character...

if(millis() - time_now >= timeout){

As the code in reply #3 won't compile I wonder if there are other problems lurking in the unposted code.

Wawa:
if(DownButton == HIGH)...can be written as...if(DownButton)

if(DownButton == LOW)...can be written as...if(!DownButton)
Note the exclamation mark.

All those separate if() startements can be inside one big if() statement, glued together with AND (&&).

if(!DownButton && !UpButton && !RightButton && !LeftButton && millis() - time_now >= timeout) {
// code here
}

Conditions are tested in that order. If one is not true, then the rest is not tested.
Leo..

that's pretty cool, I didn't know that. Thanks