有IWDG例子:
[C++] 纯文本查看 复制代码 #include
const int buttonPin = 2;
const int ledPin = LED_BUILTIN;
static int default_buttonState = LOW;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
if (IWatchdog.isReset(true)) {
// LED blinks to indicate reset
for (uint8_t idx = 0; idx < 5; idx++) {
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
}
}
// Read default state of the pushbutton
default_buttonState = digitalRead(buttonPin);
// Init the watchdog timer with 10 seconds timeout
IWatchdog.begin(10000000);
// or with a 2 seconds window
// IWatchdog.begin(10000000, 2000000);
if (!IWatchdog.isEnabled()) {
// LED blinks indefinitely
while (1) {
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
}
}
}
void loop() {
// Compare current button state of the pushbutton value:
if (digitalRead(buttonPin) == default_buttonState) {
digitalWrite(ledPin, LOW);
} else {
digitalWrite(ledPin, HIGH);
// Uncomment to change timeout value to 6 seconds
//IWatchdog.set(6000000);
// Reload the watchdog only when the button is pressed
IWatchdog.reload();
}
}
|