/* Arduino IDE 1.8.19 ATmega2560 01.02.2026 */ const byte pinLed {28}; // Pinnummer byte countTrigger; constexpr byte countMax {6}; // Limit wann Zwangs-Aus constexpr unsigned long timeStep {1000}; unsigned long timeout; unsigned long lastMillis; enum class Funktion : uint8_t {Taster, Schalter}; template class Kontakt { private: bool pushed {false}; bool oldPress {true}; unsigned long lastDebounce {0}; bool timer (void) { const unsigned long ms {millis()}; bool state {false}; if (ms - lastDebounce >= debounce) { lastDebounce = ms; state = true; } return state; } void updateNoRetrigger() { pushed = false; if (timer() ) { const bool read = !digitalRead(pin); if (read && !oldPress ) { pushed = true; } oldPress = read; } } void updateSwitch() { if (timer() ) { const bool read = !digitalRead(pin); if (read && (!oldPress)) { pushed = !pushed; } oldPress = read; } } public: Kontakt() = default; void init() { pinMode(pin, INPUT_PULLUP); } bool update() { switch(mode) { // Rückgabewertlogik ist: gedrückt "true", nicht gedrückt "false" case Funktion::Taster: updateNoRetrigger(); break; case Funktion::Schalter: updateSwitch(); break; } return pushed; } }; Kontakt <6, 50, Funktion::Taster> taster; // Pinnummer, Entprellzeit, Funktion void setup(void) { Serial.begin(250000); pinMode(pinLed, OUTPUT); taster.init(); } void loop(void) { const bool trigger = taster.update(); if (trigger) { if (0 == countTrigger) { lastMillis = millis(); digitalWrite(pinLed, HIGH); } countTrigger++; timeout = timeout + timeStep; Serial.print(countTrigger); Serial.print(" "); Serial.println(timeout); } if (countMax <= countTrigger) { countTrigger = 0; timeout = 0; Serial.println("- 0 -"); } if (millis() - lastMillis >= timeout) { countTrigger = 0; timeout = 0; digitalWrite(pinLed, LOW); } }