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'
// 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);
}
}
Do you have external pull-ups or pull-downs (or what) on the switch pins?
What happens if you use the Bounce risingEdge() and/or fallingEdge() function instead of the read() function for the switches?
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.
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.
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.
davekw7x:
What happens if you use the Bounce risingEdge() ... function instead of the read() function for the switches?
Maybe like this:
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...