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

Hello everyone.
I am new to the community of arduino, but I am not new to Arduino world.

I have got an issue which seems to be really simple but I cannot solve.
I have a board called ArduPLC Micro, which is compatible (when coding) with board Arduino Micro; I wired a push button embedding a 5V LED and a built-in resistor (Rugged Metal Pushbutton with White LED Ring [16mm White Momentary] : ID 558 : $4.95 : Adafruit Industries, Unique & fun DIY electronics and kits) in the following way:

the contact C of the push button is connected to the ground and the contact NO is in the digital I/O IO1 [PIN0].
The embedded LED has the - to GND and the + wired in IO2 [PIN9] set as output.
the board embeds 4K7 ohm pull-up resistors for the digital I/O set as inputs, so PIN0 has the pull-up resistor enabled via a jumper.

when I push the momentary button, I notice that it takes the "command" only if I press and hold the push button for 1s or so. If I click the button without holding, the program doesn't start, and the LED doesn't turn on (or turn off, depending on previous state).

apart from this the rest of the program works well..so the only problem I can't solve is this.

Here you can find the code:
(if this is not enough please let me know)

int buttonState;
int lastButtonState = LOW;
int counter = 0; /* counts if the push button has been clicked and even or odd number of times*/

/* momentary push button NO contact wired in IO1 [PIN0] with internal pull-up resistor in the micro ATmega enabled, and external 4K7 ohm pull-up resistor enabled. Contact "C" of the push button is wired in GND */
#define button 0
#define led 9

void setup() {

pinMode(button, INPUT_PULLUP);
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
}

void loop(){
buttonState = digitalRead(button);
if(buttonState != lastButtonState){
if(buttonState == LOW){
counter = counter + 1;
}
}
lastButtonState = buttonState;

// if the counter is odd, then the button went from OFF to ON
if(counter%2 == 1){

digitalWrite(led, HIGH);
/* DO STUFF */

}
if(counter%2 == 0){

digitalWrite(led, LOW);
/* DO OTHER STUFF */
}
}

If I did anything wrong, or if you have any advice I am glad to learn better and more.
thank you

If I did anything wrong, or if you have any advice I am glad to learn better and more.

The /* DO STUFF / and / DO OTHER STUFF */ comments bother me. Did you delete that stuff, or is there really nothing there (yet)?

I see no reason that the code that was posted (incorrectly) shouldn't loop thousands to times per second, and reacting instantly when the switch becomes pressed.

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.

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:

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

It doesn't matter how much code you take out of loop() and put into functions called from loop(), if there is blocking code in the functions then the operation of loop() will be affected.

Did you refactor your code to remove blocking when you put it into functions ?