/*
https://forum.arduino.cc/t/c-ich-habe-nur-begrenzte-ahnung/1441024/8
Link in post
2026-04-22: noiasca
Variant: Flipflops with button
*/
#include "button.h"
class Flipflop {
private:
Button button; // the channel needs a button
uint8_t ledPin; // pin for the LED
public:
Flipflop (uint8_t ledPin, uint8_t buttonPin): ledPin{ledPin}, button(buttonPin) {};
void begin() {
pinMode(ledPin, OUTPUT);
button.begin();
}
void toggle() {
Serial.print(F("action on pin=")); Serial.println(ledPin);
if (digitalRead(ledPin) == LOW)
digitalWrite(ledPin, HIGH);
else
digitalWrite(ledPin, LOW);
}
void update() {
if (button.wasPressed()) toggle();
}
};
// create the Channel objects as array and hand over the pins
Flipflop flipflop [] {
{5, 2}, // ledPin, buttonPin
{6, 3},
{7, 4} // and so on ...
};
void setup() {
Serial.begin(115200);
Serial.println(F("FlipFlop with Button"));
for (auto & f : flipflop) f.begin(); // initialize hardware for each channel
}
void loop() {
for (auto & f : flipflop) f.update(); // trigger each channel
}
//toggle