-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathwakeupKill.ino
92 lines (78 loc) · 2.43 KB
/
wakeupKill.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
void checkWakeupPins() {
pinMode(contactOpenedPin, INPUT);
pinMode(contactClosedPin, INPUT);
pinMode(contactStatusPin, INPUT);
pinMode(wakeButtonPin, INPUT);
if (digitalRead(contactStatusPin)) {
Serial.println("Contact Status is Open");
contactStatusClosed = false;
}
else {
contactStatusClosed = true;
Serial.println("Contact Status is Closed");
}
if (!digitalRead(contactOpenedPin)) {
Serial.println("Contact from Closed to Open");
contactLatchOpen = true;
}
else
contactLatchOpen = false;
if (!digitalRead(contactClosedPin)) {
Serial.println("Contact from Open to Closed");
contactLatchClosed = true;
}
else
contactLatchClosed = false;
if (!digitalRead(wakeButtonPin)) {
Serial.println("Wake Button was pressed");
buttonWasPressed = true;
} else
buttonWasPressed = false;
pinMode(killPowerPin, OUTPUT);
digitalWrite(killPowerPin, HIGH);
delay(100);
digitalWrite(killPowerPin, LOW);
}
void killPower() {
Serial.println("killing power");
checkIfContactChanged();
//could get stuck if you really beat on it
unsigned long waitForLatchStartTime = millis();
while ((!digitalRead(contactClosedPin) || !digitalRead(contactOpenedPin))) {//in case a race condition
if (millis() - waitForLatchStartTime > 1000) {//added this in after finding that sometimes it freezes up in here
Serial.println("latches did not unlatch - trying again");
digitalWrite(killPowerPin, HIGH);
delay(100);
digitalWrite(killPowerPin, LOW);
waitForLatchStartTime = millis();
}
//Serial.println("dying");
}
checkIfContactChanged();
rtcInit(config.timerCountDown, false);//just in case the RTC timer tripped while we were doing something else
checkIfContactChanged();
digitalWrite(ESPlatchPin, LOW);
Serial.println("last breath...");
}
void checkIfContactChanged() {
if (!digitalRead(contactStatusPin) != contactStatusClosed) {//chnaged
contactChanged = true;
rtcInit(config.timerCountDown, false);
Serial.println("contact changed - rebooting");
delay(100);
ESP.restart();
delay(1000);
}
}
void waitForButton() {
Serial.println("waiting for button");
unsigned long buttonHoldStartTime = millis();
while (millis() - buttonHoldStartTime < 3000) {
}
digitalWrite(ESPlatchPin, HIGH);
for (int i = 0; i < 20; i++) {
digitalWrite(LEDpin, !digitalRead(LEDpin));
delay(50);
}
digitalWrite(LEDpin, LOW);//stay on
}