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;
}
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:
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.
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.
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.
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.
Everybody has to learn and nobody knows everything 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
A software man wait several ms before bounces stop.
An electronician doesn't wait : signal is clean immediately