Using Millis() to delay an event after button is pressed

Hi all,

What I'm trying to do is the following:

When a button is pressed, a variable(Leds1) will be set to a number. After some time(1-2 secs), the variable will change to a new number. I have spent the greater part of the day trying to resolve this trying every possible iteration of Millis(), but I can't get it to work properly. I removed the rest of the code and simplified it as much as possible to make it easier. The trained eye will see straight away that what I got now prints only the first part of the code after the button is pressed. I really have no clue how to make it work, so any help will be valuable.

#include <ezButton.h>

#define DEBOUNCE_TIME 50 // the debounce time in millisecond, increase this time if it still chatters

ezButton button(4); // create ezButton object that attach to pin GIOP21

int period = 2000;
unsigned long startMillis;
int Leds1=20;

void setup() {

  Serial.begin(115200);

  pinMode(4, INPUT_PULLUP);    // initialize the pushbutton pin as an input:

  button.setDebounceTime(DEBOUNCE_TIME);

}

void loop() {


  button.loop(); // MUST call the loop() function first


  if (button.isPressed()) {
    Leds1 = 1000;
    Serial.print("Leds1 = "); Serial.println(Leds1);

    startMillis = millis();
    if (millis() - startMillis >= period)
    {
      Leds1 = 10;
      Serial.print("Leds1 = "); Serial.println(Leds1);
      
    }

  }

}
#include <ezButton.h>

#define DEBOUNCE_TIME 50 // the debounce time in millisecond, increase this time if it still chatters

ezButton button(4); // create ezButton object that attach to pin GIOP21

int period = 2000;
unsigned long startMillis;
int Leds1 = 20;

void setup() {

  Serial.begin(115200);

  pinMode(4, INPUT_PULLUP);    // initialize the pushbutton pin as an input:

  button.setDebounceTime(DEBOUNCE_TIME);

}
bool ItWasDone = false;

void loop() {


  button.loop(); // MUST call the loop() function first


  if (button.isPressed()) {
    Leds1 = 1000;
    Serial.print("Leds1 = "); Serial.println(Leds1);
    ItWasDone = true;
    startMillis = millis();
  }
  if (ItWasDone && ( millis() - startMillis >= peirod )
  {
  Leds1 = 10;
  Serial.print("Leds1 = "); Serial.println(Leds1);
    ItWasDone = false;
  }

}

Might be of help or not.

1 Like

Thanks so much for the fast reply, it works! I will go through it and understand what I was missing before.

1 Like

Let me know what you figure.

Well, I see that 2 conditions have to be met in the second statement: the first has to be concluded, and the time has to be right.
What I have trouble understanding is the following: Shouldn't time by itself be enough of a condition? The counter starts together with the first statement, why should it need the extra verification?

You have 2 small typos in your code btw, maybe you want to correct them:

if (ItWasDone && ( millis() - startMillis >= peirod )

Should be

if (ItWasDone && ( millis() - startMillis >= period ))

It is if you want the new value to be printed continuously after the end of the period

Expanding on post#6

Notice the If statement

if (ItWasDone && ( millis - StartMillis >= period ))

Examine this if if ( millis - StartMillis >= period ). Once the millis() timeout has been reached the >= will cause the math to always produce a true. You could go with a === instead but there is a risk.

You could also do this instead of using a boolean

#include <ezButton.h>

#define DEBOUNCE_TIME 50 // the debounce time in millisecond, increase this time if it still chatters

ezButton button(4); // create ezButton object that attach to pin GIOP21

int period = 2000;
unsigned long startMillis;
int Leds1 = 20;

void setup()
{
  Serial.begin(115200);
  pinMode(4, INPUT_PULLUP);    // initialize the pushbutton pin as an input:
  button.setDebounceTime(DEBOUNCE_TIME);
}

void loop()
{
  button.loop(); // MUST call the loop() function first
  if (button.isPressed())
  {
    Leds1 = 1000;
    Serial.print("Leds1 = ");
    Serial.println(Leds1);
    startMillis = millis();
  }
  
  if ((startMillis > 0) && ( millis() - startMillis >= period ))
  {
    Leds1 = 10;
    Serial.print("Leds1 = ");
    Serial.println(Leds1);
    startMillis = 0;
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.