How can I save my arduino mega 2560

I've uploaded a watchdog timer code. When I connected to the battery, it keep resetting and I can't stop it. I tried to upload another code, but it kept resetting and it stopped at the uploading part.

You need to reflash the boot loader.

If you need watchdogs to work, you need a new bootloader.

if you simply need to recover the MEGA so you can used ordinary sketches, you should be able to use the "hold the reset switch down while plugging in the board, and don't release it till exactly the right time during the upload of a new sketch" procedure.

westfw:
If you need watchdogs to work, you need a new bootloader.

if you simply need to recover the MEGA so you can used ordinary sketches, you should be able to use the "hold the reset switch down while plugging in the board, and don't release it till exactly the right time during the upload of a new sketch" procedure.

That depends on the time out value that the WDT is using, if too short then even the manual 'hold the reset button down' doesn't work. The following sketch using a 15 millisecond WDT value will brick any standard bootloader equiped mega1280/2560 board and can only be recoved via reburning the bootloader.

// Test sketch to see if WDT interrupts are handled properly by the bootloader

/*
 Warning Warning Warning this is a semi-destructive test in that
 if your bootloader does not reset WDT interrupts upon starting 
 it will be forced into a tight loop of bootloader starts/WDT resets
 chip/bootloader starts again/lather rinse repeat. One can only
 recover to normal operation by reburning the bootloader with
 a ICSP programmer. If this sketch runs properly on a chip with a WDT aware bootloader,
 you will see continuous serial output on the serial monitor.

Note that current arduino mega boards will fail this test and brick

*/

// Code from arduino forum poster (sorry name not remembered) 1/11/13

#include <avr/wdt.h>

void setup(){
  Serial.begin(57600);
  delay(100);
  Serial.println("Hello world");
  wdt_enable(WDTO_15MS);
}

void loop(){
  Serial.println("I am going to not get stuck..");
  for(int x=0; x<100; x++) {
    wdt_reset();
    x++;
    delay(10);
  }
  wdt_reset();
  Serial.println("I am going to get stuck now..");
  for(int x=0; 1; x++) {
    delay(10);
  }
}

That depends on the time out value that the WDT is using, if too short then even the manual 'hold the reset button down' doesn't work.

Why would that be the case? By holding the RESET switch during powerup, you're preventing the WDT from ever being enabled by the sketch (by preventing the sketch from running), so it doesn't matter what the sketch sets the timeout to. A sketch can't set the fuse that enables the WDT at poweron...

The "problem" is that the WDT is not disabled by a external RESET operation, so once a sketch turns on the WDT, it STAYS on through the usual "upload" resets. But the WDT is still off at power on (unless you change the fuse.)

Nevertheless, I decline to try out your sample sketch :slight_smile:

westfw:

That depends on the time out value that the WDT is using, if too short then even the manual 'hold the reset button down' doesn't work.

Why would that be the case? By holding the RESET switch during powerup, you're preventing the WDT from ever being enabled by the sketch (by preventing the sketch from running), so it doesn't matter what the sketch sets the timeout to. A sketch can't set the fuse that enables the WDT at poweron...

The "problem" is that the WDT is not disabled by a external RESET operation, so once a sketch turns on the WDT, it STAYS on through the usual "upload" resets. But the WDT is still off at power on (unless you change the fuse.)

Nevertheless, I decline to try out your sample sketch :slight_smile:

You are of course correct, I just read past the power off then hold reset while powering back up part. That will allow a new sketch to be reloaded, however it will tax one's skill at timing the release as it took me 3 or 4 attempts before it would take.

Nevertheless, I decline to try out your sample sketch :slight_smile:

Probably a good choice unless you have some time to spare. :wink:

Lefty