Skip to content

Commit e18075b

Browse files
committed
Watchdog timer: update example
1 parent 1220c31 commit e18075b

File tree

1 file changed

+44
-12
lines changed

1 file changed

+44
-12
lines changed

libraries/WDT/examples/WatchdogRefresh/WatchdogRefresh.ino

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,63 @@
44
This sketch shows how to enable the watchdog and
55
refresh the timer to avoid resets
66
7+
Watchdog intervals are limited to 7 timeout periods
8+
the library will select the best clock divisor and timeout
9+
according to the selected wdtInterval.
10+
11+
UNO R4 min wdtInterval 1ms / max wdtInterval 5592ms
12+
Comment out Serial.print() in the setup to make it work with
13+
small intervals
14+
15+
Portenta C33 min wdtInterval 1ms / max wdtInterval 2684ms
16+
717
Circuit:
818
- Portenta C33
19+
- UNO R4
920
*/
1021

1122
#include <WDT.h>
1223

24+
const long ledInterval = 1000;
25+
unsigned long ledMillis = 0;
26+
bool ledState = true;
27+
const long wdtInterval = 2684;
28+
unsigned long wdtMillis = 0;
29+
1330
void setup() {
1431
Serial.begin(9600);
1532
while (!Serial);
1633

17-
wdt_cfg_t p_cfg;
34+
pinMode(LED_BUILTIN, OUTPUT);
1835

19-
p_cfg.timeout = WDT_TIMEOUT_16384;
20-
p_cfg.clock_division = WDT_CLOCK_DIVISION_8192;
21-
p_cfg.window_start = WDT_WINDOW_START_100;
22-
p_cfg.window_end = WDT_WINDOW_END_0;
23-
p_cfg.reset_control = WDT_RESET_CONTROL_RESET;
24-
p_cfg.stop_control = WDT_STOP_CONTROL_ENABLE;
36+
if(wdtInterval < 1) {
37+
Serial.println("Invalid watchdog interval");
38+
while(1){}
39+
}
2540

26-
WDT.begin(p_cfg);
41+
if(WDT.begin(wdtInterval)) {
42+
Serial.print("WDT interval: ");
43+
WDT.refresh();
44+
Serial.print(WDT.getTimeout());
45+
WDT.refresh();
46+
Serial.println(" ms");
47+
WDT.refresh();
48+
} else {
49+
Serial.println("Error initializing watchdog");
50+
while(1){}
51+
}
2752
}
2853

2954
void loop() {
30-
Serial.println("Still Alive...");
31-
// Comment the line above to stop refreshing the watchdog
32-
WDT.refresh();
33-
delay(1000);
55+
if(millis() - ledMillis >= ledInterval) {
56+
digitalWrite(LED_BUILTIN, ledState);
57+
ledState = !ledState;
58+
ledMillis = millis();
59+
}
60+
61+
if(millis() - wdtMillis >= wdtInterval - 1) {
62+
WDT.refresh(); // Comment this line to stop refreshing the watchdog
63+
wdtMillis = millis();
64+
}
65+
3466
}

0 commit comments

Comments
 (0)