#include "ExponentialFilter.h"
#define BUTTON_PIN A0
#define MIN_INPUT 0.0
#define MAX_INPUT 1.0
const float lowerThreshold = 0.293 * (MAX_INPUT - MIN_INPUT);
const float upperThreshold = 0.707 * (MAX_INPUT - MIN_INPUT);
trigger_t trigger {
lowerThreshold, upperThreshold, GTLT
};
ExponentialFilter expDebounce;
bool toggle;
void setup() {
Serial.begin(115200);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_BUILTIN, OUTPUT);
expDebounce.mode(AUTO); // Exponential Filter in Auto Mode
expDebounce.cutoffTime(MS2S(2000)); // Debounce Period = 20 milliseconds
expDebounce.init(1.0); // INPUT = 0.0, INPUT_PULLUP = 1.0
}
void loop() {
expDebounce.expFilter(digitalRead(BUTTON_PIN));
expDebounce.schmittTrigger(&trigger);
if (trigger.upperRising) toggle = !toggle;
digitalWrite(LED_BUILTIN, toggle);
Serial.println(
"Input:" + String(expDebounce.input, 3)
+ ", Output:" + String(expDebounce.output, 3)
+ ", Trigger:" + String(trigger.trigger, 3)
);
}