Hi !
I wish to get a serial output showing 1 when I press the start Button.
I am now receiving 1 and 1.
I discarded the infamous Delay() thinking the issue was coming from there but I am still getting this duplication.
I figured the IF still sees the button state being a 0, I tried isRelease or isPressed with same results.
Thanks in advance for your help!
#include <ezButton.h>
ezButton start(2);
const int DB_time = 100; // Debouncing time pour boutons
int Green = 0;
int period = 1000;
unsigned long time_now = 0;
void setup()
{
Serial.begin(9600);
start.setDebounceTime(DB_time); // debouncing time
}
void loop()
{
start.loop();
if (start.getState() == 0) // check if start button is ON
{
time_now = millis();
Green = 1;
Serial.println(Green);
while (millis() < time_now + period) //wait approx. [period] ms
{
}
}
else
{
Green = 0;
}
}
:o :o
"While" being infamous, ok now on my blacklist... if that is the cause I am thinking to add a timing condition into the IF command, the arduino loop would take care of the repeat.
That is easy here but could be cumbursome when many things happens at the same time. Is there a best practice to implement here?
Hello,
I am a creator of ezButton library.
the library has button.isPressed() function. you can use it without manipulating the millis() for deboucing. The denouncing function has built-in, You can take a look to this debounce example from library
In your code, you use "if (start.getState() == 0)". This returns the current state, if you press the button long enough, it is not only print "1" two times but also many times. It is obvious.
Also, if you ever want to make non blocking code, here is a header file to make timer objects I created to set an "alarm" for (x) amount of seconds and you can use .done(), which returns a true if done and a false if not done. I also have a stopWatch method to see how much time has passed since the timer (.start())ed.
class MillisAlarm {
private:
unsigned long interval;
unsigned long startTime;
public:
void start(unsigned long timerLength) {
interval = timerLength;
startTime = millis();
}
bool done() {
return (millis()-startTime >= interval);
}
};
class MillisStopWatch {
private:
unsigned long startTime;
public:
void start() {
startTime = millis();
}
unsigned long check() { return millis()-startTime; }
};
Here is how to use it:
//include the code from above here or put it in an .h file if you wish
//uncomment if you download my .h file or copy the above code
//#include "MillisTimers.h"
MillisAlarm alarm;
MillisStopWatch stopWatch;
//these create objects of the MillisAlarm and MillisStopWatch classes
//called alarm and stopWatch. You can create as many alarms and
//stop watches as you like, just replace the alarm and stopWatch
//names to make more!
//I like to use the MillisAlarm to wait a certain amount of time without
//blocking other things from running and I like to use MillisStopWatch
//to see how long it takes for a big chunk of code to run on a more human
//speed scale than micros... of which I can see how long a small chunk of
//code takes to run
void setup() {
//start an alarm that will end in 1 second
alarm.start(1000);
//start a stopWatch, timer starting @ current millis();
stopWatch.start();
Serial.begin(115200);
while (!Serial);
}
void loop() {
// put your main code here, to run repeatedly:
if (alarm.done()) {
Serial.print("Alarm went off in ");
Serial.print(stopWatch.check());
Serial.println(" milliseconds!");
//set another alarm for 1 second
alarm.start(1000);
//restart the stopWatch
stopWatch.start();
}
//now you can do other things here while waiting for the
//alarm to go off since the condition skips the code
//while the alarm hasn't gone off
//makes life easier
}