     ++_sameCount;
   // Disable the ADC
   ADCSRA &= ~bit(ADEN);
-
-  _tempReadLast = millis();
+}
+
+static unsigned char scheduleSleep(void)
+{
+  // Need millis to remain constant through this function to prevent overflow
+  unsigned long m = millis();
+  unsigned long durTemperature;
+  unsigned long durRfReceive;
+  unsigned long rfInterval;
+
+  // Check 1: If past due for a temp check do that immediately
+  durTemperature = m - _tempReadLast;
+  if (durTemperature > (_sleepInterval * 1000UL))
+    return WAKETASK_TEMPERATURE;
+
+  // Check 2: Run balls to the wall when searching for signal
+  if (_recvState == RECVSTATE_SEARCHING)
+    return WAKETASK_RFSEARCH;
+
+  // Check 3: Past due for Rf receive, we may have missed the packet already
+  // but run the receive task to let the logic there adjust
+  rfInterval = (_recvLost + 1) * _recvCycleAct - _recvWindow;
+  durRfReceive = m - _recvLast;
+  if (durRfReceive > rfInterval)
+    return WAKETASK_RFRECEIVE;
+
+  // We now know that both of our events are in the future so it is ok to subtract
+  unsigned int sleepDur;
+  unsigned char retVal;
+  durTemperature = (_sleepInterval * 1000U) - durTemperature;
+  durRfReceive = rfInterval - durRfReceive;
+  if (durTemperature < durRfReceive)
+  {
+    sleepDur = durTemperature;
+    retVal = WAKETASK_TEMPERATURE;
+  }
+  else
+  {
+    sleepDur = durRfReceive;
+    retVal = WAKETASK_RFRECEIVE;
+  }
+
+  sleep(sleepDur);
+  return retVal;
 }
 
 void setup(void)

    battPct = (adcSum * 100UL) / (1024 * BATREAD_COUNT);

#ifdef LMREMOTE_SERIAL
    Serial.print("Battery %: "); Serial.print(battPct, DEC); Serial.print('\n');
#endif

    if (battPct < BATTERY_LOW_PCT)


#ifdef LMREMOTE_SERIAL
  PRR &= ~bit(PRUSART0);
  Serial.begin(LMREMOTE_SERIAL); Serial.println("$UCID,lmremote," __DATE__ " " __TIME__); Serial.flush();
#endif

  rf12_initialize(_rfBand);
  // Crystal 1.66MHz Low Battery Detect 2.2V
  rf12_control(0xC040);
  if (_pinLedRx != 0xff) pinMode(_pinLedRx, OUTPUT);
  if (_pinLedTx != 0xff) pinMode(_pinLedTx, OUTPUT);
  if (_pinLedRxSearch != 0xff) pinMode(_pinLedRxSearch, OUTPUT);
  if (_pinLedRxConverge != 0xff) pinMode(_pinLedRxConverge, OUTPUT);
  if (_pinLedRxLocked != 0xff) pinMode(_pinLedRxLocked, OUTPUT);

  pinMode(_pinProbeSupply, OUTPUT);

  // Force a transmit on next read
  memset(_previousReads, 0xff, sizeof(_previousReads));
  resetEstimate();
}

void loop(void)
{
#ifdef LMREMOTE_SERIAL
  Serial.flush();
#endif

  unsigned char wakeTask = scheduleSleep();
  switch (wakeTask)
  {
    case WAKETASK_RFRECEIVE:
      optimalSleep();
      break;
    case WAKETASK_TEMPERATURE:
      checkTemps();
      break;
    case WAKETASK_RFSEARCH:
      rfContinueSearch();
      break;
  }
}
