rc-gateway/window1/window1.ino

59 lines
944 B
C++

#include <TinyPower.h>
#include <Tiny.h>
// Pins
#define SWITCH 0
#define SENDER 2
#define CONTROLLER 4
#define SENSOR_ID 1
ContactSensor sensor = ContactSensor(SENSOR_ID, SENDER);
int counter = 0;
bool currentState;
void setup() {
pinMode(SWITCH, INPUT_PULLUP);
pinMode(CONTROLLER, OUTPUT);
digitalWrite(CONTROLLER, LOW);
sensor.setup();
updateState();
sensor.sendStateAndVoltage(readState());
TinyPower::setup();
TinyPower::enableWdt(WDTO_8S);
}
void loop() {
TinyPower::sleep();
}
void updateState() {
currentState = readState();
}
byte readState() {
return digitalRead(SWITCH);
}
ISR(PCINT0_vect) {
sensor.sendStateAndVoltage(readState());
}
ISR(WDT_vect) {
bool state = readState();
if (state != currentState) {
sensor.sendStateAndVoltage(readState());
updateState();
return;
}
counter++;
if (counter % 76 == 0) {
sensor.sendStateAndVoltage(readState());
counter = 0;
}
}