easy debounce button?

Hello i have tried my code (i am sure someone did the same thing before ) for debouncing button, arduino´s debounce button code was a little confusing for me, so i made my own debounce button code: here is the code:

const int debounce=10000;
unsigned long button=0;
unsigned long conteo=0;

void setup()
{

Serial.begin(9600);
pinMode (7,INPUT);
delay(1000);
}

void loop()
{

if ( digitalRead(7)==HIGH and button<debounce)
{
button++;
if (button==debounce)
{
conteo++;
Serial.println(conteo);
}
}
if (digitalRead(7)==LOW and button==debounce)
{
button=0;
}

}

here is a video demostration : (i made it in lcd but code has no lcd library add it if you want)

it is like when you press a button and add a number for example “number++;” it will add number like forever increasing 1 by 1 very fast, it could be like the “millis” almost the same cycle, for example if i check number and millis after ten seconds it would be like:

number : 10

millis : 10001

almost the same…

that´s what the code does with “const int debounce=10000;” that is the limit for the add for avoiding debounce, i use button with a little large heads, it works better for my application, i hace test it with 2 buttons also, works ok,

I have a feeling that i have to ask if it is ok?, arduino´s expert may be see somthing that i don´t , maybe if i use like ten buttons maybe. i don´t know , i wanted to share this, thanks

50745120_317567228886689_3230842689754759168_n.jpg

For debouncing inputs, just poll them every 50ms.

Then look for a state change.

Don’t get complicated.

What does "POll" mean and is it necessary to debounce a limit switch?

mn i think you should try a limit switch first maybe with a counter and see if counting is ok

to larryd

yeah that´s another way

Do you understand this code:

const byte mySwitch = 7;

byte lastState = LOW;

unsigned long conteo = 0;
unsigned long previousMillis;

//*****************************************************************************
void setup()
{
  Serial.begin(9600);
  pinMode (mySwitch, INPUT);
  
}

//*****************************************************************************
void loop()
{
  //*****************************
  //is it time to poll the switch(es)?
  if (millis() - previousMillis >= 50)  //every 50ms
  {
    //re-initialize the timing
    previousMillis = millis();

    //go and check my switches
    checkSwitches();
  }
  //*****************************

} //END of loop()


//*****************************************************************************
void checkSwitches()
{
  byte currentState = digitalRead(mySwitch);

  //*****************************
  if (lastState != currentState)
  {
    //update to the new state of the switch
    lastState = currentState;

    if (currentState == HIGH)
    {
      conteo++;
      Serial.println(conteo);
    }

    else
    {
      //do some LOW stuff if needed
    }
  }
  //*****************************

  //other switches go here

} //END of checkSwitches()

//*****************************************************************************

HermanJFourie: What does "POll" mean and is it necessary to debounce a limit switch?

"poll" means "digitalRead(ButtonPin);" Poll means to check an input or a flag or a port etc. regularly in your loop. Yes, it is necessary to debounce a mechanical switch if you are trying to use a single mechanical action (pushing a button) as a single trigger input. This is because as the switch closes, there will be a lot of noise, oscillation between high and low states, so depending on how you are polling, it is possible for a single mechanical input to trigger multiple times.

Almost all the problems are contained in this one line...

if (digitalRead(7)==LOW and button==debounce)

"7". Hmmm. So this code can only ever be used to debounce pin 7 and no other. Time to learn about parameters.

"button". Variable names should describe what they contain. This certainly doesn't contain a button or pin number. Variable names are "free as in beer" because they aren't downloaded to the Arduino. A long variable name takes the same precious SRAM space as a short one.

Call it numCyclesHeldDown or something.

So now we see that the duration of the button debounce is driven by the duration of loop() cycles. Once you finish testing and tuning in your little example, what happens when you move it into a real program which takes a whole millisecond to run loop()? You have to adjust it every time. Then a more-complex program does more work some of the time so your adjustment must be adjusted.

"debounce". Same problem with naming. What is this for? numCyclesDeterminedToBeARealButtonPress? Maybe you can think of a shorter name equally descriptive.

"==". Ohhhh. Nasty. It is only possible to reset the numCycles when the number is exactly equal to the maximum. So you could have an unlucky bounce and fill this up to just a few counts less than the max and now we are sitting on a hair-trigger that doesn't debounce.

Screw up somewhere else and you might add twice accidentally. You could go past equality. Now the whole program is unresponsive until you hold the button down long enough for your counter to overflow the long int and cycle back to zero. That could take days or years of holding it down.

Even without that screwup, once debounced it just keeps counting as fast as possible. You've done the same as a bare digitalRead() but with a somewhat-random delay built in.

If you really have problems, just load up the Bounce.h library and try to forget about how it works internally.

If you really have problems, just load up the Bounce.h library and try to forget about how it works internally.

Apologies for intervention... but just a quick question: What if the switches are not directly connected to arduino but to a port expander chip (eg PCF8574) ? Does any debounce library support this option?

That is more complex. You might assume that the slower polling rate on the port expander will hide most bounces.