Watchdog not working as expectd


I have this code that triggers the ISR at about 1 second and it works fine but inside the ISR I try to change the variable value to 99 but it never changes. The variable is declared as VOLATILE but even doing this it does not work.

#include "avr/wdt.h"

volatile int lp = 0;

ISR(WDT_vect) {
  lp = 99;

void setup() {
    //Se um reset foi causado pelo watchdog.
    if(MCUSR & _BV(WDRF)){
        MCUSR &= ~_BV(WDRF);
        WDTCSR |= (_BV(WDCE) | _BV(WDE));
        WDTCSR = 0x00;
    //Seta watchdog para aproximadamente 1 segundo.
    WDTCSR |= (_BV(WDCE) | _BV(WDE));
    WDTCSR =  _BV(WDIE) | _BV(WDP2) | _BV(WDP1);



void loop() {

	for (int i=0;i<lp;i++) {


You’ve been told about Serial inside an ISR before.

Using serial in an ISR is unreliable.

Did the Arduino voltage go lower than 4.5V?

@aarg I know but for some reason any code I insert in the ISR(WDT_vect) { does not get executed, like lighitn the 13 pin LED. It never gets executed.

Well, post code that does that then.

I never see OK in the serial.

I wouldn't expect you to. The watchdog fires, the processor resets, and the serial printing is aborted.

Anyway, if you don't set the WDIE bit (which you don't) the processor resets, it doesn't call the ISR.

I'll just collect all three of your threads about the watchdog timer together, will I? Wasting time I might have spent answering your questions.

Please do not cross-post. This wastes time and resources as people attempt to answer your question on multiple threads.

Threads merged.

At 7:25 a.m. my time you were told not to use serial prints inside an ISR.

At 10:25 a.m. you make another thread which also uses a serial print inside an ISR.

Then at 10:46 a.m. you make a third thread (Watchgod[1] incompatibility) which also uses serial prints.

Look, if you are going to ignore our answers, at least don’t keep starting new threads. I can feel a temporary ban coming up.

  1. It’s a dog, not a god.

This instructable does a great job of explaining how to set up a timer.

It also does a good job of reminding you not to put Serial calls inside of an ISR, so be ready to hear it again.

Here is a good page that covers using the Arduino WDT properly,

and also how to deal with the bootload lockup problem evident in some Arduino boards.

Seriously, both of your questions took 0.43 seconds to find answers to, according to Google. Please attempt to educate yourself before just wantonly asking for someone to fix your code.

Sorry Nick Gammon, but if you take a close look every question was a different one, but with the same "theme". I was not talking about exactly the same thing.

Yes, and in each of them you continue to ignore the advise given in the last one and you continue putting Serial.print statements in your ISR routines. You are wasting our time. This appears to be a habit with you. Start a thread, get good advise, ignore it, start a new thread. If you're not going to learn, then I'm done with any thread you start.

I would also write a 1 to WDIF just in case. If it is already set when you set WDIE then you'll immediately fire an interrupt you weren't expecting.

Why woudl I need to set the WDIF bit?

I didn't say set it, I said to clear it.

Otherwise if WDIF is set when you set WDIE then it will immediately fire an interrupt that you aren't expecting.

@Delta_G thank you, I understood you wrong, sorry. I see you are right, but for some reason I am not getting the inteerrupt fired as you said. It only fires after one second of inactivity, it's not firing when I set it as you said. I turn on/off the pin 13 LED everytime the ISR is fired but it never fired when I set it, which is against what you said. Am I wrong?

No, you're not wrong. You're thick.

If the WDIF bit isn't set, then it won't fire. I didn't say clear it because it WILL fire. I didn't say anything WILL happen. I said clear it JUST IN CASE. It can't cause you any problem to clear it, but you cannot possibly see every possible scenario to know that it won't ever be sitting there waiting to get you.

clear means set the bit to zero

The datasheet says:

10.9.2 WDTCSR – Watchdog Timer Control Register
Alternatively, WDIF is cleared by writing a logic one to the flag.

It may seem strange but there it is. You write 1 to the bit to clear it. That is not a misprint.

inverse logic? zero is set?

No. Zero is "do nothing".