Hello, may someone, please help with this code? Everytime I press the button the Arduino Mega 2560 sometimes reads it as HIGH to LOW, but most the time the button is HIGH. The hardware is below (so is the code). I basically want to toggle the button, like a switch, while printing on or off in the Serial Monitor.
#include <Bounce2.h>
#define button 36
Bounce debouncer = Bounce(); // Instantiate a Bounce object
void setup() {
// put your setup code here, to run once:
debouncer.attach(button,INPUT_PULLUP); // Attach the debouncer to a pin with INPUT_PULLUP mode
Serial.begin(9600);
debouncer.interval(25); // Use a debounce interval of 25 milliseconds
}
void loop() {
// put your main code here, to run repeatedly:
buttonState();
debouncer.update(); // Update the Bounce instance
}
void buttonState() {
if (debouncer.fell()) {
Serial.println("ON!");
}
else{
delay(1000);
Serial.println("OFF!");
}
}
if ( debouncer.fell() ) { // Call code if button transitions from HIGH to LOW
This tells that a HIGH to LOW transition has occured. At the next call this function will return a false value as I understand.
As I think…...When You have one level of logic You need to test for the other level occuring. You need to remember in what state You have and use the proper logic.
Is there a function like debouncer.raise for transistions from LOW to HIGH?
I am looking through the files (I found .cpp and .h) and I do not see any debouncer.raise. "debouncer" doesn't even exist. It's all debounce. Even in the examples. I'll send a the folder to you so you can see. There is also another page on the forum for this subject.