Why complicate debouncing?

Hello tinkerers! I'm a little confused on debouncing a button why use this https://www.arduino.cc/en/Tutorial/Debounce?action=sourceblock&num=1

If you can use as simple as this:

const int LED = 13, BTN = 2;
int ledState = HIGH, lastBtnState = LOW;
void setup() {
  pinMode(LED, OUTPUT);
  pinMode(BTN, INPUT);
  digitalWrite(LED, ledState);
}

void loop() {
  byte state = digitalRead(BTN);
  if (state == HIGH && lastBtnState == LOW) {
    
    ledState = !ledState;
    delay(20);
    digitalWrite(LED, ledState);
    
  }
  delay(10);
  lastBtnState = state;
}

They both work but, why is it that the Arduino's debouncing code is so lengthy?

why is it that the Arduino's debouncing code is so lengthy?

Maybe because it makes better use of the processor?

karlmarx:
They both work but, why is it that the Arduino's debouncing code is so lengthy?

because most of the code you posted is commentary explaining what's happening... there really isn't much happening at all, plus the author accomplished what yours won't... having a non-blocking button press example (i.e. notice he's not using delay() )

const int buttonPin = 2; 
const int ledPin = 13; 

int ledState = HIGH; 
int buttonState; 
int lastButtonState = LOW;   

long lastDebounceTime = 0; 
long debounceDelay = 50;  

void setup() 
{
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, ledState);
}

void loop() 
{
  int reading = digitalRead(buttonPin);
  if (reading != lastButtonState) 
  {
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) 
  {

    if (reading != buttonState) 
    {
      buttonState = reading;
      if (buttonState == HIGH) 
      {
        ledState = !ledState;
      }
    }
  }
  digitalWrite(ledPin, ledState);
  lastButtonState = reading;
}

Because debouncing is complex.

If I used delay(20) in any of my real working code then I would have motors running off the end of their tracks, serial buffers overflowing and people mashing the buttons really hard because It doesn't always get their first attempt at pushing the button.

karlmarx:
Hello tinkerers! I'm a little confused on debouncing a button

I think you are mixing up "debouncing" with "detecting the change in button state"

Your piece of code is doing both.

The actual debouncing problem is covered by the line

delay(20);

which allows a little time between successive digitalRead()s so that spurious switch contacts are ignored.

The rest of the code is for detecting the change of state.


Don't assume that the official examples represent coding perfection. :slight_smile:

...R

karlmarx:
They both work but, why is it that the Arduino's debouncing code is so lengthy?

Well, the answer is that they don't both work. At least, not for certain types of buttons - most especially reed switches, which tend to go 'boi-oi-oi-oi-oi-oinnnnggggggg'. The other issue is that your code introduces delays into processing. This might be fine for your app, but it won't do at all for something that is metering fluid flow and needs to respond to button clicks.

The arduino code is lengthy because debouncing is a hard problem, especially in the general case. Even the hardware guys discuss and debate the merits of different ways of doing it. You'd think that all you need to do is slap a capacitor across the contacts. Apparently its more complicated than that.

My little debouncer is available here.

I have a separate class 'Button' for this which works with interrupts. And you call the example code complicated?

karlmarx:
Hello tinkerers! I'm a little confused on debouncing a button ...

@KarlMarx: Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the "Code" icon above the posting area. It is the first icon, with the symbol: </>

How to use this forum

karlmarx:
They both work but, why is it that the Arduino's debouncing code is so lengthy?

It's not that lengthy. If we remove comments, I make the Arduino example as 28 lines of code and your code as 19. There are more than one ways of skinning a cat. The longer example handles a slightly more complex situation.

@Nick Gammon Hi sir. Can you point out that complex situation that the example code handles sir?

The situation it avoids is all the thumb-twiddling your code does.

There is a very simple explanation of the disadvantages of using the delay() function here:
https://www.arduino.cc/en/Reference/Delay and here: Arduino Playground - AvoidDelay

karlmarx:
@Nick Gammon Hi sir. Can you point out that complex situation that the example code handles sir?

As the other replies said, yours blocks, the more complex one doesn't. That may or may not matter to you.

it seldom matters until it becomes a problem, then you are back here asking why your sensors don't sense, servos don't servo or your pushbuttons don't pushbutton.

:wink:

Thanks for all the replies. It's now all clear to me. And excuse my ignorance, I'm just starting out with arduino as my hobby. :slight_smile:

karlmarx:
Thanks for all the replies. It's now all clear to me. And excuse my ignorance, I'm just starting out with arduino as my hobby. :slight_smile:

Everybody has to learn and nobody knows everything :slight_smile: A number of the more seasoned users here sometimes seem to forget that there are newbies here as well.

The fact that you only have a post count of 3 (at this moment) does not necessarily mean that you're a newbie and hence the answers might not be adjusted to your experience level.

There are many manners for debouncing.
A sofware man debounces with many lines of code.
An electronician man simply sold a capacitor on the button's pins and add 0 line of code :smiley:

A software man wait several ms before bounces stop.
An electronician doesn't wait : signal is clean immediately :smiley:

The electronician does not even notice that his signal is delayed.

Discharge RC time constant is extremely short : with 100nF and an "enormous contact resistor of 1 ohm" RCdischarge = 100 ns

Signal can't be delayed : it is impossible unless you consider that 100ns (less than two clock cycle) is a delay.

sterretje:
A number of the more seasoned users here sometimes seem to forget that there are newbies here as well.

I don't forget that. I had a post count of one, a few years ago.

I think this thread has been pretty friendly. We have tried to explain various pros and cons of certain approaches.