/* Devil-Elec - µC.net IDE 1.8.19 avr-gcc: 7.5.0 & C++17 Arduino Mega 2560 18.05.2026 */ template class SimpleTimer { private: unsigned long lastMillis {0xEFFFFFFF}; // provoziert eine sofortige Auslösung nach uC Reset unsigned long duration {0}; Callback callback; public: SimpleTimer (unsigned long d, Callback cb): duration(d), callback(cb) {} void update() { if (millis() - lastMillis >= duration) { lastMillis = millis(); callback(); // direkt, ohne std::function } } void set (const unsigned long d) { duration = (0 class DipSwitch { private: uint8_t readNew {0}; uint8_t readOld {3}; bool state {false}; // Trigger für: es gibt neue Werte uint32_t lastMillis {0}; // private Methoden bool updateDebounce() { bool status {false}; const uint32_t ms {millis()}; if (ms - lastMillis >= 50) { lastMillis = ms; status = true; } return status; } public: // Konstruktor DipSwitch() = default; void init() { pinMode(pinA, INPUT_PULLUP); pinMode(pinB, INPUT_PULLUP); } bool update (void) { state = false; if (updateDebounce() ) { readNew = 0; if (digitalRead(pinA)) {readNew = 0x01; } if (digitalRead(pinB)) {readNew |= 0x02; } if (readNew != readOld) { state = true; } readOld = readNew; } return state; } uint8_t getSwitchValue (void) { return readOld; } }; template class AnalogRead { static_assert(1 <= FF && FF <= 255, "FF erlaubt 1 ... 255"); private: float filteredValue {0}; uint16_t newValue {0}; void readAnalog (void) { newValue = analogRead(pin); } /***************************************************************************************** ** Funktion Filtern() by GuntherB ** ****************************************************************************************** ** Bildet einen Tiefpassfilter (RC-Glied) nach. ** ** FF = Filterfaktor; Tau = FF / Aufruffrequenz ** ******************************************************************************************/ void filtern (void) { const uint16_t newValue = analogRead(pin); filteredValue = ((filteredValue * FF) + newValue) / (FF + 1); } public: // Konstruktor AnalogRead() = default; void init() { pinMode(pin, INPUT); } unsigned int getUpdate (void) { filtern(); return filteredValue; } }; DipSwitch <7, 8> dipSwitch; // Instanz AnalogRead poti; // Instanz SimpleTimer timerDipSwitch (50, []() { // Instanz, mit Parameter Aufruf aller 50ms, einfache Entprellung if (dipSwitch.update()) { Serial.print("change: "); Serial.println(dipSwitch.getSwitchValue()); } }); SimpleTimer timerPoti (100, []() { // Instanz, mit Parameter Aufruf aller 100ms poti.getUpdate(); }); SimpleTimer timerSerialPoti (1000, []() { // Instanz, mit Parameter Aufruf aller 1000ms Serial.print("Poti: "); Serial.println(poti.getUpdate()); }); void setup (void) { delay(1000); dipSwitch.init(); poti.init(); Serial.begin(9600); Serial.println("\nStart ####\n"); } void loop (void) { timerDipSwitch.update(); timerPoti.update(); timerSerialPoti.update(); }