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

View File

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