Using watchdog to recover from freeze or hangup

I am using the Mega 2560 in an application where I'm running a timer, and I am getting infrequent "freezes" or hang ups.

Few questions to throw out there...
I found a watchdog tutorial for Mega and I will look into using this to sort out why it's freezing, but before I get too far...
My timer counts down from 20-45min down to zero depending on setting. It's on every day, although primarily used during the day.

My ideal scenario to fix this would be use the watchdog and somehow recover from a freeze and not reset. I would like to keep my hardware as-is and not have to add external memory to remember how much time was left or anything like that if possible.

Sketch uses 4% storage space, global variables 17% dynamic memory, leaving 6786 bytes for local variables.

I'll see about blurring out some areas of the code if its needed for sharing... prefer to not post the whole thing. Also it's the longest thing I've ever programmed and I'm a newbie so I probably broke all the best practices out there :slight_smile:

Thanks in advance!

Watchdog isn't used to recover, it's to reset the device.

But let's show some code, links to libraries and schematics (NO Fritzing breadboard mess). Maybe the error is obvious :slight_smile:

Although not the usual use for it, it is possible to use the watchdog as a diagnostic tool to help you find the cause of the freeze. I wrote a library to make this a bit easier:

It's not exactly a silver bullet because interpreting the disassembly can be a bit tricky but I've found this to be quite useful in the past. In fact I discovered the problem I was having was not caused by my code but the code of a library I was using.

The previous posters have alluded to the important fact that the WDT is not there to fix runtime bugs in the system design, but to catch exceptional circumstances with a 'pretty severe' reset of the processor.

You should already 'know' when the watchdog is going to be needed, - rarely to design it as a part of the mainstream functional software .