use flag inside interrupt and move send logic outside, into the loop

This commit is contained in:
Nicu Hodos 2018-12-31 18:08:49 +01:00
parent bd36cfed2d
commit e8666d1ebe
2 changed files with 24 additions and 28 deletions

View File

@ -9,7 +9,8 @@
ContactSensor sensor = ContactSensor(WINDOW1, SENDER); ContactSensor sensor = ContactSensor(WINDOW1, SENDER);
volatile int counter = 0; volatile int counter = 0;
bool currentState; volatile bool currentState;
volatile bool shouldSend = true;
void setup() { void setup() {
@ -19,27 +20,24 @@ void setup() {
sensor.setup(); sensor.setup();
updateState();
sensor.sendStateAndVoltage(readState());
TinyPower::setup(); TinyPower::setup();
TinyPower::enableWdt(WDTO_8S); TinyPower::enableWdt(WDTO_8S);
} }
void loop() { void loop() {
if (shouldSend) {
shouldSend = false;
sensor.sendStateAndVoltage(readState());
}
TinyPower::sleep(); TinyPower::sleep();
} }
void updateState() {
currentState = readState();
}
byte readState() { byte readState() {
return digitalRead(SWITCH); return digitalRead(SWITCH);
} }
ISR(PCINT0_vect) { ISR(PCINT0_vect) {
sensor.sendStateAndVoltage(readState()); shouldSend = true;
wdt_reset(); wdt_reset();
counter = 0; counter = 0;
} }
@ -47,13 +45,13 @@ ISR(PCINT0_vect) {
ISR(WDT_vect) { ISR(WDT_vect) {
bool state = readState(); bool state = readState();
if (state != currentState) { if (state != currentState) {
sensor.sendStateAndVoltage(state); shouldSend = true;
currentState = state; currentState = state;
return; return;
} }
counter++; counter++;
if (counter % 225 == 0) { if (counter % 225 == 0) {
sensor.sendStateAndVoltage(readState()); shouldSend = true;
counter = 0; counter = 0;
} }
} }

View File

@ -8,7 +8,8 @@
ContactSensor sensor = ContactSensor(WINDOW2, SENDER); ContactSensor sensor = ContactSensor(WINDOW2, SENDER);
volatile int counter = 0; volatile int counter = 0;
bool currentState; volatile bool currentState;
volatile bool shouldSend = true;
void setup() { void setup() {
@ -16,27 +17,24 @@ void setup() {
sensor.setup(); sensor.setup();
updateState();
sensor.sendStateAndVoltage(readState());
TinyPower::setup(); TinyPower::setup();
TinyPower::enableWdt(WDTO_8S); TinyPower::enableWdt(WDTO_8S);
} }
void loop() { void loop() {
if (shouldSend) {
shouldSend = false;
sensor.sendStateAndVoltage(readState());
}
TinyPower::sleep(); TinyPower::sleep();
} }
void updateState() {
currentState = readState();
}
byte readState() { byte readState() {
return digitalRead(SWITCH); return digitalRead(SWITCH);
} }
ISR(PCINT0_vect) { ISR(PCINT0_vect) {
sensor.sendStateAndVoltage(readState()); shouldSend = true;
wdt_reset(); wdt_reset();
counter = 0; counter = 0;
} }
@ -44,13 +42,13 @@ ISR(PCINT0_vect) {
ISR(WDT_vect) { ISR(WDT_vect) {
bool state = readState(); bool state = readState();
if (state != currentState) { if (state != currentState) {
sensor.sendStateAndVoltage(state); shouldSend = true;
currentState = state; currentState = state;
return; return;
} }
counter++; counter++;
if (counter % 225 == 0) { if (counter % 225 == 0) {
sensor.sendStateAndVoltage(readState()); shouldSend = true;
counter = 0; counter = 0;
} }
} }