From 1b26db7e9173d368aeb7123fafa02fc241aad1da Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Sun, 20 Jan 2019 01:26:45 +0100 Subject: [PATCH] send state only after window is opened or closed - save battery --- window1/window1.ino | 19 ++++++++----------- window2/window2.ino | 19 ++++++++----------- 2 files changed, 16 insertions(+), 22 deletions(-) diff --git a/window1/window1.ino b/window1/window1.ino index 73e53a5..e79ce67 100644 --- a/window1/window1.ino +++ b/window1/window1.ino @@ -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; } } diff --git a/window2/window2.ino b/window2/window2.ino index c1792d5..3b8e31b 100644 --- a/window2/window2.ino +++ b/window2/window2.ino @@ -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; } }