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);
|
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,19 +20,16 @@ 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() {
|
||||||
TinyPower::sleep();
|
if (shouldSend) {
|
||||||
|
shouldSend = false;
|
||||||
|
sensor.sendStateAndVoltage(readState());
|
||||||
}
|
}
|
||||||
|
TinyPower::sleep();
|
||||||
void updateState() {
|
|
||||||
currentState = readState();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
byte readState() {
|
byte readState() {
|
||||||
@ -39,7 +37,7 @@ byte readState() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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,19 +17,16 @@ 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() {
|
||||||
TinyPower::sleep();
|
if (shouldSend) {
|
||||||
|
shouldSend = false;
|
||||||
|
sensor.sendStateAndVoltage(readState());
|
||||||
}
|
}
|
||||||
|
TinyPower::sleep();
|
||||||
void updateState() {
|
|
||||||
currentState = readState();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
byte readState() {
|
byte readState() {
|
||||||
@ -36,7 +34,7 @@ byte readState() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user