   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();
+}
+

/*
 * 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();


  }
  Serial.println("Successfully joined TTN");
  okSeq();
}

void okSeq () {
    led_on();
    delay(100);
