Time between high signal & Low signal + During High Signal

Hi Everybody,

I'm new in Arduino world.
I prepared a test system for time between two button signals. Everything works but the problem is I can measure only time between High & Low signal or opposite. Therefore, I can't measure total time during button press and release.

I want to calculate between first pulse of high signal and next first pulse of high signal. Could you support me if you know a solution please find the codes below. Thank you.

byte button =7;
int led = 8;
int buzzer = 3;
unsigned long startTime;
unsigned long endTime;
unsigned long duration;
unsigned long startTime1;
unsigned long endTime1;
unsigned long duration1;
byte timerRunning;
void setup(){
pinMode (button, INPUT_PULLUP);
pinMode(led, OUTPUT);
pinMode(buzzer, OUTPUT);
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);
  digitalWrite(buzzer, HIGH);
  digitalWrite(led, HIGH);
  }
  else
{
  
  digitalWrite(buzzer, LOW);
  digitalWrite(led, LOW);
}
  
}

At the moment you have two states - timerRunning = 0 and 1. I think if you introduce a 3rd state you can detect that the button has gone HIGH and then LOW again

Something like this

    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
		timerRunning = 2;
	}
    if (timerRunning == 2 && digitalRead(button) == LOW){ // timer running, button released
        endTime = millis();
        timerRunning = 0;
        duration = endTime - startTime;

If I have misunderstood your requirement please make a simple drawing showing the pulse and the points at which you wish to measure the time and post a photo of the drawing.

...R

It works perfectly thank you. :slight_smile:

Pulse example can be find in attachment In order to help other users.

Image from Reply #2 so we don’t have to download it. See this Image Guide

978faa4961df71416930ae8563531614293608e1.png

…R