mirror of
https://github.com/Crocmagnon/buzzer.git
synced 2024-11-21 23:48:07 +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;
|
Preferences preferences;
|
||||||
|
|
||||||
long lastActionTime = 0;
|
long lastActionTime = 0;
|
||||||
|
byte buttonLastState = HIGH;
|
||||||
|
long startPress = NOT_PRESSED;
|
||||||
|
|
|
@ -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
|
10
src/main.cpp
10
src/main.cpp
|
@ -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();
|
||||||
|
|
||||||
|
|
|
@ -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();
|
||||||
|
|
|
@ -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);
|
||||||
|
|
Loading…
Reference in a new issue