Arduino mega watchdogtimer issue

Hey guys i have been trying to figure out problems with watchdog timer in arduinomega R3(board resets forever.I have googled and found out many suggesting bootloader change but before I do that is there any issue in my code


#define AVR8_WDT_PRESCALER_16MS     ((unsigned char)0x00)
#define AVR8_WDT_PRESCALER_32MS     ((unsigned char)0x01)
#define AVR8_WDT_PRESCALER_64MS     ((unsigned char)0x02)
#define AVR8_WDT_PRESCALER_125MS    ((unsigned char)0x03)
#define AVR8_WDT_PRESCALER_250MS    ((unsigned char)0x04)
#define AVR8_WDT_PRESCALER_500MS    ((unsigned char)0x05)
#define AVR8_WDT_PRESCALER_1S       ((unsigned char)0x06)
#define AVR8_WDT_PRESCALER_2S       ((unsigned char)0x07)
#define AVR8_WDT_PRESCALER_4S       ((unsigned char)0x20)
#define AVR8_WDT_PRESCALER_8S       ((unsigned char)0x21)
// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin 13 as an output.
  pinMode(13, OUTPUT);
void myAVR8SoftwareReset(void)
    //Configure Watchdog as...
    //  WDTCSR.WDIF   = 1  (just to clear WDT interrupt flag if set)
    //  WDTCSR.WDIE   = 0  (WDT interrupt disabled, so WDT generates system reset on overflow)
    //  WDTCSR.WDE    = 1 (enable WDT)
    //  WDTCSR.WDCE   = 1  (If this bit is present, we need set it to perform changes in WDTCSR register contents)
    //  WDTCSR.WDP3_0 = (one of the prefedined prescalers above, as you want)
    WDTCSR = ((unsigned char)0x98 | AVR8_WDT_PRESCALER_8S); //In this case we established 8s timeout period.

    //Endless loop until watchdog overflow.
    while(1) {}

// the loop function runs over and over again forever
void loop() {
   WDTCSR = (unsigned char)0x10;//WDTCSR.WDE=0 and WDTCSR.WDCE=1 (if present) to                  disable watchdog
  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);   // wait for a second

I’d have to dive into the register definitions to see if you are doing it right or not. Could you try avr/wdt.h?

This is my sketch for testing the WDT:

//  Watchdog Timer Example
#include <avr/wdt.h>

unsigned long ToggleDelay;  //  When this delay grows to longer than the WDT interval the WDT resets the Arduino

const int LEDpin = 13;

void toggle_led()
  digitalWrite(LEDpin, !digitalRead(LEDpin));

void setup()
  ToggleDelay = 1;   // Start with a very short delay
  pinMode(LEDpin, OUTPUT);
  wdt_enable(WDTO_250MS);  // Set watchdog to 1/4 second

void loop()
  toggle_led();   // Blinking goes slower and slower until the WDT causes reset, then starts over.
  ToggleDelay += 5;   // Increase the delay by 5 milliseconds.

Hey johnwasser thanks a lot for your reply, I tried your program the blinking gets slower at one point wdt overflows and resets recursively(evident as led glows steady without blinking ) instead of blinking.I need to find out what the problem is indeed