/*
https://forum.arduino.cc/t/arduino-nano-taster-mit-mehreren-funktionen/1291196/4
2024-09-01 by noiasca
code in thread
*/
#include <OneButton.h>
// as simple helper class to toggle pins
class Output {
protected:
const uint8_t pin; // pins are considered to be HIGH active
public:
Output(uint8_t pin) : pin(pin) {}
void begin() {
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
}
void off() {
digitalWrite(pin, LOW);
}
void on() {
digitalWrite(pin, HIGH);
}
void toggle() {
digitalRead(pin) == LOW ? digitalWrite(pin, HIGH) : digitalWrite(pin, LOW);
}
};
Output output[] {2, 3, 4, 5, 6, 7, 8, 9}; // create 8 outputs on specific pins
OneButton button[6]; // create 6 buttons, pins and modes will be set in setup
// the callbacks
void click0() {
output[0].toggle();
}
void click1() {
output[1].toggle();
}
void click2() {
output[2].toggle();
}
void click3() {
output[3].toggle();
}
void click4() { // kurz klicken: ersten 4 Relais aus
for (int i = 0; i < 4; i++)
output[i].off();
}
void long4() { // langes Drücken ersten 4 Relais an
for (int i = 0; i < 4; i++)
output[i].on();
}
void double4() { // mehrfaches klicken: zwei weitere Relais an / aus
output[4].toggle();
output[5].toggle();
}
void click5() { // kurz klicken: alle Relais aus
for (int i = 0; i < 8; i++)
output[i].off();
}
void long5() { // langes Drücken alle Relais an
for (int i = 0; i < 8; i++)
output[i].on();
}
void double5() { // mehrfaches klicken: letzten beiden Relais & Relais an / aus
output[6].toggle();
output[7].toggle();
}
void setup() {
for (auto &i : output) i.begin();
button[0].setup(A0, INPUT_PULLUP, true);
button[1].setup(A1, INPUT_PULLUP, true);
button[2].setup(A2, INPUT_PULLUP, true);
button[3].setup(A3, INPUT_PULLUP, true);
button[4].setup(A4, INPUT_PULLUP, true);
button[5].setup(A5, INPUT_PULLUP, true);
button[0].attachClick(click0);
button[1].attachClick(click1);
button[2].attachClick(click2);
button[3].attachClick(click3);
button[4].attachClick(click4);
button[4].attachLongPressStop(long4);
button[4].attachDoubleClick(double4);
button[5].attachClick(click5);
button[5].attachLongPressStop(long5);
button[5].attachDoubleClick(double5);
}
void loop() {
for (auto &i : button) i.tick();
}
//