Measuring time between button presses

Hi guys.
I searched the internet for a simple program which would measure the time between pressing a button,
but none of that were suitable for me. And that’s why I write here. Can someone help me put together a simple program that would do this:

When I press the button first time it will start counting the time in milliseconds, when press it again stop count, print the value to the serial monitor, reset to zero and start count again. And do this in round.

I have the button debounced by rc filter and Schmitt trigger, so I have no problem with bouncing.

Thank you in advance for any help.

That's pretty simple to do.
Assume a button that connects a pin to Gnd when pressed:

byte button =2;
unsigned long startTime;
unsigned long endTime;
unsigned long duration;
byte timerRunning;
void setup(){
pinMode (button, INPUT_PULLUP);
Serial.begin(9600);
}
void loop(){
  if (timerRunning == 0 && digitalRead(button) == LOW){ // button pressed & timer not running already
  startTime = millis();
  timerRunning = 1;
  }
  if (timerRunning == 1 && digitalRead(button) == HIGH){ // timer running, button released
  endTime = millis();
  timerRunning = 0;
  duration = endTime - startTime;
  Serial.print ("button press time in milliseconds: ");
  Serial.println (duration);
  }
}

Even compiles on the first pass. Let me know how it works.

1 Like

I try it.
It works perfectly, exactly as I needed.
I just changed INPUT PULLUP to just INPUT because I have pullup resistor on switch wich is inverted by schmitt trigger.
Thank you very much, it help me a lot.

Hello. May i know how to program when sensor detect object it star record the time and it stop record when ocject is leave the sensor. I use ir module as the senor... Thank you

That's being answered in the thread you opened on the same topic (which, by the way, is the correct thing to do - asking the same question in a different, unrelated thread, is not).

@crossroads : I read your code and it works perfectly-- Have you experience with mongoose os ? I would like to do same with mos. But my current code shows error.

Hello,
@CrossRoads

Your code seems to work perfectly fine, however once I let go of the button and then press it again, the timer starts from zero all over again. How do I make the timer start again from where it had left off when I let go off the button earlier?

CrossRoads colleague code works perfectly.
And I didn't want to start from scratch, so I added a variable to keep durathion and add the new durathion value every time.
sumOfDuration+=duration;

hello friend first of all thank you for sharing i would be glad if you can help with something.
I want to use the codes for another purpose, but I couldn't, I would like a led or buzzer to be active if the second press time after the first press of a button, for example 3 seconds, is delayed.

cenkgursu, can you start a new topic and give a link to this one.
Explain carefully what you want, because I don’t understand it. When should the led or buzzer become active and when not ?

Pressing a button, the counter will start and will alert if I do not press it again in 3 seconds. If I press the button before the time is up, it will not warn

Koepel:
cenkgursu, can you start a new topic and give a link to this one.
Explain carefully what you want, because I don't understand it. When should the led or buzzer become active and when not ?

I have an application that I get the number of times the button is pressed when I press a button, it becomes LOW when the button is pressed. If the button is not pressed for 3 seconds, I would like a warning led or buzzer to be active. If the button is pressed before the 3 seconds time is up, it will not warn. I can only do this once, but the time continues to count. I would be glad if you helped.