Momentary push button embedding LED, has delay in switching ON and running code

Hello PaulS

thank you very much for replying. Apologise for incorrectly uploading the code.

"If you deleted code after those comments, then that code could be causing loop() to take a long time to iterate, meaning that you need to hold the switch pressed until loop() starts again, and reads the switch state."

actually yes, your advice was correct.

I finally solved the problem with the following code, for momentary push buttons , that checks the edge detection:

boolean readButtons() {

  // Read button states every 5 ms (debounce time):
  static unsigned long lastDebounce;
  if (millis() - lastDebounce >= 5) {
    lastDebounce = millis();

    // Rising edge (if switch is released)
    if (DRE(digitalRead(button), button1RisingState)) {
      Serial.print ("Rising edge (pulled high by internal pullup resistor). State variable: ");
      Serial.println (button1RisingState);
    }

    // Falling edge (if switch is pressed)
    if (DFE(digitalRead(button), button1FallingState)) {
      digitalWrite(led, !digitalRead(led)); // switch LED on or off
      Serial.print ("Falling edge (pulled low by switch). State variable: ");
      Serial.println (button1FallingState);
      counter = counter + 1;
    }
  }
}

the code is available here:
Switched_Edge_Detection by The DIY Guy999

and modified the rest of my algorithm declaring standalone functions instead, called then in the loop()

Thank you for your time :smiley: