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...