// Rules are:
//
// If the button is pressed "Switch LED1 ON for 2 seconds"
//
// If the button is Pressed again while LED1 is still on
// then "Switch LED2 ON for 2 seconds and keep LED1 ON as long as LED2 is on"
//
// If the button is Pressed again while LED-02 is on
// "Switch LED3 ON for 2 seconds and Keep LED1 and LED2 ON for as long as LED3 is on"
//
// So 2 seconds after the LAST button press - everything should reset to OFF
//
#include <Button_SL.hpp>
constexpr uint PINS_LED[] {17, 5, 18};
constexpr uint WAIT_MS {2000};
constexpr uint MAX_LED_COUNTER {sizeof(PINS_LED) / sizeof(PINS_LED[0])};
class Timer {
public:
void start() { timeStamp = millis(); }
bool operator()(const uint32_t duration) { return (millis() - timeStamp >= duration) ? true : false; }
private:
uint32_t timeStamp {0};
};
Btn::ButtonSL button {16};
Timer wait;
void setup() {
for (auto &pin : PINS_LED) { pinMode(pin, OUTPUT); }
button.begin();
Serial.begin(115200);
Serial.println("Start...");
}
void loop() {
static int ledCounter {0};
bool doLightLed {false};
if (button.tick() != Btn::ButtonState::notPressed && ledCounter < MAX_LED_COUNTER) {
doLightLed = true;
}
if (doLightLed) {
wait.start();
digitalWrite(PINS_LED[ledCounter++], HIGH);
}
if (wait(WAIT_MS) && ledCounter) {
for (uint i = 0; i < ledCounter; ++i) { digitalWrite(PINS_LED[i], LOW); }
ledCounter = 0;
}
}