Using time intervals in calculations

I need to apply a formula that uses the time interval between excitations of a microphone. I know how to bring the millis() function in (total time), but I dont know how to save individual times so I can compute the interval between them. Suggestions would be appreciated.

Rodrigo

	unsigned long t1 = millis();
	
	unsigned long tdelta1 = millis() - t1;

Thank you!

For some reason it is not working, maybe something wrong with my code? (I have not applied the time-interval formula yet). In the loop I have.....

void loop() {

{

noiseLevel = analogRead(A0);
}

{
if(noiseLevel >= threshold) {
counter = counter +1;

}

{

if(noiseLevel >= threshold) {

unsigned long t1 = millis();
unsigned long tdelta = millis() - t1;

}
}

{
if(noiseLevel >= threshold) {

lcd.print ("BN:");
lcd.setCursor(0,1);
lcd.print("STK:");
lcd.setCursor(6,0);
lcd.print(counter);
lcd.setCursor(6,1);
lcd.print(tdelta/1000);
digitalWrite(ledPin,HIGH);
delay(1000);

}
else{

lcd.clear();
digitalWrite(ledPin,LOW);
}
}
}
}

it is only increasing my counter ("BN") every second instead of only when triggered and displaying a zero for STK (where the time interval should be).

RH

did you ever used a stopwatch? calculate the delta in the moment where the events end. and make the variables global. now they exist only in surrounding {}

the equivalent of a stopwatch that catches the time of each trigger is exactly what I need to code in the program. I expect the microphone to be triggered about 3000 times every time i use it (around once per second but it will vary every time)

RH

start the stopwatch t1 = millis();
stop the stopwatch tdelta = millis() - t1;

Got it. So the time interval should be = tdelta1, I'll keep working on it

I appreciate your input

RH