   #define SLEEPMODE_ADC   SLEEP_MODE_IDLE
 #endif
 
-#define RECV_CYCLE_TIME  5000    // expected receive cycle, millisecond
-#define MIN_RECV_WIN     8       // minimum window size, power of 2
-#define MAX_RECV_WIN     512     // maximum window size, power of 2
+#ifndef digitalWriteFast
+#define digitalWriteFast digitalWrite
+#endif
+
+#define WAKETASK_RFRECEIVE   0
+#define WAKETASK_TEMPERATURE 1
+#define WAKETASK_RFSEARCH    2
 
 static unsigned int _previousReads[RF_PINS_PER_SOURCE];
 static unsigned long _tempReadLast;

    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;
  }
}
