#include <Arduino.h>
#include <TinyPower.h>

#define CONTROLLER_PIN   	PIN_B4
#define NOTIFICATION_PIN    PIN_B0

#define HOUR_INTERVAL 450							// 450*8s = ~1 hour
#define WAKE_INTERVAL (HOUR_INTERVAL*12)			// ~12 hours


volatile int counter = 0;
volatile bool wakeUp = true;

void setup() {
	pinMode(PIN_B3, INPUT);
	pinMode(CONTROLLER_PIN, OUTPUT);
	digitalWrite(CONTROLLER_PIN, LOW);
	pinMode(NOTIFICATION_PIN, INPUT_PULLUP);
	TinyPower::setup();
	TinyPower::enableWdt(WDTO_8S);
}

void loop() {
	if (wakeUp) {
		wakeUp = false;
		digitalWrite(CONTROLLER_PIN, HIGH);
	}
	TinyPower::sleep(NOTIFICATION_PIN);
}

ISR(WDT_vect) {
	counter++;
	if ((counter % WAKE_INTERVAL == 0)) {
		wakeUp = true;
	}
}

ISR(PCINT0_vect) {
	if (!digitalRead(NOTIFICATION_PIN))
		digitalWrite(CONTROLLER_PIN, LOW);
}