Debounce function internal resistor pull-up doesn't work well

I want to add a function debounce for my button using internal resistor pullup

But this function, I think that it don’t work enough well

Could help me improve my function debounce

Why when I hit my button doesn’t turn on led correctly

// Actuators: Buttons and LEDS

#define PUMP_ON 11 //push-button
#define LED_PIN 13

//Variables to be used by Actuators

boolean pumpStatus = 0;
int timePumpOn = 10; // turn pump on in minutes

// Variables to be used with times

long sampleTimingSeconds = 30;  // ==> Define Sample time in seconds
long startTiming = 0;
long elapsedTime = 0;

void setup() {

 pinMode(LED_PIN, OUTPUT);
 //pinMode(PUMP_PIN, OUTPUT);
 pinMode(PUMP_ON, INPUT_PULLUP); //Button
 Serial.begin(9600);
 startTiming = millis(); //starting the "program  clock"

}

void loop() {

 readLocalCmd();

}

void readLocalCmd()
{
 int digiValue = debounce(PUMP_ON);
 Serial.print(digiValue);
 if(!digiValue)
 {
   pumpStatus = !pumpStatus;    
   aplyCmd();
 }
}

void aplyCmd()
{
 if(pumpStatus == 1)
 {
   digitalWrite(LED_PIN, HIGH);
 }
 if(pumpStatus == 0) 
 {
 
   digitalWrite(LED_PIN, LOW);
 }
}

boolean debounce(int pin)
{
 boolean state; 
 boolean previousState;
 const int debounceDelay = 60;

 previousState = digitalRead(pin);

 for(int counter=0; counter<debounceDelay; counter++)
 {
   delay(1);
   state = digitalRead(pin);
   if(state != previousState)
   {
     counter = 0;
     previousState = state;
   }
 }

 return state;
}

That's the worst example of debouncing a switch that I've ever seen. Look at some of other examples. All that is required is that, when you detect a state change the first time, you ignore any other state changes that happen too soon (for whatever too soon means to you) after that.

Please modify your post and use the code button </>

so your code looks like this

and is easy to copy to a text editor. See How to use the Forum

The use of a pullup resistor will not necessarily solve all bounce problems on its own. Many switches make contact with a few glitches which can be detected by an Arduino but not by a human. A common way to deal with that is to leave a short interval (maybe 50 millisecs) between subsequent reads of the button.

Another issue which is often confused with bounce is the need to detect when the switch changes state - particularly if the same button is used to (say) turn something on with one press and off with the next press. That involves saving the state of the switch the last time it was read and comparing it with the latest reading.

…R

If you are having bounce problems then a simple capacitor across the switch to ground will usually solve it.

Also posted at https://stackoverflow.com/q/46194755

It seems like you have a habit of doing this cross posting thing every time. If you're going to do that please be considerate enough to post a link to the other places you've asked the question so we don't waste time due to duplicate effort and so that anyone else with the same question who comes across the post can easily find all the relevant information.