ATTiny, Counting with interupts

Actually I think the whole logic was screwed in the code above, I must have been having a brain fart. :blush:

This code does seem to work as expected...

void CountBeeps()
{
  // if the output is on
  if (onTimer != 0)
  {
    // check for output time out
    if ( millis() - onTimer > outTime * 1000)
    {
       // if time is up - turn off output, reset vars/flags & sleep
       digitalWrite(outPin, LOW);
       onTimer = 0; 
       beepCount = 0; 
       startTime = 0;
       lastBeepTime = 0;  
       goToSleep();
    }
  }
  else // output is off
  {
    // get length of beep
    unsigned long beepLength = TimeBeep ();
    // check if the beep is longer than sensitivity * 500 (500,1000,1500)
    if (beepLength >= sensitivity * 500)
    {
      // turn on output
      TurnOnOutput();
    }
    else
    {
      // say length > 50 is a valid beep
      if (beepLength > 50)
      {
        Debug.print("Beep length: ");
        Debug.println(beepLength);
        beepCount +=1;
        // if count equal or more than 3
        if ( beepCount >= 3)
        {
          // turn on output
          TurnOnOutput();
        }
      }
      //  if output wasn't turned on above
      if (onTimer ==0)
      {
        // if time is longer than 2 seconds + sensitivity (1,2,3)
        if (millis() - startTime >= (2 + sensitivity) * 1000)
        {
          // count not met so reset and sleep
          Debug.print("Beeps: ");
          Debug.println(beepCount);
          beepCount = 0;
          startTime = 0;
          goToSleep();
        }
        else
        {
	  // reset last beep time
          lastBeepTime = 0;
        }
      }
    }
  }
}

void TurnOnOutput()
{
  // turn on output
  digitalWrite(outPin, HIGH);
  // set output timer
  onTimer = millis();  
}

unsigned long TimeBeep()
{
  // if lastbeeptime 0 then get current time
  if (lastBeepTime == 0)
  {
    lastBeepTime = millis();
  }
  // loop until pin high or timeout
  while (!digitalRead(inPin))
  {
    if ( millis() - lastBeepTime >= sensitivity * 500)
    {
      break;
    }
  }
  // return beep length
  return millis() - lastBeepTime;
}

void wake()
{
  // set timers
  startTime = millis();
  lastBeepTime = startTime;
}

The extra current is only 2ma for a max of 5 seconds, so probably not worth worrying about anyway.

Regards,

Les