Watchdog reset - a very simple sketch - SOLVED

Hi,

I want to reset Arduino (ATmega168) every 8 seconds.
To do this I want to use watchdog interrupt mode not system reset mode (I use system reset mode in the ISR(WDT_vect) function).

After 8 + 4 (wdt_enable(WDTO_4S)) seconds the LED (pin13) starts blinking very quick and reset button doeas not work. I need to cut the power off to restart my Arduino.

Could You please help me to solve this problem?

#include <avr/sleep.h>
#include <avr/wdt.h>

int pinLed=13;

void setup()
{  
  //
  //
  // SET UP WATCHDOG TO INTERRUPT EVERY 8 SECONDS 
  MCUSR &= ~(1<<WDRF);
  WDTCSR |= (1<<WDCE) | (1<<WDE);
  WDTCSR = 1<<WDP0 | 1<<WDP3; /* 8.0 seconds */  
  /* Enable the WD interrupt (note no reset). */
  WDTCSR |= _BV(WDIE);
  //
  //
  //
  
  pinMode(pinLed,OUTPUT);
  Serial.begin(9600);  
}

void loop()
{            
    digitalWrite(pinLed,1);
    delay(1000);
    digitalWrite(pinLed,0);
    delay(1000); 
    Serial.print("blink:");
    Serial.println(millis());
}

ISR(WDT_vect) {
    Serial.println("wdog interr.");    
    //
    //
    // RESET ARDUINO
    wdt_enable(WDTO_4S);
}

I want to reset Arduino (ATmega168) every 8 seconds.

Explain WHY first.

PaulS:

I want to reset Arduino (ATmega168) every 8 seconds.

Explain WHY first.

I need to reset the board when the app has locked up.
First I wanted to check if I can reset the board every 8 seconds... unfortunatelly it does not work.

I need to reset the board when the app has locked up.

In general, it is better to work on the other end of the problem. Why is the application locking up? It shouldn't unless it is running out of memory, waiting on hardware that is not responding, or some other addressable situation.

I have no idea why the code you have doesn't work. Does the interrupt handler get called?

One thing about setup() I notice is that << has a very low precedence. Some parentheses in the code in setup might be needed.

I need to reset the board when the app has locked up.

Would not it be better to try and find the root cause of your application code 'locking up"? Debugging code can be difficult but required in cases like this. Forcing a reset to work around a problem is not a solution but rather a poor hack in my opinion.

Lefty

retrolefty:
Would not it be better to try and find the root cause of your application code 'locking up"? Debugging code can be difficult but required in cases like this. Forcing a reset to work around a problem is not a solution but rather a poor hack in my opinion.

once again... my goal is to reset the board (software-reset).
I want to know what I am doing wrong (this way we can lern something new!)

I've tried this code too:

#include <avr/wdt.h>

void setup()
{  
  wdt_enable(WDTO_4S);
  pinMode(4,INPUT); //pulled down
  Serial.begin(9600);  
}

void loop()
{    
    if (digitalRead(4) == LOW) 
      wdt_reset();       
    
    Serial.print("blink:");
    delay(500);
}

When I put 5v to pin 4 I expect that my board resets.
Unfortunatelly the PIN13 LED starts blinking and I have to cut the power off to restart my board (the reset button does not work).

The problem was the original Arduino bootloader.
...solution: http://wulfden.org/TheShoppe/freeduino/ADABOOT.shtml

Thanks for help.