#include <TinyPower.h>
#include <Tiny.h>

// Pins
#define SWITCH 0
#define SENDER 2
#define CONTROLLER 4

ContactSensor sensor = ContactSensor(WATER_SENSOR, SENDER);

int counter = 0;

void setup() {
    
  pinMode(SWITCH, INPUT_PULLUP);
  pinMode(CONTROLLER, OUTPUT);
  digitalWrite(CONTROLLER, HIGH);

  sensor.setup();
  sensor.sendStateAndVoltage(readState());

  TinyPower::setup();
  TinyPower::enableWdt(WDTO_8S);
}

void loop() {
  TinyPower::sleep();
}

bool readState() {
  digitalWrite(CONTROLLER, LOW);
  bool state = digitalRead(SWITCH);
  digitalWrite(CONTROLLER, HIGH);
  return state;
}

ISR(PCINT0_vect) {
  sensor.sendStateAndVoltage(readState());
  delay(5000);
  sensor.sendStateAndVoltage(readState());
}

ISR(WDT_vect) {
  counter++;
  if (counter % 220 == 0) {
    counter = 0;
    sensor.sendStateAndVoltage(readState());
    delay(10000);
    sensor.sendStateAndVoltage(readState());
  }
}
