From 2508d2cf5e6fc02a9be8a219332cd228d4c357e8 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Sun, 15 Jul 2018 23:56:37 +0200 Subject: [PATCH] optimize state reading in wdt, reset wdt counter when interrupting --- window1/window1.ino | 10 ++++++---- window2/window2.ino | 8 +++++--- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/window1/window1.ino b/window1/window1.ino index fda26b0..4658d54 100644 --- a/window1/window1.ino +++ b/window1/window1.ino @@ -8,7 +8,7 @@ ContactSensor sensor = ContactSensor(WINDOW1, SENDER); -int counter = 0; +volatile int counter = 0; bool currentState; void setup() { @@ -40,14 +40,16 @@ byte readState() { ISR(PCINT0_vect) { sensor.sendStateAndVoltage(readState()); + wdt_reset(); + counter = 0; } ISR(WDT_vect) { bool state = readState(); if (state != currentState) { - sensor.sendStateAndVoltage(readState()); - updateState(); - return; + sensor.sendStateAndVoltage(state); + currentState = state; + return; } counter++; if (counter % 225 == 0) { diff --git a/window2/window2.ino b/window2/window2.ino index e11d727..fb7e9dd 100644 --- a/window2/window2.ino +++ b/window2/window2.ino @@ -7,7 +7,7 @@ ContactSensor sensor = ContactSensor(WINDOW2, SENDER); -int counter = 0; +volatile int counter = 0; bool currentState; void setup() { @@ -37,13 +37,15 @@ byte readState() { ISR(PCINT0_vect) { sensor.sendStateAndVoltage(readState()); + wdt_reset(); + counter = 0; } ISR(WDT_vect) { bool state = readState(); if (state != currentState) { - sensor.sendStateAndVoltage(readState()); - updateState(); + sensor.sendStateAndVoltage(state); + currentState = state; return; } counter++;