7
7
#define COMMAND_MILLIS 129
8
8
#define COMMAND_LASTWATCHDOGREBOOT 130
9
9
#define COMMAND_WATCHDOGREBOOTCOUNT 131
10
+ #define COMMAND_LASTWATCHDOGPET 132
11
+ #define COMMAND_LASTPETATBITE 133
10
12
#define COMMAND_WATCHDOGPETBASE 200
11
13
12
14
// ---- STATE ----
13
15
volatile long receivedValue = 0 ;
14
16
volatile long dataToSend = -1 ;
15
- unsigned long lastWatchdogPet = 0 ;
16
- unsigned long lastWatchdogReboot = 0 ;
17
+ volatile unsigned long lastWatchdogPet = 0 ;
18
+ volatile unsigned long lastWatchdogReboot = 0 ;
19
+ volatile unsigned int lastTimeoutScale = 0 ;
20
+ volatile unsigned long lastPetAtBite = 0 ;
17
21
18
- unsigned long watchdogTimeout = 200 ;
19
- unsigned int rebootCount = 0 ;
20
- unsigned long timeLastPrinted = 0 ;
22
+ volatile unsigned long watchdogTimeout = 200 ;
23
+ volatile unsigned int rebootCount = 0 ;
24
+ volatile unsigned long timeLastPrinted = 0 ;
21
25
22
26
// forward declarations
23
27
void receiveEvent (int howMany);
@@ -54,11 +58,15 @@ void loop() {
54
58
55
59
if ((now - lastWatchdogPet) > watchdogTimeout * 1000 ) {
56
60
rebootCount++;
61
+ // Serial.println("REBOOT!!");
57
62
// timeout -> toggle REBOOT_PIN low then high
58
63
digitalWrite (REBOOT_PIN, LOW);
59
64
delay (100 );
60
65
digitalWrite (REBOOT_PIN, HIGH);
66
+ lastPetAtBite = (now - lastWatchdogPet)/1000 ;
61
67
lastWatchdogPet = now; // reset watchdog timer
68
+ lastWatchdogReboot = now;
69
+
62
70
}
63
71
}
64
72
@@ -94,7 +102,7 @@ void receiveEvent(int howMany) {
94
102
handleCommand (command, value);
95
103
96
104
// Always give master a safe response in case it does a requestFrom
97
- dataToSend = 0 ;
105
+ // dataToSend = 0;
98
106
99
107
} else if (bytesRead > 0 ) {
100
108
// ---- Write to pin ----
@@ -123,8 +131,7 @@ void receiveEvent(int howMany) {
123
131
124
132
// ---- Command handler ----
125
133
void handleCommand (byte command, long value) {
126
- //
127
- Serial.print (" command: " );
134
+ // Serial.print("command: ");
128
135
Serial.println (command);
129
136
switch (command) {
130
137
case COMMAND_REBOOT:
@@ -144,13 +151,25 @@ void handleCommand(byte command, long value) {
144
151
dataToSend = rebootCount;
145
152
break ;
146
153
154
+ case COMMAND_LASTPETATBITE:
155
+ dataToSend = lastPetAtBite;
156
+ break ;
157
+
158
+ case COMMAND_LASTWATCHDOGPET:
159
+ dataToSend = lastWatchdogPet;
160
+ break ;
161
+
147
162
default :
148
163
if (command > 199 && command < 210 ) {
149
164
150
165
byte watchdogTimingIndication = command - COMMAND_WATCHDOGPETBASE;
151
- watchdogTimeout = 1 ;
152
- for (byte i = 0 ; i < watchdogTimingIndication; i++) { // better than pow()
153
- watchdogTimeout *= 10 ;
166
+
167
+ if (lastTimeoutScale != watchdogTimingIndication && watchdogTimingIndication>2 ) {
168
+ watchdogTimeout = 1 ;
169
+ for (byte i = 0 ; i < watchdogTimingIndication; i++) { // better than pow()
170
+ watchdogTimeout *= 10 ;
171
+ }
172
+ lastTimeoutScale = watchdogTimingIndication;
154
173
}
155
174
lastWatchdogPet = millis ();
156
175
}
0 commit comments