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);
}
}