I want to trigger a soft reset on my Arduino Leonardo, and after some searching settled on using the watchdog timer, as it seemed to be the easiest and cleanest way to do it.
However, I don't think it works as it should. It seems to work fine on the following example:
#include <avr/wdt.h>
int outputPin = LED_BUILTIN;
void setup() {
MCUSR = 0;
wdt_disable();
pinMode(outputPin, OUTPUT);
digitalWrite(outputPin, HIGH);
delay(500);
digitalWrite(outputPin, LOW);
delay(500);
digitalWrite(outputPin, HIGH);
delay(500);
digitalWrite(outputPin, LOW);
delay(1000);
}
void loop() {
wdt_enable(WDTO_15MS);
while (true) {}
}
The LED blinks continuously,.
But for the following example:
#include <avr/wdt.h>
void setup() {
MCUSR = 0;
wdt_disable();
Serial.begin(9600);
while (!Serial);
Serial.println("Hello");
}
void loop() {
wdt_enable(WDTO_15MS);
while (true) {}
}
"Hello" only gets printed once (where I would expect it to arrive continuously).
I tried increasing the watchdog timeout to 2 seconds, but that didn't solve the issue.
WDTO_15MStoWDTO_2Sin the above example only causes the final state (Lon,RXblinking) to set in 2 seconds later, unfortunately it didn't solve the problem.void (*RESET)() = NULL;and then call the function when wanted, e.g.RESET();. Please note that all hardware registers are not reset. The sketch setup should not assume a hardware register state.pinModefrom my original example, and that solved that specific issue. However, for the (now added) Serial example it still doesn't work.