use flag inside interrupt and move send logic outside, into the loop
This commit is contained in:
parent
bd36cfed2d
commit
e8666d1ebe
@ -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,19 +20,16 @@ void setup() {
|
||||
|
||||
sensor.setup();
|
||||
|
||||
updateState();
|
||||
sensor.sendStateAndVoltage(readState());
|
||||
|
||||
TinyPower::setup();
|
||||
TinyPower::enableWdt(WDTO_8S);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
TinyPower::sleep();
|
||||
if (shouldSend) {
|
||||
shouldSend = false;
|
||||
sensor.sendStateAndVoltage(readState());
|
||||
}
|
||||
|
||||
void updateState() {
|
||||
currentState = readState();
|
||||
TinyPower::sleep();
|
||||
}
|
||||
|
||||
byte readState() {
|
||||
@ -39,7 +37,7 @@ byte readState() {
|
||||
}
|
||||
|
||||
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);
|
||||
shouldSend = true;
|
||||
currentState = state;
|
||||
return;
|
||||
}
|
||||
counter++;
|
||||
if (counter % 225 == 0) {
|
||||
sensor.sendStateAndVoltage(readState());
|
||||
shouldSend = true;
|
||||
counter = 0;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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,19 +17,16 @@ void setup() {
|
||||
|
||||
sensor.setup();
|
||||
|
||||
updateState();
|
||||
sensor.sendStateAndVoltage(readState());
|
||||
|
||||
TinyPower::setup();
|
||||
TinyPower::enableWdt(WDTO_8S);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
TinyPower::sleep();
|
||||
if (shouldSend) {
|
||||
shouldSend = false;
|
||||
sensor.sendStateAndVoltage(readState());
|
||||
}
|
||||
|
||||
void updateState() {
|
||||
currentState = readState();
|
||||
TinyPower::sleep();
|
||||
}
|
||||
|
||||
byte readState() {
|
||||
@ -36,7 +34,7 @@ byte readState() {
|
||||
}
|
||||
|
||||
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);
|
||||
shouldSend = true;
|
||||
currentState = state;
|
||||
return;
|
||||
}
|
||||
counter++;
|
||||
if (counter % 225 == 0) {
|
||||
sensor.sendStateAndVoltage(readState());
|
||||
shouldSend = true;
|
||||
counter = 0;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user