Start timer when value reaches 0. If value goes above zero in timer, interrupt.

Sorry about the title, I ran out of characters.

I am reading the analog 0 pin. When the pin reads a value of 0, I want it to start a timer. This timer will go on for 5 seconds. If within the timer (when the timer is going) the analog read returns a value greater than 0, I want it to break out of the timer, and call another function.

This is what I have but it doesn’t work because it never stops calling function1() even when the value is above 0.

void loop() {
  if (analogRead(0) == 0 && called == false) {
    if(timer <= 5000){
      timer++;
    }
    else{
      Serial.print("time!");
      called = true;
      timer = 0;
    }
  }
  else{
    function1();
  }
  if(analogRead(0) != 0){
    Serial.println(analogRead(0));
    called = false;
    function2();
  }
}

The input to the analog pin is an external audio signal.

Thanks,
Dream

void loop()
{
  currentTime = millis();
  if (analogRead(0) == 0)
    {
      startTime = millis();
      timing = true;
    }
  else
    {
      timing = false;
    }

  if (timing)
    {
       if (currentTime - startTime >= 5000)
         //do whatever you want
    }
}

Awesome, it works!

Thanks,
Dream

DreamingInsanity:
Awesome, it works!

Thanks,
Dream

I am glad that I was able to help.
Do you understand how it works ?

UKHeliBob:
I am glad that I was able to help.
Do you understand how it works ?

I do understand how it works!

Dream