unsigned long time = 0;
const int signalType = HIGH;
int signalDuration = 0;
void onSignal()
{
if (signalDuration)
return;
if (digitalRead(inPin) == signalType)
{
time = micros();
}
else
{
signalDuration = micros() - time;
}
}
void loop() {
//...
if (needToCalculateDuration)
{
needToCalculateDuration = 0;
signalDuration = 0;
//attachInterrupt(...)
}
//...
if (signalDuration)
{
//detachInterrupt
doSmthWithDuration(signalDuration)
}
//...
}