Go Down

Topic: Button Press (Read 603 times) previous topic - next topic


Aug 05, 2011, 02:18 am Last Edit: Aug 05, 2011, 03:17 am by mykiscool Reason: 1
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: [Select]

void loop()
 buttonState = digitalRead(buttonpin);
  lcd.setCursor(0, 1);
 if (buttonState == HIGH)
  presses = presses + 1;
Technician at Yuneec Electric Aviation

Nick Gammon

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.
Please post technical questions on the forum, not by personal message. Thanks!

More info:


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

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:
Serial LCD keypad panel,phi_prompt user interface library,SDI-12 USB Adapter

David Pankhurst

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: [Select]

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


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

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

I write books about Arduino and Electronics: http://simonmonk.org

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131