Resetting Arduino

Graynomad:
Try increasing one of the delays to 10000 to verify that the WD works.

I have written a small code to test wdt.

#include <avr/wdt.h>

void setup()
{
  Serial.begin(9600);
  wdt_enable(WDTO_8S);
}

void loop()
{
  Serial.println("Time delay 5000");
  delay(5000);
  wdt_reset();
  Serial.println("Time delay 7000");
  delay(7000);
  wdt_reset();
  Serial.println("Time delay 10000");
  delay(10000);
  Serial.println("Time delay 20000");
  delay(20000);
}

It prints:

Time delay 5000
Time delay 7000
Time delay 10000

and hangs (doesn’t print “Time delay 20000”) . If wdt is resetting, then I assume Arduino shouldn’t print “Time delay 10000” rather starts from beginning.

Graynomad:
… if he could detect the problem there’s a there are easy ways to restart the code

Could you please give an example of efficient use of wdt?

Also why do wdt_enable(WDTO_8S); every time, surely you do that once in setup.

Because…

WizenedEE:
If you just want to use the WDT for reset, you should disable it completely in setup (or, ideally, the bootloader) and only enable it when you want to reset. Unless, of course, you want an actual watchdog for your rc car or transformer or whatever.

And yes, I am using Mega R3 board.