send state only after window is opened or closed - save battery
This commit is contained in:
parent
efc504be30
commit
a84afeea61
@ -9,7 +9,6 @@
|
||||
ContactSensor sensor = ContactSensor(WINDOW1, SENDER);
|
||||
|
||||
volatile int counter = 0;
|
||||
volatile bool currentState;
|
||||
volatile bool shouldSend = true;
|
||||
|
||||
void setup() {
|
||||
@ -27,31 +26,29 @@ void setup() {
|
||||
void loop() {
|
||||
if (shouldSend) {
|
||||
shouldSend = false;
|
||||
sensor.sendStateAndVoltage(readState());
|
||||
sensor.sendStateAndVoltage(digitalRead(SWITCH));
|
||||
}
|
||||
TinyPower::sleep();
|
||||
}
|
||||
|
||||
byte readState() {
|
||||
return digitalRead(SWITCH);
|
||||
}
|
||||
|
||||
ISR(PCINT0_vect) {
|
||||
shouldSend = true;
|
||||
wdt_reset();
|
||||
TinyPower::enableWdt(WDTO_8S);
|
||||
counter = 0;
|
||||
}
|
||||
|
||||
ISR(WDT_vect) {
|
||||
bool state = readState();
|
||||
if (state != currentState) {
|
||||
counter++;
|
||||
if (counter == 1) {
|
||||
shouldSend = true;
|
||||
currentState = state;
|
||||
return;
|
||||
}
|
||||
counter++;
|
||||
if (counter % 225 == 0) {
|
||||
if (counter % 75 == 0) {
|
||||
shouldSend = true;
|
||||
}
|
||||
if (counter >= 225) {
|
||||
TinyPower::disableWdt();
|
||||
counter = 0;
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,7 +8,6 @@
|
||||
ContactSensor sensor = ContactSensor(WINDOW2, SENDER);
|
||||
|
||||
volatile int counter = 0;
|
||||
volatile bool currentState;
|
||||
volatile bool shouldSend = true;
|
||||
|
||||
void setup() {
|
||||
@ -24,31 +23,29 @@ void setup() {
|
||||
void loop() {
|
||||
if (shouldSend) {
|
||||
shouldSend = false;
|
||||
sensor.sendStateAndVoltage(readState());
|
||||
sensor.sendStateAndVoltage(digitalRead(SWITCH));
|
||||
}
|
||||
TinyPower::sleep();
|
||||
}
|
||||
|
||||
byte readState() {
|
||||
return digitalRead(SWITCH);
|
||||
}
|
||||
|
||||
ISR(PCINT0_vect) {
|
||||
shouldSend = true;
|
||||
wdt_reset();
|
||||
TinyPower::enableWdt(WDTO_8S);
|
||||
counter = 0;
|
||||
}
|
||||
|
||||
ISR(WDT_vect) {
|
||||
bool state = readState();
|
||||
if (state != currentState) {
|
||||
counter++;
|
||||
if (counter == 1) {
|
||||
shouldSend = true;
|
||||
currentState = state;
|
||||
return;
|
||||
}
|
||||
counter++;
|
||||
if (counter % 225 == 0) {
|
||||
if (counter % 75 == 0) {
|
||||
shouldSend = true;
|
||||
}
|
||||
if (counter >= 225) {
|
||||
TinyPower::disableWdt();
|
||||
counter = 0;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user