Using the bounce (debounce) library

Hi,

I’ve found the bounce library on the Arduino website. Am I right in thinking I can use this as a neater way of coding a debounce? It does the same thing but I don’t need to clutter my sketch with the debounce code, it’s in the library? I’m using it like this:-

// Includes
#include <Bounce.h>

// Definitions
#define NurseryButt 2
#define NurseryLED 4
#define DoorbellButt 3
#define DoorbellLED 5


// Instantiate a Bounce object with a 3 millisecond debounce time 
Bounce nursery = Bounce( NurseryButt,2 ); 
Bounce doorbell = Bounce( DoorbellButt,2 ); 

void setup() {
  pinMode(NurseryButt,INPUT);
  pinMode(NurseryLED,OUTPUT);
  pinMode(DoorbellButt,INPUT);
  pinMode(DoorbellLED,OUTPUT);
  }
}

void loop() {
 // Update the debouncer
  nursery.update ( );
  doorbell.update ( );  

 
 // Get the update value
 int nurseryvalue = nursery.read();
 int doorbellvalue = doorbell.read();
 
 // Turn on or off the Nursery lamp
 if ( nurseryvalue == HIGH) {
   digitalWrite(NurseryLED, HIGH );
 } else {
    digitalWrite(NurseryLED, LOW );
 }
 // Turn on or off the doorbell lamp
 if ( doorbellvalue == HIGH) {
   digitalWrite(DoorbellLED, HIGH );
 } else {
    digitalWrite(DoorbellLED, LOW );
 } 

}

Does that look right? i.e. create a whole new load of bounce objects and debouncer values etc for every input?

// Instantiate a Bounce object with a 3 millisecond debounce time 
Bounce nursery = Bounce( NurseryButt,2 ); 
Bounce doorbell = Bounce( DoorbellButt,2 );

3, huh?

Does that look right?

Yes, but more importantly, does it work? Only you can tell that.

PaulS:

Does that look right?

Yes, but more importantly, does it work? Only you can tell that.

Well... it works, but I get a LOT of flicker with my LEDs which is not ideal...

PaulS:

// Instantiate a Bounce object with a 3 millisecond debounce time 

Bounce nursery = Bounce( NurseryButt,2 );
Bounce doorbell = Bounce( DoorbellButt,2 );



3, huh?

Ignore that, it's just from not having updated the comment when I was trying to get rid of the flicker. Surely I need a certain minimum for the debounce routine to be effective?

Surely I need a certain minimum for the debounce routine to be effective?

Sure. You are talking about a real contact caused by a human vs. contact bounce caused my mechanical issues in the switch itself. A debounce time of 50 milliseconds would likely be better.

It's hard for people to press, release, and press again in that period of time. And, if they do, do you really want to count that as two presses?

PaulS:

Surely I need a certain minimum for the debounce routine to be effective?

A debounce time of 50 milliseconds would likely be better.
It's hard for people to press, release, and press again in that period of time. And, if they do, do you really want to count that as two presses?

Thanks. That sounds good. But won't that result in massive LED flickering? At 5ms it was noticeably flickering, when I reduced it to 2ms it was "almost" like a constant. I probably have an error in my code wouldn't you say?

I probably have an error in my code wouldn't you say?

I'd say it is more likely that you do not have your switches wired correctly. You are not using the internal pullup resistors, so external pullup or pulldown resistors are needed. How ARE your switches wired?

PaulS:

I probably have an error in my code wouldn't you say?

How ARE your switches wired?

...you don't want to know.

Directly.

:blush:

...you don't want to know.

Yes, I do.

Directly.

What does this mean? You need to have the switch between two things. What two things do you have it (them) between?

PaulS:

...you don't want to know.

Yes, I do.

Directly.

What does this mean? You need to have the switch between two things. What two things do you have it (them) between?

I was being kinda tongue in cheek with the "you don't want to know". Sorry if that wasn't understood.

I have the switch between two things. Ground and an input pin. No resistors used. Same with my LEDs.

No resistors used. Same with my LEDs.

Bad boy! No desert for you. You'll need to save that money to buy a new Arduino.

I have the switch between two things. Ground and an input pin.

So, when the switch is not pressed, the pin is floating, and when it is pressed, the pin is connected to ground. The "when pressed" part is OK. The "when not pressed" part is not.

PaulS:
You'll need to save that money to buy a new Arduino.

Eek...hope not. Was just trying to save time when prototyping (I see it was false economy now). What do you think could have damaged the arduino?

I have the switch between two things. Ground and an input pin.

So, when the switch is not pressed, the pin is floating[/quote]
Coudl this explain the flickering LED? The thing is, it never flickers when the switch is off...only when it's on?

I'll take a look tonight.

What do you think could have damaged the arduino?

An LED has no current limiting resistor. They will suck more current that the Arduino can provide. Once on, an LED looks like a short circuit.

Oh dear. So I guess I have a potential board failure to look forward to? Whoops. I mean they haven’t been on for long, but they’ve definitely been on…

So I guess I have a potential board failure to look forward to?

Probably not immediately, and only for the pins that the LEDs were connected to. The good news is that a new chip is only $6 at Sparkfun. No need to replace the whole board (unless you have the SMD version).