diff --git a/window1/window1.ino b/window1/window1.ino index 4658d54..73e53a5 100644 --- a/window1/window1.ino +++ b/window1/window1.ino @@ -9,7 +9,8 @@ ContactSensor sensor = ContactSensor(WINDOW1, SENDER); volatile int counter = 0; -bool currentState; +volatile bool currentState; +volatile bool shouldSend = true; void setup() { @@ -19,27 +20,24 @@ void setup() { sensor.setup(); - updateState(); - sensor.sendStateAndVoltage(readState()); - TinyPower::setup(); TinyPower::enableWdt(WDTO_8S); } void loop() { + if (shouldSend) { + shouldSend = false; + sensor.sendStateAndVoltage(readState()); + } TinyPower::sleep(); } -void updateState() { - currentState = readState(); -} - byte readState() { return digitalRead(SWITCH); } ISR(PCINT0_vect) { - sensor.sendStateAndVoltage(readState()); + shouldSend = true; wdt_reset(); counter = 0; } @@ -47,13 +45,13 @@ ISR(PCINT0_vect) { ISR(WDT_vect) { bool state = readState(); if (state != currentState) { - sensor.sendStateAndVoltage(state); - currentState = state; - return; + shouldSend = true; + currentState = state; + return; } counter++; if (counter % 225 == 0) { - sensor.sendStateAndVoltage(readState()); - counter = 0; + shouldSend = true; + counter = 0; } } diff --git a/window2/window2.ino b/window2/window2.ino index fb7e9dd..c1792d5 100644 --- a/window2/window2.ino +++ b/window2/window2.ino @@ -8,7 +8,8 @@ ContactSensor sensor = ContactSensor(WINDOW2, SENDER); volatile int counter = 0; -bool currentState; +volatile bool currentState; +volatile bool shouldSend = true; void setup() { @@ -16,27 +17,24 @@ void setup() { sensor.setup(); - updateState(); - sensor.sendStateAndVoltage(readState()); - TinyPower::setup(); TinyPower::enableWdt(WDTO_8S); } void loop() { + if (shouldSend) { + shouldSend = false; + sensor.sendStateAndVoltage(readState()); + } TinyPower::sleep(); } -void updateState() { - currentState = readState(); -} - byte readState() { return digitalRead(SWITCH); } ISR(PCINT0_vect) { - sensor.sendStateAndVoltage(readState()); + shouldSend = true; wdt_reset(); counter = 0; } @@ -44,13 +42,13 @@ ISR(PCINT0_vect) { ISR(WDT_vect) { bool state = readState(); if (state != currentState) { - sensor.sendStateAndVoltage(state); - currentState = state; - return; + shouldSend = true; + currentState = state; + return; } counter++; if (counter % 225 == 0) { - sensor.sendStateAndVoltage(readState()); - counter = 0; + shouldSend = true; + counter = 0; } }