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);
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;
}
}

View File

@ -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;
}
}