I'm working on a project that requires calculating the time for which a pushbutton is kept pressed without being released. Is there a code snippet that i can use for this particular need ?

  • 1
    Constrain your question. Are you talking Minutes? Seconds? Milliseconds? If milliseconds, how do you plan on contending with switch bounce?
    – st2000
    Jul 8 '18 at 18:33
  • Milliseconds only. I haven't figured out the solution for the bounce problem yet.
    – VishwaV
    Jul 8 '18 at 20:51
  • Was not asking how you were going to solve it. Wanted to know how you were going to interpret it. Guessing, I would say you want to start counting in millisecond increments on first contact. Then I am going to assume you want to stop counting at last contact. But the tricky bit: You will not know about last contact until after (depending on the quality of your switch) 10s of milliseconds goes by with out additional contacts. So, what ever solution you choose, you will need to consider these issues and guess at the timing, try them then adjust them until you get the desired results.
    – st2000
    Jul 9 '18 at 1:44
  • Yeah that is exactly how I'am interpreting this question. Okay let me try and get back here. Thanks buddy.
    – VishwaV
    Jul 9 '18 at 7:58

There is a library called "Debounce", which you can find here: playground.arduino.cc. Based on that library, here is a simple sketch that displays the amount of time a button is pressed.

// Connect one end of a N.O. push button switch to GND and the other
// end to pin 4 of the Arduino.
// NOTE: If the button is held down for less than the debounce delay
//       time, the elapsed time will display the debounce delay time.
#include <Debounce.h>
const byte SWITCH = 4;
unsigned long startTime = 0;
unsigned long endTime = 0;
unsigned long elapsedTime = 0;
const byte debounceDelayTime = 50;

Debounce debouncer = Debounce(debounceDelayTime, SWITCH);

void setup(){

void loop(){
    if(debouncer.read() == 0){
      startTime = millis();
      Serial.println("Button Pressed");
    else if(debouncer.read() == 1){
      endTime = millis();
      Serial.println("Button Released");
      elapsedTime = endTime - startTime;
      Serial.print("Button Held Down For ");
      Serial.println(" ms.");
  • The millis() overflow shouldn't be an issue, since the substraction endTime - startTime will also overflow, since elapsedTime is also an unsigned long.
    – chrisl
    Jul 8 '18 at 20:18
  • Hi VE7JRO, I get this error, is there something wrong with importing the library ? :Arduino: 1.8.5 (Windows 7), Board: "Arduino/Genuino Uno" C:\Users\user\Desktop\buttonpresstrial3\buttonpresstrial3.ino:1:22: fatal error: debounce.h: No such file or directory #include <debounce.h> ^ compilation terminated. exit status 1 Error compiling for board Arduino/Genuino Uno. This report would have more information with "Show verbose output during compilation" option enabled in File -> Preferences.
    – VishwaV
    Jul 8 '18 at 20:43
  • Sorry @VishwaV, you can find it here: playground.arduino.cc/uploads/Code/Debounce.zip. I'll update my answer. I'm using version of the IDE, and I'm guessing the newer versions use the updated Bounce2 libraries.
    – VE7JRO
    Jul 8 '18 at 21:15
  • Hi @VE7JRO, Even after importing it to my libraries folder in arduino, I'm getting this error : D:\arduino-1.8.5\libraries\Debounce\Debounce.cpp:2:22: fatal error: WProgram.h: No such file or directory . What should i do ?
    – VishwaV
    Jul 9 '18 at 8:06
  • On line 2 of Debounce.cpp, remove this: #include "WProgram.h" and replace it with this: #include "Arduino.h".
    – VE7JRO
    Jul 9 '18 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.