#include "NonBlockingDelay.hpp"
#define PUSH_BTN1 2
#define PUSH_BTN2 3
NonBlockingDelay nbDelay;
// Bool variable to be setted
bool myBoolVar = false;
// Callback function to be called.
void toggle() {
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
Serial.print(millis());
Serial.println(" - output toggled");
}
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(PUSH_BTN1, INPUT_PULLUP);
pinMode(PUSH_BTN2, INPUT_PULLUP);
Serial.begin(115200);
Serial.println();
}
void loop() {
// Store button press time for NON blocking pushbutton debounce
static uint32_t btnRedTime = millis();
if (digitalRead(PUSH_BTN1) == LOW && millis() - btnRedTime > 200) {
btnRedTime = millis();
Serial.print(millis());
Serial.println(" - red button is pressed");
// Set actual delay passing a callback function
nbDelay.SetTimeout(1000, toggle);
}
// Store button press time for NON blocking pushbutton debounce
static uint32_t btnGreenTime = millis();
if (digitalRead(PUSH_BTN2) == LOW && millis() - btnGreenTime > 200) {
btnGreenTime = millis();
Serial.print(millis());
Serial.println(" - green button is pressed");
// Set actual delay passing a bool variable
nbDelay.SetTimeout(2000, myBoolVar);
}
// Check the bool variable and wait for true
if (myBoolVar) {
myBoolVar = false;
Serial.print(millis() - btnGreenTime);
Serial.println(" milliseconds since green button was pressed");
}
nbDelay.run();
}