     led_off();
 }
 
-void wakeUP_RN2483() {
-    Serial1.end();
-    pinMode(PIN_SERIAL1_TX, OUTPUT);
-    digitalWrite(PIN_SERIAL1_TX, LOW);
-    delay(5);
-    digitalWrite(PIN_SERIAL1_TX, HIGH);
-    Serial1.begin(57600);
-    Serial1.write(0x55);
-    delay(100); //wait for the RN2xx3's startup message
-    Serial1.flush();
-}
-
 // the loop routine runs over and over again forever:
 void loop()
 {
-  if (count == 0) {
-     //wakeUP_RN2483();
-    myLora.autobaud();
-    int16_t temp_c;
-    int16_t humidity;
-    int16_t battery;
-    led_on();
-
-    temp_c = int16_t(sht1x.readTemperatureC()*100);
-    humidity = int16_t(sht1x.readHumidity()*100);
-    battery = analogRead(batVoltPin)*4.88;
-
-
-    txBuffer[0] = byte(temp_c>>8);
-    txBuffer[1] = byte(temp_c);
-    txBuffer[2] = byte(humidity>>8);
-    txBuffer[3] = byte(humidity);
-    txBuffer[4] = byte(battery>>8);
-    txBuffer[5] = byte(battery);
-    txBuffer[6] = 0xFE;
-
-    myLora.txBytes(txBuffer, sizeof(txBuffer));
-    led_off();
-    myLora.sleep(128000);
-    delay(100);
-    count = 16;
-  } else {
-    LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
-    count --;
-  }
+  startTime = millis();
+  led_on();
+  myLora.autobaud();
+  led_off();
+  delay(100);
+  led_on();
+  int16_t temp_c;
+  int16_t humidity;
+  int16_t battery;
+
+  temp_c = int16_t(sht1x.readTemperatureC()*100);
+  humidity = int16_t(sht1x.readHumidity()*100);
+  battery = analogRead(batVoltPin)*4.88;
+
+
+  txBuffer[0] = byte(temp_c>>8);
+  txBuffer[1] = byte(temp_c);
+  txBuffer[2] = byte(humidity>>8);
+  txBuffer[3] = byte(humidity);
+  txBuffer[4] = byte(battery>>8);
+  txBuffer[5] = byte(battery);
+  txBuffer[6] = 0xFE;
+
+  myLora.txBytes(txBuffer, sizeof(txBuffer));
+  led_off();
+
+  sleepTime = (SAMPLE_INTERVAL_MS-(millis()-startTime));
+  myLora.sleep(sleepTime);
+  delay(100);
+
+  go_to_sleep();
 }
 
 void led_on()

/*
 * Author: NC Thompson
 * Date: 2017-02-15
 *
 */
#include <rn2xx3.h>
#include <SHT1x.h>
#include "LowPower.h"

//create an instance of the rn2483 library, using the given Serial port
rn2xx3 myLora(Serial1);
#define dataPin  SDA
#define clockPin SCL
#define PIN_SERIAL1_TX PIN1

SHT1x sht1x(dataPin, clockPin);

uint8_t count = 0;

uint8_t txBuffer[7];
int batVoltPin = A5;    // select the input pin for the battery voltage

// the setup routine runs once when you press reset:
void setup()
{
  //output LED pin
  pinMode(13, OUTPUT);
  led_on();

  // Open serial communications and wait for port to open:
  Serial.begin(57600); //serial port to computer

  Serial1.begin(57600); //serial port to radio
  //wakeUP_RN2483();
  // make sure usb serial connection is available,
  // or after 10s go on anyway for 'headless' use of the
  // node.
  while ((!Serial) && (millis() < 10000));

  Serial.println("Startup");

  initialize_radio();

  //transmit a startup message
  myLora.tx("TTN Mapper on TTN Uno node");
  Serial.end();
  led_off();
  delay(2000);
}

void initialize_radio()
{
  delay(100); //wait for the RN2xx3's startup message
  Serial1.flush();


  digitalWrite(13, 0);
}

ISR(WDT_vect)
{
  wdt_reset();
  wdtCount++;
  if(wdtCount<=(WDT_INTERVAL_MS/8000))
  {
    sleep_now();
  }
  else
  {
    pinMode(12, OUTPUT);
    digitalWrite(12, LOW);
  }
}

ISR(INT2_vect)
{
  //wake up if we are in sleep
}

ISR(BADISR_vect)
{
  //unknown interrupt - do nothing, just wake up
}

void enable_wdt_interrupt_8s()
{
  /*
  WDTCSR configuration:
  WDIE = 1 :Interrupt Enable
  WDE = 1  :Reset Enable
  WDP3:0 = 1001 = 8s
  */
  MCUSR &= ~(1<<WDRF);  // because the data sheet said to
  // Enter Watchdog Configuration mode:
  WDTCSR = (1<<WDCE) | (1<<WDE);
  // Set Watchdog settings: interrupte enable, 1001 for timer
  WDTCSR = (0<<WDE) | (1<<WDIE) | (1<<WDP3) | (0<<WDP2) | (0<<WDP1) | (1<<WDP0);
  wdt_reset();
}

void go_to_sleep()
{
  enable_wdt_interrupt_8s();

  ADCSRA &= ~(1 << ADEN);
  power_adc_disable();

  power_timer4_disable();
  power_timer3_disable();
  power_timer1_disable();
  power_timer0_disable();
  power_spi_disable();
  power_usart1_disable();
  power_twi_disable();
  power_usb_disable();

  sleep_now();

  wdt_reset();
  wdt_disable();
  wdtCount=0;

  power_adc_enable();
  ADCSRA |= (1 << ADEN);

  power_timer4_enable();
  power_timer3_enable();
  power_timer1_enable();
  power_timer0_enable();
  power_spi_enable();
  power_usart1_enable();
  power_twi_enable();
  power_usb_enable();
}

void sleep_now()
{
  // mcu sleep
  cli();
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_enable();
  //sleep_bod_disable(); // Only Pico Power devices can change BOD settings through software
  sei();
  sleep_cpu();
  sleep_disable();
}

