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;
}
}
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 ))
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.
#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;
}
}