I'm trying to write a simple program that enables the watchdog timer, goes to sleep to save power, wakes up from the sleep via the watchdog and turns on an LED, resets the watchdog counter, and then goes back to sleep for another 8 seconds. The LED (pin 13) on my Nano keep flashing constantly as if the board is being reset every few mS. One I write this code to my Nano I have to power cycle it to program it again, like it's stuck in a loop. Can someone tell me what the issue with my code is?
#include "LowPower.h"
#include <avr/wdt.h>
void setup() {
//watchdog timer with 2 Seconds time out
wdt_disable() ;
pinMode(LED_BUILTIN, OUTPUT); //Pin 13 LED
}
void loop() {
// put your main code here, to run repeatedly:
wdt_enable(WDTO_8S); //Enable wdt to reset after 8 seconds
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF); //sleep to save power
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
wdt_reset(); //clear the wdt to start the count over
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF); //Go to back to sleep to save power
}
Thanks for the link I will take a look. I didn’t want to enable to wdt in the setup because (I thought) that would mean it would be running when it’s sleeping and using more power. In this simple program it doesn’t matter but I aim to apply what I understand here to a larger program and enable/disable the wdt in the middle of the code.
I was using an Arduino Nano and the old bootloader which has a bug where the wdt flag isn't cleared so it just gets stuck. It wasn't my code that was the issue but the fact that I enabled the wdt caused the bug to show up. Switched to an UNo and works fine. Plan on either fixing the bootloader and uploading with the new or the uno bootloader.