is it just me or Bounce library is broken ?

Hi, I've been trying to use the Bounce library, and have had weird results. I think I use the latest revision of the code, with arduino 022, on a 'full custom, home made' arduino 2009 compatible board (eg 328 based).

My test setup is as follow: I use the bounce example code, and my switch is a two way switch open/to the ground, so in the setup() I added this line: digitalWrite(BUTTON,HIGH);//enable pullup, because we're switching ground

I also change the delay to 1500ms... just to be sure. Then, I do the following: -switch quickly from open to closed, and closed to open again. quickly as 'certainly in less than 1500ms', to see if the debouncing works. -switch quickly from closed to open and backward: same stuff but from a different stable state. guess what the result is ? Actually, as soon a I change the switch state, the led state changes ; it then stays this way for approx 1500ms, before coming back to the expected state. So... I checked the source code, and found that the "int Bounce::update()" function is clearly broken.

Here's my proposed patch: //Quick'nDirty style patch ; should be cleaned out before release, but this at least, actually works int Bounce::update() { uint8_t newState;

//check if pin changed newState = digitalRead(pin); if (state != newState ) { if(false == stateChanged) { //check the flag, to see if it's the first time we see this change stateChanged = true; previous_millis = millis();//reset counter } } if (millis() - previous_millis >= interval_millis) {//debouncing time elapsed, time to draw some conclusions if(state != newState ) {//the state durably changed, reflect that state = newState; stateChanged = false;//dont forget to reset this flag } else { //false alert, filter this change out and watch for new events stateChanged = false;//dont forget to reset this flag } } }

PLEASE NOTE: I haven't tested the other functions of the lib with this patch. It may have broken, but hey, as the core functionnality was broken, I suppose it'd be wiser to check the whole thing before using any other bit of it. Hope this helps...

I do not know what you are expecting, but the original code works perfectly for me. Once a change of state is detected, all future state changes are ignored for the specified time.

That is exactly what the author of the library told me. One issue is if you get EMI on your lines: any spike will trigger a state change, while a differently implemented code would avoid that. What I mean is that the current code doesn't detect "stable" change of state, it just freezes on any new state, which is quite different. Check for example this page: http://www.ganssle.com/debouncing.htm Anyway, as discussed by email, I have to agree that this patch would change quite a lot of things on a lib that is already widely in use, and therefore cannot be applied. I'll keep using my modified version, and everybody will be happy this way :)

Hey, I just found this thread after debugging a problem with a product I'm developing where my switch code was not working right. And I have to say I totally agree that the current bounce library is totally broken. It doesn't debounce a signal it simply delays recognition of a new signal. It's also quite funny that I also am a big fan of the same Jack Ganssle the other poster cited and have based my debounce code over the years and many (non Arduino) projects on articles he did back in the 90s.

So, my statement is that if you want code that actually debounces a switch input then don't use this debounce library -- write your own. I figured I would go against my typical "write it yourself to make sure it's right" mentality and use this preexisting library. That was a mistake.

Caveat emptor.