 #include <OneWire.h>
 #include <DallasTemperature.h>
 
-#define VERSION_STRING "V3.1.2"
-
-// Number of readings before a battery reading is taken
-#define BATTERY_READ_INTERVAL 10
-
-// Polling interval values
-#define FIFTEENMINS 0
-#define TENMINS 1
-#define FIVEMINS 2
-#define TWENTYSEC 3
+// Used in START msg to indicate firmware version, 313 = 3.1.3
+#define VERSION_ID 313
 
 // Hardware pin defines
 // Enable SRF

  // Wait for it to be initialised
  delay(200);

  // Send STARTED message if successful or ERROR if not able to set SleepMode 3.
  // set up sleep mode 3 (low = awake)
  uint8_t val;
  while ((val = setSRFSleep()) != 5)
  {
    LLAP.sendInt("ERR",val); // Diagnostic
    delay(5000);	// try again in 5 seconds
  }

  // Repeat "STARTED" message 5 times to alert
  for(int i=0;i<5;i++) {
    LLAP.sendMessage("STARTED");
    delay(1000);
  }

}

// The main loop, we basically wake up the SRF, take a reading, transmit reading then go back to sleep
void loop() {

  // Determine if we need to send a battery voltage reading yet 
  if( --batteryCountDown <= 0 ) {
    int mV = readVcc();
    LLAP.sendIntWithDP("B", mV, 3 );
    batteryCountDown = BATTERY_READ_INTERVAL;
  } 

  // Start Ultrasonic distance reading
  uint16_t cm = getRange();
  // Send temperature reading
  if( tempSensorFound ) {
    int latestTemp = (int)(latestTemperature * 100);
    LLAP.sendIntWithDP( "T", latestTemp, 2);
  }
  // Uncompensated distance
//  LLAP.sendInt( "D", lastUncompDistance);
  
  // Send reading n times to make sure it gets through
  for(int n = 0; n < 1; n++ ) {
    if( cm > 17 ) {
      LLAP.sendInt( "D", cm );  // Send uncompensated distance
    } 
    else if( cm == 0 ) {
      LLAP.sendMessage( "DMax" );
    } 
    else {
      LLAP.sendMessage( "DErr" );
    }
  }

  // Short delay to allow reading to be sent then sleep
  delay(50);
  pinMode(SRF_SLEEP, INPUT);                // sleep the radio
  LLAP.sleep(WAKE_INT, RISING, false);      // sleep until woken on pin 2, no pullup (low power)
  pinMode(SRF_SLEEP, OUTPUT);               // wake the radio

}
// That's all folks


  int8_t arraysize = 9; // quantity of values to find the median (sample size). Needs to be an odd number
  //declare an array to store the samples. not necessary to zero the array values here, it just makes the code clearer
  uint16_t rangevalue[] = { 
    0, 0, 0, 0, 0, 0, 0, 0, 0           };

  float temperature = 0.0;
  digitalWrite(SENSOR_ENABLE, HIGH);
