Pages: [1]   Go Down
Author Topic: Button Press  (Read 399 times)
0 Members and 1 Guest are viewing this topic.
California
Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

How can I make a button only record 1 hit per button press. Right now the loop happens so fast it sees that the button state is on high many times and adds like 100 hits per second. Will something like debounce help? Can someone please give me help. I would really appreciate some sample code.

Code:
void loop()
{
  buttonState = digitalRead(buttonpin);
   lcd.setCursor(0, 1);
  if (buttonState == HIGH)
  {
   presses = presses + 1;
  }
« Last Edit: August 04, 2011, 08:17:40 pm by mykiscool » Logged

Proud Member of the Nighthawk Robotics Club Team 569 B

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Debouncing handles when the switch bounces on/off/on/off due to mechanical action of the springs. Your problem is more simple, you keep incrementing while the button is pressed, so if it goes up 100 for one press you are probably holding it down for 1/10th of a second or so.

Look in your Examples folder of the IDE (Examples menu). There is a debounce example that not only debounces, it checks for the button changing state.
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 72
Posts: 7171
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

A button is a digital input device. It has two states, HIGH and LOW. Detecting it is as simple as digitalRead(button_pin).
On the other hand, if you want to detect a press, that is different, you want to detect essentially a transition from LOW to HIGH (or HIGH to LOW if you use pull-up resistor). To detect transition (in calculus terms, to calculate a derivative), you need to save the previous button status from the previous iteration and compare it with the current reading. If the previous state is LOW and the current is HIGH, you got a press, and you can do what you want when a button is pressed, such as increment a variable. Apparently you need a variable to hold the previous state of the button and overwrite it every time at the end of an iteration with the current value.

Give it a try see if you can make it work. It should look like this:

start with previous state as LOW state

loop:
read current state
if previous state is LOW and current state is HIGH, button is pressed and do something as a response
update previous state with current state
loop end:
Logged


Victoria, BC, Canada
Offline Offline
Full Member
***
Karma: 0
Posts: 222
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I would really appreciate some sample code.

If all you want is to count wait after a press, then a simple option is to use delay()

Code:
void loop()
{
  buttonState = digitalRead(buttonpin);
  lcd.setCursor(0, 1);
  if (buttonState == HIGH)
  {
    presses = presses + 1;
    wait(1000); // no other key presses read for 1 second
  }

When you're comfortable with delay code, look at the BlinkWithOutDelay example (Arduino menu, File; Examples; 2.Digital) to see how to avoid stopping your whole program for that one second...
Logged

UK
Offline Offline
God Member
*****
Karma: 13
Posts: 903
Twitter: @simonmonk2
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

There is also a nice library that takes care of all this:

Nice and easy to use. Have a look at the examples.

http://www.arduino.cc/playground/Code/Bounce
Logged

--
My New Arduino Book: http://www.arduinobook.com

Pages: [1]   Go Up
Jump to: