How to reset Arduino when it hangs

Hello,
I'm using Duemilanove in my project. When the module starts, everything goes weill. But it always hangs after running for a while.
I want the device can restart automatically when it hangs. I think the watch dog functionality can meet my requirement. Does anybody know where can I get the sample code regarding it?

Regards,
Jeffrey

Perhaps if you posted your sketch we might spot the mistake that causes the hang.

I don't think there's an example for the WDT. :\

This will give you a WDT interrupt every ~16mS.

ISR (WDT_vect) {
 asm ("wdr");  // reset the watchdog
}


void setup () {
  WDTCSR = (1<<WDIE);
}

To stop the interrupt put the asm ("wdr"); in your main loop.

What you do on entering the ISR is another story. You can also set a longer time (up to 8s) and have the WD to reset instead of interrupt.

However I'd be looking into the problem a lot more rather than curing the symptoms.

As mentioned, post the code.


Rob