debounce question: introduce delayed action

Robin2:
The code in the Original Post looks very convoluted to me. I think the following does the same thing but IMHO in a more logical way. I deal with the debounce and the state-change check first. Then deal with the led timing. Note that I have changed the name of the variable reading for what I believe is greater clarity.

void loop() {

currentTime = millis();
        // first debounce
    if (currentTime - lastDebounceTime > debounceDelay) {
        lastDebounceTime += debounceDelay;
        buttonState1 = digitalRead(buttonPin1);
       
            // then check the change of state
        if (buttonState1 != prevButtonState1) {
            if (buttonState1 == HIGH) {
              ledState = !ledState;
                if (ledState == HIGH) {
                    startTime = currentTime;
                }
            }
            prevButtonState1 = buttonState1;
        }
    }
        // then manage the LED
    if (currentTime - startTime > 10000 and ledState == HIGH) {
        digitalWrite(LED2, HIGH);
    }
    else {
        digitalWrite(LED2, LOW);
    }
}




EDIT to add ...

The final IF statement (or more particularly its ELSE clause) may not do what you expect because the ELSE will be triggered if either part of the IF is false - which is probably most of the time.

...R

Thank you Robin2, grts