Not sure if this is the correct main topic, I'm pretty new to the forum (but not new to Arduinos).
Anyway, I'm trying to use the built in wdg of the Mega (Genuine board), and while it works 95% of the time, sometimes it just won't reset the board.
I have lots of external i2c accessories (well sensors and what not) hooked to the board, and they sometimes won't do a correct reading - if any at all - hanging the board, and those are the times when I'd need to reset the board (and the accessories).
Right now the code is simple, I call a wdt_enable(WDTO_4S) in the setup function, and then reset the timer in the begining of the main loop.
But as I've said, it works most of the times.
+1 info is that I have a poor PSU, and I'm feeding 5V from it to an 5V pin of the arudino (this is unfortunately not changeable) - and I think this might play a role in the problem.
The code is already too complicated to debug it where exactly it hangs..
Anyway. Am I right that the only way to make this more reliable is to use an external wdg?
If you have the watchdog enabled, you simply have to clear it regularly within your code before it times out.
If you simply have an infinite construct that just happens to clear the WDT as part of its standard functionality, then the timer will never expire!
There probably a million different guides to watchdog use, but generally, look at your code and identify those few places where the code might get caught ... e.g. waiting and possibly blocking for some critical state to be satisfied... then if the WDT times out it will wrench control back to a WDT reset as a known state to continue from.
Use EEPROM to save critical values during runtime so you can restore your operating environment after the reset.
I would think that if the WDT does not trigger a reset, your loop() keeps on kicking it. So your code is not hanging.
Although a little outside my area of expertise, with multiple devices on the I2C bus (you don't say how many), the internal pull-up resistors might not be strong enough and external pull-up resistors might be required. If you use modules, they might have them already on-board; problem in that case might be that you're overloading the driver in the Mega.