I was looking for a pushbutton debouncer when I came across ezButton.h, seemed like just what I needed. When things didn't work as expected, I went back to the "singlebuttondebounce" example. It worked as I thought it would. Press the button, it prints "The button is pressed" then prints "The button is released". I then added the "delay(500)" line and instead of printing "The button is pressed" when I pressed it, it printed "The button is pressed twice with a half second delay between the prints then printed "The button is released. I don't understand why adding a simple delay command causes it to print twice. The code follows:
/*
Created by ArduinoGetStarted.com
This example code is in the public domain
Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-library
This example reads the state of a button with debounce and print it to Serial Monitor.
*/
#include <ezButton.h>
ezButton button(2); // create ezButton object that attach to pin 7;
void setup() {
Serial.begin(9600);
button.setDebounceTime(50); // set debounce time to 50 milliseconds
}
void loop() {
button.loop(); // MUST call the loop() function first
if (button.isPressed())
{ // I added this brace
Serial.println("The button is pressed");
delay(500); // I added this line to the code
} // I added this brace
if (button.isReleased())
Serial.println("The button is released");
}
It is printing "pressed" twice because the delay() function suspends all other program activity, including the ezButton library. So, after the delay(500), the library thinks you have pressed the button again.
I use this library, but button2.h is a bit more versatile.
If you must use a delay() in the loop function, then you should use millis() instead. This link should get you started.
But, why? For that to happen, the code would have to actively observe a release first. At the end of the 500ms, it should observe a release, but not any subsequent press.
The delay was an arbitrary line of code. The actual program I am working on actuates an animated fire station on an O gage train layout in a children’s museum where I volunteer. The program is required to actuate a relay momentarily (I use 500ms on time), wait 40 seconds while the fire station opens, actuates the relay for another 500ms then waits for 45 seconds while the fire station closes then waits for another button press to do it all over again.
I haven’t looked at the library code; I was hoping someone here had. I am glad there is some confusion, it means I’m not crazy.
I will look at button2.h as an alternative and look at millis(), thanks for the link.
i used the myDelay function as described in the Arduino Cookbook page 401 which uses the millis() function with the same result, it prints :The button is pressed" twice for eash actual press.
/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-library
*
* This example reads the state of a button with debounce and print it to Serial Monitor.
*/
#include <ezButton.h>
ezButton button(2); // create ezButton object that attach to pin 7;
void setup() {
Serial.begin(9600);
button.setDebounceTime(50); // set debounce time to 50 milliseconds
}
void myDelay(unsigned long duration)
{
unsigned long start = millis();
while (millis() - start <= duration);
}
void loop() {
button.loop(); // MUST call the loop() function first
if(button.isPressed())
{
Serial.println("The button is pressed");
myDelay(500);
}
if(button.isReleased())
Serial.println("The button is released");
}
That doesn't really fix the problem since the real problem is that the button state changes during the delay() which the library does not see so gets out of sync with reality. Calling button.loop() inside myDelay() doesn't fix it either since the code now sees all the transitions, but doesn't call the isPressed/isReleased functions so nothing gets printed.
Bottom line: Don't use delay() in your code if you need to be polling buttons.