The project I'm working on requires putting an Arduino Micro to sleep, but when I put the board to sleep and wake it back up, the Serial Monitor won't show the new information. I've been trying to get this to work with a simple program:
#include <avr/sleep.h>//this AVR library contains the methods that controls the sleep modes
#include <SoftwareSerial.h>
#define interruptPin 7 //Pin we are going to use to wake up the Arduino
void setup() {
Serial.begin(115200);//Start Serial Comunication
while (!Serial) {
; // wait for serial port to connect. Needed for native USB on Micro
}
delay(100);
pinMode(10,OUTPUT);//We use the led on pin 10 to indecate when Arduino is A sleep
pinMode(interruptPin,INPUT_PULLUP);//Set pin d7 to input using the buildin pullup resistor
digitalWrite(10,HIGH);//turning LED on
Serial.println("ready to start!");
}
void loop() {
delay(5000);//wait 5 seconds before going to sleep
Serial.println("Goodnight!");
delay(500);
Going_To_Sleep();
}
void Going_To_Sleep(){
sleep_enable();//Enabling sleep mode
attachInterrupt(digitalPinToInterrupt(7), wakeUp, LOW);//attaching a interrupt to pin d7
set_sleep_mode(SLEEP_MODE_PWR_DOWN);//Setting the sleep mode, in our case full sleep
digitalWrite(10,LOW);//turning LED off
delay(100); //wait a second to allow the led to be turned off before going to sleep
USBDevice.detach(); //turning off the serial port
sleep_mode(); //activating sleep mode
delay(100);
USBDevice.attach(); //restarting the serial port
delay(100);
Serial.begin(115200);
delay(1000);
//while (!Serial)
// ; // wait for serial port to connect. Needed for native USB
Serial.println("just woke up!");//next line of code executed after the interrupt
digitalWrite(10,HIGH);//turning LED on
}
void wakeUp(){
sleep_disable();//Disable sleep mode
detachInterrupt(4); //Removes the interrupt from pin 7;
}
This program should turn on an LED while printing to screen "ready to start!" then, after 5 seconds, print "Goodnight!" and turn the LED off. After I ground the wire attached to pin 7 (interrupt 4 on the Micro) it should wake up, print to screen "just woke up!" and turn the LED back on, then go back to the 5 second "Goodnight!" loop.
When I run the program I can get it to work partially. The LED always works, turning back on when I run the interrupt. The problem is, every time it wakes up from sleep I have to close the serial monitor window and reopen it. If I'm fast enough I can catch the "just woke up!" but it would be nearly impossible for me to do the same on my full project.
I've looked at other forum posts that have told me how to get this far, but none of them mention having to reset the Serial Window being a fixable problem:
Wake up from sleep using external interrupt - Wake up from sleep using external interrupt - Project Guidance - Arduino Forum
Serial hangs after wake-up (Micro) - [resolved] Serial hangs after wake-up (Micro) - Programming Questions - Arduino Forum
I can turn the USB Port on and off successfully, but I need it to go a step further and actually print to screen without me having to physically turn the monitor on and off.
Any way to fix this problem, or any ideas on how to use some other technique to see what I'm doing that bypass Serial altogether would be appreciated
(Also, this code works perfectly fine with an Arduino Uno, I'd only need to change the pins)