Starting the code back from line 1 after software reset

Hi, I am using Arduino MKR1500 and I am running a watchdog timer. This is just a simple test code I am running(using the code from here). When I run the modified code

#include <WDTZero.h>

WDTZero MyWatchDoggy; // Define WDT  

void setup() {
  int t = 20; //Initialize serial and wait for port to open, max 10 seconds
  Serial.begin(9600);
  while (!Serial) {
    delay(500);
    if ( (t--) == 0 ) break;
  }
 Serial.print("\nWDTZero-Demo : Setup Soft Watchdog at 32S interval"); 
 MyWatchDoggy.attachShutdown(myshutdown);
 MyWatchDoggy.setup(WDT_SOFTCYCLE32S);  // initialize WDT-softcounter refesh cycle on 32sec interval
}

void loop() {
  unsigned int t = 0;


  Serial.print("\nWatchdog Test1 - run 60 seconds with a WDT.clear()\n");
  for (t = 1; t < 60; ++t) {
    MyWatchDoggy.clear();  // refresh wdt - before it loops
    delay(950);
    Serial.print(t);Serial.print(".");
  }

  Serial.print("\n\nWatchdog Test2 - Free run wait for reset @32s\n");
  for (t = 1; t > 0; ++t) {
    delay(950);
    Serial.print(t);Serial.print(".");
    }
    
Serial.print("\nYou will never read this line");

}

void myshutdown()
{

Serial.print("\nWe gonna shut down ! ...");
 NVIC_SystemReset(); 
}

The code shows the following output:

11:40:54.794 -> WDTZero-Demo : Setup Soft Watchdog at 32S interval
11:40:54.794 -> Watchdog Test1 - run 60 seconds with a WDT.clear()
11:40:55.724 -> 1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40.41.42.43.44.45.46.47.48.49.50.51.52.53.54.55.56.57.58.59.
11:41:51.064 -> 
11:41:51.064 -> Watchdog Test2 - Free run wait for reset @32s
11:41:52.003 -> 1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.
11:42:22.285 -> We gonna shut down ! ...

After that the I hear the USB disconnect and connect sound(ensuring the Arduino reset) but after that the code never starts running again and the serial monitor is blank. How do I make it keep counting down and then reset itself after the timer ends and I can see in the serial monitor that counter again?

I have not used a watchdog timer, but be aware that NONE of your program variables are ever reset to the initial values that are there when your program is first loaded. They still contain the values that were there when the timer expired. Perhaps that is causing your problem.
Paul

1 Like

Did you try restarting the serial monitor? I suppose the monitor will not establish the USB connection automatically after it has been lost.

2 Likes

Hi MacroBahner, I never guessed that I had to restart the serial monitor. I always thought it should auto-restart and should start printing the data.
I restarted the serial monitor and there was the output running.
Thanks a ton!

I already had similar problems with a Leonardo/Micro. I suppose this is a general problem with all Arduinos with native USB. When they are resetted the USB connection gets lost and is reconnected. And the serial monitor cannot cope with that.

1 Like

Some terminal programs will reconnect. Under Windows, TeraTerm is the one I'm aware of.

1 Like