What is the best practice for push-button event tracking/handling without any hardware debouncing circuit
Posted: Thu May 30, 2019 2:37 pm
Hi ESP32:
I think ESP32 is really cool & powerful. Thanks to its dual-core hardware & RTOS(much important then the previous one) I successfully made really something I've been pround of and would like to use. (And may be later I print the circuit and sells it, because my project is really awesome & useful I think)
My humboldt stone in the project right now is something about "user interaction" -> How to deal with a push-button!
Right now I use the single push-button in this project for two different thing: one click: make a gps coordinate snapshot save to sd card, a long press button click for different display mode change. Everything works most of the time. But sometimes for unknown reason it just won't do anything or even worse in some rare chance it will even crash the whole RTOS.
This is the original POST describe how the project looks like. Here is the part that I read the button:
It can be found in this project's GitHub Repo
This is pretty what I've learned from the Arduino World. Because I do not have any hardware debouncing circuit, I have to "slow down" the read speed. Yeh, I know its kinda amateur. But this make me wanna ask, why the problem like above happens (push button long reaction time & system crash may happen), and is there still possible without hardware debouncing using GPIO's interrupt routine to handling push button debouncing, and how?
Thanks a lot!
Gfast2
I think ESP32 is really cool & powerful. Thanks to its dual-core hardware & RTOS(much important then the previous one) I successfully made really something I've been pround of and would like to use. (And may be later I print the circuit and sells it, because my project is really awesome & useful I think)
My humboldt stone in the project right now is something about "user interaction" -> How to deal with a push-button!
Right now I use the single push-button in this project for two different thing: one click: make a gps coordinate snapshot save to sd card, a long press button click for different display mode change. Everything works most of the time. But sometimes for unknown reason it just won't do anything or even worse in some rare chance it will even crash the whole RTOS.
This is the original POST describe how the project looks like. Here is the part that I read the button:
Code: Select all
int btn_old = 0;
int btn_now = 0;
uint32_t btn_long_press_old = 0;
// Flag marking if btn press a long press to preventing long press trigger a
// short btn press event at the same time.
int longPressFlag = false;
while(1) {
btn_now = gpio_get_level(BTN);
if(btn_now != btn_old){
if(btn_now == 1){ // btn pressed down
ESP_LOGI(TAG, "BTN get pressed down.");
btn_long_press_old = xTaskGetTickCount();
longPressFlag = false;
} else { // btn released
ESP_LOGI(TAG, "BTN get released.");
// When button get released in <1 second, do the gps coordinate snapshot
if(abs(xTaskGetTickCount()-btn_long_press_old) < 1000/portTICK_PERIOD_MS
&& !longPressFlag)
{
ESP_LOGI(TAG, "Triggered a snapshot");
snapshot_handler();
}
}
btn_old = btn_now;
btn_long_press_old = xTaskGetTickCount();
}
else if(btn_now == 1) { // Check if this is a long press btn event
// When the button get released after more then 1 second, then go to the next display mode.
if(abs(xTaskGetTickCount()-btn_long_press_old) > 1000/portTICK_PERIOD_MS){
ESP_LOGI(TAG, "long btn press get triggered");
mode_switch_handler();
btn_long_press_old=xTaskGetTickCount(); // Update timer preparing trigger the next long btn press event
longPressFlag = true;
}
}
else {
// ESP_LOGI(TAG, "Button stays unpressed.");
}
vTaskDelay(100 / portTICK_PERIOD_MS);
}
This is pretty what I've learned from the Arduino World. Because I do not have any hardware debouncing circuit, I have to "slow down" the read speed. Yeh, I know its kinda amateur. But this make me wanna ask, why the problem like above happens (push button long reaction time & system crash may happen), and is there still possible without hardware debouncing using GPIO's interrupt routine to handling push button debouncing, and how?
Thanks a lot!
Gfast2