Added Debouncer

This commit is contained in:
Dirk Jahnke 2021-02-02 16:10:58 +01:00
parent d6b84bae70
commit bb86029452
2 changed files with 19 additions and 6 deletions

View File

@ -16,3 +16,4 @@ lib_deps =
bblanchon/ArduinoJson@^6.17.2
knolleary/PubSubClient@^2.8
paulstoffregen/Ethernet@0.0.0-alpha+sha.9f41e8231b
Bounce2

View File

@ -32,6 +32,7 @@
#include <Ethernet.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include <Bounce2.h>
// Update these with values suitable for your network.
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xEA };
@ -62,6 +63,12 @@ PubSubClient mqttClient(ethClient);
#define LIGHT_REQUEST_BUTTON_PIN 8
#define POSTBOX_FLAP_PIN 7
#define POSTBOX_DOOR_PIN 6
// Debounce intervals (ms)
#define DOOR_BELL_DEBOUNCE_ms 200
#define LIGHT_REQUEST_DEBOUNCE_ms 200
#define POSTBOX_FLAP_DEBOUNCE_ms 200
#define POSTBOX_DOOR_DEBOUNCE_ms 500
// Output pins
#define LED_PIN 13
#define DOOR_BELL_BUZZER_PIN 14
@ -76,11 +83,13 @@ struct {
bool isTriggered;
const char *topic; // sends changes: trigger / re-trigger, / release
const uint8_t triggerOutputPin;
Bounce debouncer;
uint16_t debounceInterval_ms;
} myInputs[] = {
{ DOOR_BELL_BUTTON_PIN, 500, 0 , false, doorBellTopic, DOOR_BELL_BUZZER_PIN },
{ LIGHT_REQUEST_BUTTON_PIN, 500, 0 , false, doorLightRequestTopic, POSTBOX_LIGHT_PIN },
{ POSTBOX_FLAP_PIN, 1000, 0, false, postboxFlapTopic, OUTPUT_NONE },
{ POSTBOX_DOOR_PIN, 1000, 0, false, postboxDoorTopic, OUTPUT_NONE }
{ DOOR_BELL_BUTTON_PIN, 500, 0 , false, doorBellTopic, DOOR_BELL_BUZZER_PIN, Bounce(), DOOR_BELL_DEBOUNCE_ms },
{ LIGHT_REQUEST_BUTTON_PIN, 500, 0 , false, doorLightRequestTopic, POSTBOX_LIGHT_PIN, Bounce(), LIGHT_REQUEST_DEBOUNCE_ms },
{ POSTBOX_FLAP_PIN, 1000, 0, false, postboxFlapTopic, OUTPUT_NONE, Bounce(), POSTBOX_FLAP_DEBOUNCE_ms },
{ POSTBOX_DOOR_PIN, 1000, 0, false, postboxDoorTopic, OUTPUT_NONE, Bounce(), POSTBOX_DOOR_DEBOUNCE_ms }
};
#define NUM_INPUTS (sizeof(myInputs) / sizeof(myInputs[0]))
int ledState = LOW;
@ -247,6 +256,8 @@ void setup()
// setup the input and output pins
for (unsigned int i = 0; i < NUM_INPUTS; i++) {
pinMode(myInputs[i].pin, INPUT_PULLUP);
myInputs[i].debouncer.attach(myInputs[i].pin);
myInputs[i].debouncer.interval(myInputs[i].debounceInterval_ms);
}
// Setup the output pins :
@ -285,8 +296,10 @@ void checkInputSignals() {
for (unsigned int i = 0; i < NUM_INPUTS; i++) {
myInputs[i].debouncer.update();
if (millis()-myInputs[i].lastTrigger_ts > myInputs[i].quietAfterTriggerFor_ms) {
if (!digitalRead(myInputs[i].pin)) {
int readValue = myInputs[i].debouncer.read();
if (!readValue) {
// input is triggered, compare with existing state
if (myInputs[i].isTriggered) {
// was triggered as well, could send a retrigger now
@ -392,4 +405,3 @@ void loop() {
checkOutputAutoOff();
}