user10923: I am working on the temperature project using arduino to collect temperature. But sometimes arduino hang there so I have to unplug cord and plug it back in to restart the arduino. I do not when it is going to hang up again. So I decide to do some research on the internet but no luck for that. Here is the article I read about resetting arduino code with watchdog timer library.(unfortunately, it only work for arduino uno) If someone has any idea how to reset arduino mega 2560 code. Please help !!

user10794: I tried Gerben's suggestion with a display:
#include <avr/wdt.h>

void setup ()
  {
  Serial.begin (115200);
  Serial.println ();
  Serial.println (F("Code starting ..."));
  wdt_enable(WDTO_8S);
  }  // end of setup

void loop()
{
//   wdt_reset();// make sure this gets called at least once every 8 seconds!
}

That does indeed print "Code starting ..." every 8 seconds or so.

But sometimes arduino hang there so I have to unplug cord and plug it back in to restart the arduino. 

You are better off working out why. There is probably a bug. Fix the bug rather than just restarting the Arduino. 

user2881: avr-libc already has the appropriate functions created for you. Just include <avr/wdt.h>.
Sample usage
#include <avr/wdt.h>

void setup()
{
   wdt_enable(WDTO_8S);
   ...
}

void loop()
{
   wdt_reset();// make sure this gets called at least once every 8 seconds!
   ...
}


10923: <p>I am working on the temperature project using arduino to collect temperature. But sometimes arduino hang there so I have to unplug cord and plug it back in to restart the arduino. I do not when it is going to hang up again. So I decide to do some research on the internet but no luck for that. Here is <a href="http://sysmagazine.com/posts/189744/" rel="nofollow">the article</a> I read about resetting arduino code with watchdog timer library.(unfortunately, it only work for arduino uno) If someone has any idea how to reset arduino mega 2560 code. Please help !!</p>
 10794: <p>I tried Gerben's suggestion with a display:</p>

<pre class="lang-C++ prettyprint-override"><code>#include &lt;avr/wdt.h&gt;

void setup ()
  {
  Serial.begin (115200);
  Serial.println ();
  Serial.println (F("Code starting ..."));
  wdt_enable(WDTO_8S);
  }  // end of setup

void loop()
{
//   wdt_reset();// make sure this gets called at least once every 8 seconds!
}
</code></pre>

<p>That does indeed print "Code starting ..." every 8 seconds or so.</p>

<blockquote>
  <p>But sometimes arduino hang there so I have to unplug cord and plug it back in to restart the arduino. </p>
</blockquote>

<p>You are better off working out <strong>why</strong>. There is probably a bug. Fix the bug rather than just restarting the Arduino. </p>
 2881: <p>avr-libc already has the appropriate functions created for you. Just include <a href="http://www.nongnu.org/avr-libc/user-manual/group__avr__watchdog.html" rel="nofollow"><code>&lt;avr/wdt.h&gt;</code></a>.</p>

<p>Sample usage</p>

<pre><code>#include &lt;avr/wdt.h&gt;

void setup()
{
   wdt_enable(WDTO_8S);
   ...
}

void loop()
{
   wdt_reset();// make sure this gets called at least once every 8 seconds!
   ...
}
</code></pre>
