/*
https://forum.arduino.cc/t/c-ich-habe-nur-begrenzte-ahnung/1441024/8
Link in post
2026-04-22: noiasca
Variant: Flipflops and Monoflops 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
{7, 4} // and so on ...
};
class Monoflop {
private:
Button button; // the channel needs a button
uint8_t ledPin; // pin for the LED
uint32_t previousMillis = 0; // for time management
public:
Monoflop (uint8_t ledPin, uint8_t buttonPin): ledPin{ledPin}, button(buttonPin) {};
void begin() {
pinMode(ledPin, OUTPUT);
button.begin();
}
void trigger() {
Serial.print(F("action on pin=")); Serial.println(ledPin);
previousMillis = millis(); // start timer
digitalWrite(ledPin, HIGH);
}
void update(uint32_t currentMillis = millis()) {
if (button.wasPressed()) trigger(); // check button
if ((currentMillis - previousMillis > 30 ) && digitalRead(ledPin) == HIGH) digitalWrite(ledPin, LOW); // check if time is over
}
};
// create the Channel objects as array and hand over the pins
Monoflop monoflop [] {
{6, 3}, // ledPin, buttonPin
{8, 9} // 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
for (auto & m : monoflop) m.begin();
}
void loop() {
for (auto & f : flipflop) f.update(); // trigger each channel
for (auto & m : monoflop) m.update();
}
//toggle
monoflop