Hi all, so I have a T-Beam. I'm trying to implement D-bounce on the button press, so that when the button is pressed there is a debounce for however long. Currently I have it working so that when the button is pressed, it will print but due to the noise from the button it prints several hundred times. I have put the debounce code in, however it doesn't work as intended and i'm not sure why.
The second line does nothing because it is not part of the "if" statement. It should be "if the the state of the button is high and current millis minus last millis is greater than debounce millis then...", and it look like this:
Here is some sample code I created that does debounce and increments a counter. Try it, play with it, adapt it to what you need.
/* Simple button debounce for 4 buttons. Increments a count and sends the updated count to the serial monitor once per button press */
/* Tested on an Uno */
/* Connect simple push to make buttons between 0V and pin 2, 0V and pin 3, 0V and pin 4 and between 0V and pin 5 */
#define noOfButtons 4 //Exactly what it says; must be the same as the number of elements in buttonPins
#define bounceDelay 20 //Minimum delay before regarding a button as being pressed and debounced
#define minButtonPress 3 //Number of times the button has to be detected as pressed before the press is considered to be valid
const int buttonPins[] = {2, 3, 4, 5}; // Input pins to use, connect buttons between these pins and 0V
uint32_t previousMillis[noOfButtons]; // Timers to time out bounce duration for each button
uint8_t pressCount[noOfButtons]; // Counts the number of times the button is detected as pressed, when this count reaches minButtonPress button is regared as debounced
uint8_t testCount[noOfButtons]; //Test count, incremented once per button press
void setup() {
uint8_t i;
uint32_t baudrate = 115200;
Serial.begin(baudrate);
Serial.println("");
Serial.print("Serial port connected: ");
Serial.println(baudrate);
for (i = 0; i < noOfButtons; ++i) {
pinMode(buttonPins[i], INPUT_PULLUP);
Serial.print("Testcount ");
Serial.print(i);
Serial.print(" = ");
Serial.println(testCount[i]);
}
}
void loop() {
debounce();
delay(10); //Your other code goes here instead of this delay. DO NOT leave this delay here, it's ONLY for demonstration.
}
void debounce() {
uint8_t i;
uint32_t currentMillis = millis();
for (i = 0; i < noOfButtons; ++i) {
if (digitalRead(buttonPins[i])) { //Input is high, button not pressed or in the middle of bouncing and happens to be high
previousMillis[i] = currentMillis; //Set previousMillis to millis to reset timeout
pressCount[i] = 0; //Set the number of times the button has been detected as pressed to 0
}
else {
if (currentMillis - previousMillis[i] > bounceDelay) {
previousMillis[i] = currentMillis; //Set previousMillis to millis to reset timeout
++pressCount[i];
if (pressCount[i] == minButtonPress) {
++testCount[i];
Serial.print("Button ");
Serial.print(i);
Serial.print(" testcount = ");
Serial.println (testCount[i]);
}
}
}
}
}
#include <ezButton.h>
ezButton button(7); // create Button object that attach to pin 7;
void setup() {
Serial.begin(9600);
button.setDebounceTime(100); // set debounce time to 100 milliseconds
}
void loop() {
button.loop(); // MUST call the loop() function first
int btnState = button.getState();
Serial.println(btnState);
}