Adafruit_SleepyDog icon indicating copy to clipboard operation
Adafruit_SleepyDog copied to clipboard

Serial never initializes after a watchdog system reset on Arduino MKRZERO

Open oriregev opened this issue 6 years ago • 1 comments

Thank you for a great library! 👍

I'm testing the library on an official Arduino MKRZERO and I found the following issue with the library's system reset functionality. Maybe it is more accurate to say that there is a problem in the Basic Usage example, whatever :)

Checkout the following code which is based on the BasicUsage.ino example. The only difference is that I removed the comments, added some blinking led indications, and reduced the number of loop iterations and timeouts in order to accelerate the debugging:

#include <Adafruit_SleepyDog.h>

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);

  // blink 3 times, and leave the led on
  for (int i = 1; i <= 4; ++i) {
    digitalWrite(LED_BUILTIN, LOW);
    delay(500);         
    digitalWrite(LED_BUILTIN, HIGH);
    delay(500);
  }

  Serial.begin(115200);
  while (!Serial) ; // wait for Arduino Serial Monitor (native USB boards)
  Serial.println("Adafruit Watchdog Library Demo!");
  Serial.println();
  
  int countdownMS = Watchdog.enable(4000);
  Serial.print("Enabled the watchdog with max countdown of ");
  Serial.print(countdownMS, DEC);
  Serial.println(" milliseconds!");
  Serial.println();

  Serial.println("Looping ten times while resetting the watchdog...");
  for (int i = 1; i <= 3; ++i) {
    Serial.print("Loop #"); Serial.println(i, DEC);
    delay(1000);
    Watchdog.reset();
  }
  Serial.println();

  Watchdog.disable();
  
  // turn off the led before the watchdog's system reset
  digitalWrite(LED_BUILTIN, LOW);

  countdownMS = Watchdog.enable(2000);
  Serial.print("Get ready, the watchdog will reset in ");
  Serial.print(countdownMS, DEC);
  Serial.println(" milliseconds!");
  Serial.println();
  delay(countdownMS+1000);
}

void loop() {
  Serial.println("You shouldn't see this message.");
  delay(1000);
}

When it runs, the watchdog properly resets the Arduino MRKZERO, but because of the while (!Serial) loop the program hangs (and the blinking sequence stops, leaving the led turned on forever..) I suppose the while (!Serial) is there because you want to wait for the serial port and see all the Serial.println() calls in setup(), but it breaks the logic of the sketch after the reset...

More generally, many sketches follow the advice from the Getting Started with the Arduino MKRZero guide, and add a while (!Serial) at the beginning of setup(), so it might be a good idea to add a clear warning in the example/Readme.

The bigger issue, I think, is that Serial is not properly initialized after the reset, but I don't know if there is anything the library can do about it... Can it?

Thanks!

  • Arduino board: Arduino MKRZERO
  • Arduino IDE version (found in Arduino -> About Arduino menu): 1.8.5

oriregev avatar May 04 '18 08:05 oriregev