#include <Button_SL.hpp> // https://github.com/DoImant/Button_SL
using namespace Btn;
//////////////////////////////////////////////////
// Global constants and variables
//////////////////////////////////////////////////
constexpr uint8_t buttonPin {4};
constexpr uint8_t relaisPins[] {5, 6, 7, 8}; // Relais pins
constexpr uint8_t relaisPinsRev[] {8, 7, 6, 5}; // The same pins in reverse order
constexpr unsigned long switchInterval_ms {500};
enum class State : uint8_t { off, on };
ButtonSL button {buttonPin};
//////////////////////////////////////////////////
// Functions
//////////////////////////////////////////////////
template <size_t N> void switchRelais(const uint8_t (&pins)[N], State state, unsigned long period) {
for (auto& pin : pins) {
digitalWrite(pin, static_cast<int>(state));
delay(period);
}
}
//////////////////////////////////////////////////
// Main Program
//////////////////////////////////////////////////
void setup() {
for (auto& relaisPin : relaisPins) {
pinMode(relaisPin, OUTPUT);
}
button.begin();
button.releaseOn(); // Releases button as soon as the time for a long button press has been reached.
}
void loop() {
static State lightState {State::off};
if (button.tick() != Btn::ButtonState::notPressed) {
lightState = (lightState == State::off) ? State::on : State::off;
(lightState == State::off) ? switchRelais(relaisPinsRev, lightState, switchInterval_ms)
: switchRelais(relaisPins, lightState, switchInterval_ms);
}
}
Zum Umschalten Taster drücken.