 #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


#define SENSOR_PIN 3
// Using Sleep Mode 1, SLEEP must be HIGH to run
// Using Sleep Mode 2, SLEEP must be LOW to run
// Using Sleep Mode 3, SLEEP must be LOW to run, uses interrupt to wakeup
#define SRF_SLEEP 4
#define WAKE_INT 2
// OneWire bus pin for temperature sensor pin
#define ONE_WIRE_BUS 5

// Software configuration defines
// Number of readings before a battery reading is taken
#define BATTERY_READ_INTERVAL 10

// Polling interval jumper link enumeration values, 1, 5, 10 or 15 minutes
#define FIFTEENMINS 0
#define TENMINS 1
#define FIVEMINS 2
#define ONEMIN 3
#define TWENTYSEC 4

// Define AT commands to set polling intervals defined above
// Order is Fifteen Mins, Ten Mins, Five Mins, 1 Min, 20 seconds
char *pollingIntervalCmds[] = { 
  "ATSDDBBA0",  // 15 Minutes
  "ATSD927C0",  // 10 Minutes
  "ATSD493E0",  // 5 Minutes
  "ATSDEA60",   // 1 Minute
  "ATSD4E20"    // 20s
};

// define startup polling modes
// 10 minutes of 20s interval is 30 wakeups
// After 10 mins this is reset to 10 and interval is 1 minute 
#define NORMAL_POLL 0
#define MED_POLL 1
#define FAST_POLL 2

#define FAST_POLL_COUNT 30
#define MED_POLL_COUNT 10

// Hardware objects

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
