Go Down

Topic: Beginners (de)Bounce code on momentary buttons wont work. (Read 2574 times) previous topic - next topic

cruzah

Hi,
Im trying to use two momentary buttons as digitial inputs to count up (+1) and down (-1) using the counter 'x'. Im displaing the result on an LCD screen.

However the counter is going up by random amounts. I assume as a result of 'bounce'

Why is my bounce code not working?
Reference: http://arduino.cc/playground/Code/Bounce

Any help much appreciated!

Cheers

Code: [Select]

// include the library code:
#include <LiquidCrystal.h>
#include <Bounce.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

int up = 3;         // the number of the input pin
int down = 4;

// Instantiate a Bounce object with a 5 millisecond debounce time
Bounce bouncerup = Bounce(up,5 );
Bounce bouncerdown = Bounce(down,5 );

//counter
int x = 0;

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
 
  pinMode(up, INPUT);
  pinMode(down, INPUT);
}

void loop() {
  bouncerup.update ( );
  bouncerdown.update ( );

  int readup = bouncerup.read();
  int readdown = bouncerdown.read();

  // BUTTON UP
    if (readup == HIGH) {     
      x = x +1;
      lcd.clear();
      lcd.print(x);
  }
 
  // BUTTON DOWN
    if (readdown == HIGH) {       
      x = x -1;
      lcd.clear();
      lcd.print(x);
  }
 
}



davekw7x

#1
Jun 12, 2011, 03:26 pm Last Edit: Jun 12, 2011, 03:36 pm by davekw7x Reason: 1
1.  Do you have external pull-ups or pull-downs (or what) on the switch pins?

2.  What happens if you use the Bounce risingEdge() and/or fallingEdge() function instead of the read() function for the switches?

3.  If you have a few extra counts for either switch, then try increasing the debounce interval when you create the Bounce objects.  If you have a whole lot of extra counts, then post again and describe your switch connections.


Regards,

Dave

PaulS

You are not using the internal pull-up resistors. Is it safe to assume that you have external pull-up or pull-down resistors with the switches? If so, how are they wired? If not, that is your problem.
The art of getting good answers lies in asking good questions.

cruzah

#3
Jun 13, 2011, 02:18 am Last Edit: Jun 13, 2011, 02:31 am by cruzah Reason: 1
I have connected it on my breadboard like this -



One side of the buttons are connected to ground via '10k 0.5W Metal Film Resistor' then to a digital pin on Arduino. Other side of button is connected to 5v.

Im getting about 30-50 extra counts per press.

How does my wiring look?

thanks again.

davekw7x

#4
Jun 13, 2011, 03:02 pm Last Edit: Jun 13, 2011, 04:58 pm by davekw7x Reason: 1
I hate to repeat myself but:
Quote from: davekw7x

What happens if you use the Bounce risingEdge() ... function instead of the read() function for the switches?


Maybe like this:
Code: [Select]

 int readup = bouncerup.risingEdge();
 int readdown = bouncerdown.risingEdge()



Regards,

Dave

Footnote:

For some reason your image at http://s3.postimage.org/7wl8qim7p/arduino_nano_hookup450.gif didn't show up with my browser.  Looking at it now, it seems to match your description, and it matches my current hookup, which works for me (after changing your code the way that I indicated above.)

Anyhow...

There is a question that I might have asked in my previous response.  I'll ask it now:

With your original code, what happens if you press and hold one of the switches for a second or two before releasing instead of just "jabbing" it?  That can, maybe, give a clue...

Go Up