mirror of
https://github.com/Crocmagnon/buzzer.git
synced 2024-11-24 17:08:04 +01:00
Fix debounce
This commit is contained in:
parent
828ca9742b
commit
8d2e275b2a
5 changed files with 23 additions and 10 deletions
|
@ -11,3 +11,5 @@ Audio audio;
|
|||
Preferences preferences;
|
||||
|
||||
long lastActionTime = 0;
|
||||
byte buttonLastState = HIGH;
|
||||
long startPress = NOT_PRESSED;
|
||||
|
|
|
@ -21,7 +21,8 @@
|
|||
#define BUTTON 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
|
||||
|
||||
// Screen
|
||||
|
@ -57,5 +58,7 @@ extern AsyncWebServer server;
|
|||
extern Audio audio;
|
||||
extern Preferences preferences;
|
||||
extern long lastActionTime;
|
||||
extern byte buttonLastState;
|
||||
extern long startPress;
|
||||
|
||||
#endif
|
10
src/main.cpp
10
src/main.cpp
|
@ -8,8 +8,6 @@
|
|||
#include "utils.h"
|
||||
#include "config.h"
|
||||
|
||||
byte buttonLastState = HIGH;
|
||||
long lastDebounceTime = 0;
|
||||
bool wasRunning = false;
|
||||
|
||||
void setup()
|
||||
|
@ -51,13 +49,7 @@ void setup()
|
|||
|
||||
void loop()
|
||||
{
|
||||
if ((millis() - lastDebounceTime) > DEBOUNCE_DELAY_MS)
|
||||
{
|
||||
byte buttonCurrentState = digitalRead(BUTTON);
|
||||
if (buttonCurrentState == LOW && buttonLastState == HIGH)
|
||||
play();
|
||||
buttonLastState = buttonCurrentState;
|
||||
}
|
||||
checkButtonAndPlay();
|
||||
|
||||
audio.loop();
|
||||
|
||||
|
|
|
@ -30,6 +30,21 @@ void displayText(String text)
|
|||
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()
|
||||
{
|
||||
updateLastAction();
|
||||
|
|
|
@ -9,6 +9,7 @@ bool fileIsValid(String fileName);
|
|||
bool fileExists(String fileName);
|
||||
void clearMessageArea();
|
||||
void displayText(String text);
|
||||
void checkButtonAndPlay();
|
||||
void play();
|
||||
|
||||
void selectFile(String fileName);
|
||||
|
|
Loading…
Reference in a new issue