send state only after window is opened or closed - save battery

This commit is contained in:
Nicu Hodos 2019-01-20 01:26:45 +01:00
parent efc504be30
commit a84afeea61
2 changed files with 16 additions and 22 deletions

View File

@ -9,7 +9,6 @@
ContactSensor sensor = ContactSensor(WINDOW1, SENDER);
volatile int counter = 0;
volatile bool currentState;
volatile bool shouldSend = true;
void setup() {
@ -27,31 +26,29 @@ void setup() {
void loop() {
if (shouldSend) {
shouldSend = false;
sensor.sendStateAndVoltage(readState());
sensor.sendStateAndVoltage(digitalRead(SWITCH));
}
TinyPower::sleep();
}
byte readState() {
return digitalRead(SWITCH);
}
ISR(PCINT0_vect) {
shouldSend = true;
wdt_reset();
TinyPower::enableWdt(WDTO_8S);
counter = 0;
}
ISR(WDT_vect) {
bool state = readState();
if (state != currentState) {
counter++;
if (counter == 1) {
shouldSend = true;
currentState = state;
return;
}
counter++;
if (counter % 225 == 0) {
if (counter % 75 == 0) {
shouldSend = true;
}
if (counter >= 225) {
TinyPower::disableWdt();
counter = 0;
}
}

View File

@ -8,7 +8,6 @@
ContactSensor sensor = ContactSensor(WINDOW2, SENDER);
volatile int counter = 0;
volatile bool currentState;
volatile bool shouldSend = true;
void setup() {
@ -24,31 +23,29 @@ void setup() {
void loop() {
if (shouldSend) {
shouldSend = false;
sensor.sendStateAndVoltage(readState());
sensor.sendStateAndVoltage(digitalRead(SWITCH));
}
TinyPower::sleep();
}
byte readState() {
return digitalRead(SWITCH);
}
ISR(PCINT0_vect) {
shouldSend = true;
wdt_reset();
TinyPower::enableWdt(WDTO_8S);
counter = 0;
}
ISR(WDT_vect) {
bool state = readState();
if (state != currentState) {
counter++;
if (counter == 1) {
shouldSend = true;
currentState = state;
return;
}
counter++;
if (counter % 225 == 0) {
if (counter % 75 == 0) {
shouldSend = true;
}
if (counter >= 225) {
TinyPower::disableWdt();
counter = 0;
}
}