ESP32 buttons and bouncing
ESP32 buttons and bouncing
Rigged up button to GPIO 21 to explore, button presses, debouncing and interrupts. Following worked.
/*
* This ESP32 code is created by esp32io.com Worked 7/11/24
*
* This ESP32 code is released in the public domain
*
* For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-button-debounce
*/
#include <ezButton.h>
#define DEBOUNCE_TIME 50 // the debounce time in millisecond, increase this time if it still chatters
ezButton button(21); // create ezButton object that attach to pin GPIO21
void setup() {
Serial.begin(9600);
button.setDebounceTime(DEBOUNCE_TIME); // set debounce time to 50 milliseconds
}
void loop() {
button.loop(); // MUST call the loop() function first
if (button.isPressed())
Serial.println("The button is pressed");
if (button.isReleased())
Serial.println("The button is released");
}
----------------------------------------------------Next: interrupts-----#define BUTTON_PIN 21 // The ESP32 pin GPIO21 connected to the button Worked 7/11/24
volatile bool buttonPressed = false; // use volatile for variable that is accessed from inside and outside ISR
void IRAM_ATTR handleButtonPress();
void setup() {
Serial.begin(9600);
// Initialize the GPIO pin as an input
pinMode(BUTTON_PIN, INPUT_PULLUP);
// Attach interrupt to the GPIO pin
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), handleButtonPress, FALLING);
}
void loop() {
// Main code here
if (buttonPressed) {
Serial.println("Button pressed!");
buttonPressed = false; // Reset the flag
}
// Other code here
}
// Interrupt Service Routine (ISR)
void IRAM_ATTR handleButtonPress() {
// Serial.println("Button pressed!"); // do NOT use function that takes long time to execute
buttonPressed = true;
}
------changed below button GPIO pin from 19 to 21. (RTC wants 19)---------
---------mmm how many GPIO pins can I use like this mmm----------
/*
* This ESP32 code is created by esp32io.com Worked 7/11/24
*
* This ESP32 code is released in the public domain
*
* For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-button-debounce
*/
#include <ezButton.h>
#define DEBOUNCE_TIME 50 // the debounce time in millisecond, increase this time if it still chatters
ezButton button(19); // create ezButton object that attach to pin GPIO21
void setup() {
Serial.begin(9600);
button.setDebounceTime(DEBOUNCE_TIME); // set debounce time to 50 milliseconds
}
void loop() {
button.loop(); // MUST call the loop() function first
if (button.isPressed())
Serial.println("The button is pressedd");
if (button.isReleased())
Serial.println("The button is released");
}
Comments
Post a Comment