/* IDE 1.8.19 auf Arduino Mega2560 getestet für Arduino Leonardo ATmega32U4 23.04.2023 https://www.mikrocontroller.net/topic/553491#7395414 */ // Auf ATmega32U4 ist PB6 Arduino Pin 10 // const uint8_t pinPulsOut {12}; // OC1B bzw. PB6 invertiert const uint8_t ANZAHLTASTER {4}; const uint8_t ANZAHLPULSFORM {ANZAHLTASTER+1}; // wegen Default Dummy Pulsform const bool DEBUG {false}; const bool DEBUGTASTER {true}; const byte pinTaster [ANZAHLTASTER] {2, 3, 4, 5}; struct TasterDaten { uint8_t alt {ANZAHLTASTER}; // Default keine Taste gedrückt uint8_t neu {ANZAHLTASTER}; // }; TasterDaten taster; struct TimerDaten { uint16_t duty {0}; uint16_t periode {0}; uint16_t prescaler {0}; }; TimerDaten pulsForm [ANZAHLPULSFORM] { { 16, 1600, 1}, // 2us off, 198us on { 320, 1600, 1}, // 40us off, 160us on { 125, 12500, 64}, // 1ms off, 99ms on { 1600, 1600, 64}, // Signal Low { 0, 1600, 64}, // Dummy, kein Taster gedrückt Signal High }; enum T1Status {CHECK, ZERO, WAIT, CONFIG, START}; T1Status timerStatus {CHECK}; void setup (void) { Serial.begin(250000); Serial.println(F("\nuC Reset ####\n")); digitalWrite(pinPulsOut, HIGH); // OC1B bzw. PB6 invertiert pinMode(pinPulsOut, OUTPUT); initTimer1(11); // Timer-Mode 11 changeTimer1(pulsForm[ANZAHLTASTER]); // Default, Dummy, kein Taster gedrückt startTimer1(pulsForm[ANZAHLTASTER]); // Default, Dummy, kein Taster gedrückt for (auto &p: pinTaster) { pinMode(p, INPUT_PULLUP); } } void loop (void) { checkActiveButton(pinTaster); stateMaschine(); } void stateMaschine (void) { switch(timerStatus) { case CHECK: if (taster.neu != taster.alt) { timerStatus = ZERO; if (DEBUGTASTER) { Serial.print(F("Taster ")); Serial.println(taster.neu); } if (DEBUG) { Serial.println("to ZERO"); } } break; case ZERO: OCR1B = pulsForm[ANZAHLTASTER].duty; // Default, Dummyeintrag, kein Taster gedrückt Signal High timerStatus = WAIT; if (DEBUG) { Serial.println("to WAIT"); } break; case WAIT: if (TCNT1 >= pulsForm[taster.alt].duty) { stopTimer1(); timerStatus = CONFIG; if (DEBUG) { Serial.println("to CONFIG"); } } break; case CONFIG: changeTimer1(pulsForm[taster.neu]); timerStatus = START; if (DEBUG) { Serial.println("to START"); } break; case START: startTimer1(pulsForm[taster.neu]); timerStatus = CHECK; taster.alt = taster.neu; if (DEBUG) { Serial.println("to CHECK"); } break; default: if (DEBUG) { Serial.println("Hier ging etwas schief."); } break; } } void checkActiveButton (auto pin) { static unsigned long lastMillis {0}; const unsigned long ms {millis()}; if (ms - lastMillis >= 1) { lastMillis = ms; taster.neu = ANZAHLTASTER; // Default, kein Taster gedrückt for (byte i=0; i