Fix debounce

This commit is contained in:
Gabriel Augendre 2023-03-20 10:17:22 +01:00
parent 828ca9742b
commit 8d2e275b2a
5 changed files with 23 additions and 10 deletions

View file

@ -11,3 +11,5 @@ Audio audio;
Preferences preferences; Preferences preferences;
long lastActionTime = 0; long lastActionTime = 0;
byte buttonLastState = HIGH;
long startPress = NOT_PRESSED;

View file

@ -21,7 +21,8 @@
#define BUTTON 33 #define BUTTON 33
#define DEEP_SLEEP_WAKEUP GPIO_NUM_33 #define DEEP_SLEEP_WAKEUP GPIO_NUM_33
#define DEBOUNCE_DELAY_MS 1000 #define MIN_PRESS_DURATION 500
#define NOT_PRESSED -1
#define DEEP_SLEEP_DELAY_MS 3600000 // 1h #define DEEP_SLEEP_DELAY_MS 3600000 // 1h
// Screen // Screen
@ -57,5 +58,7 @@ extern AsyncWebServer server;
extern Audio audio; extern Audio audio;
extern Preferences preferences; extern Preferences preferences;
extern long lastActionTime; extern long lastActionTime;
extern byte buttonLastState;
extern long startPress;
#endif #endif

View file

@ -8,8 +8,6 @@
#include "utils.h" #include "utils.h"
#include "config.h" #include "config.h"
byte buttonLastState = HIGH;
long lastDebounceTime = 0;
bool wasRunning = false; bool wasRunning = false;
void setup() void setup()
@ -51,13 +49,7 @@ void setup()
void loop() void loop()
{ {
if ((millis() - lastDebounceTime) > DEBOUNCE_DELAY_MS) checkButtonAndPlay();
{
byte buttonCurrentState = digitalRead(BUTTON);
if (buttonCurrentState == LOW && buttonLastState == HIGH)
play();
buttonLastState = buttonCurrentState;
}
audio.loop(); audio.loop();

View file

@ -30,6 +30,21 @@ void displayText(String text)
display.display(); display.display();
} }
void checkButtonAndPlay() {
byte buttonCurrentState = digitalRead(BUTTON);
if (buttonCurrentState == HIGH && buttonLastState == LOW) {
startPress = millis();
} else if (buttonCurrentState == LOW && buttonLastState == HIGH) {
startPress = NOT_PRESSED;
}
long pressDuration = millis() - startPress;
if (startPress != NOT_PRESSED && pressDuration >= MIN_PRESS_DURATION) {
play();
startPress = NOT_PRESSED;
}
buttonLastState = buttonCurrentState;
}
void play() void play()
{ {
updateLastAction(); updateLastAction();

View file

@ -9,6 +9,7 @@ bool fileIsValid(String fileName);
bool fileExists(String fileName); bool fileExists(String fileName);
void clearMessageArea(); void clearMessageArea();
void displayText(String text); void displayText(String text);
void checkButtonAndPlay();
void play(); void play();
void selectFile(String fileName); void selectFile(String fileName);