I've been fighting with this for a few days now. I originally thought that I was making mistakes in my code, but after cutting down to the barebones and trying two different libraries that assist with sleeping, I'm pretty sure that the code is not the problem. I've been primarily working with the JeeLib library, which provides a convenient single function way to sleep for an extended period of time. Here's the code in question:
#include <Ports.h>
int led_pin = 12;
ISR(WDT_vect) { Sleepy::watchdogEvent(); } // Setup the watchdog
void setup() {
Serial.begin(57600);
while (!Serial);
Serial.println("in setup");
pinMode(led_pin, OUTPUT);
}
void loop() {
Serial.println("on");
// Turn the LED on and sleep for 5 seconds
digitalWrite(led_pin, HIGH);
Sleepy::loseSomeTime(5000);
Serial.println("off");
// Turn the LED off and sleep for 5 seconds
digitalWrite(led_pin, LOW);
Sleepy::loseSomeTime(5000);
}
When I run this code the LED flickers on and off a couple of times per second. At first I thought this was loseSomeTime returning rapidly, but the Serial output continues to print "in setup" - so it appears that the board is resetting right after the first loseSomeTime call. I also tried the "Low-Power" library (GitHub - rocketscream/Low-Power: Low Power Library for Arduino), and got the same behaviour.
I'm a bit of a loss, but also wondering if powering the Micro over USB is having an effect, or perhaps having it in a breadboard is causing some interrupt or something else to fire. The interrupt thing seems unlikely, because I never attach them. In my more complex set-up, I have an RTC hooked up to D2/D3, which are also external interrupts. But I get the same behaviour without the RTC, so it seems unlikely to be a factor.
Any help here would be appreciated.