Serial port problems for Arduino Micro [Solved]

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 - https://forum.arduino.cc/index.php?topic=389768.0
Serial hangs after wake-up (Micro) - https://forum.arduino.cc/index.php?topic=186798.0
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)

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

The problem is not in the Arduino code but on the PC. When the Micro goes to sleep, the PC sees this as device that is disconnected. If you connect it again (the Micro wakes up) an new device is opened on the PC but the Arduino IDE must close the old device connection and opens a new one to communicate with the "new" device.

So there's not a solution? If I want to see what I'm doing I have to manually reset the Serial window?

So there's not a solution? If I want to see what I'm doing I have to manually reset the Serial window?

I would not put it to sleep. As you have an USB connection anyway it doesn't make sense to put the Micro to sleep. If you don't put it to sleep the connection stays active and you don't have the problems. There may be other solutions but then you have to tell us much more about your project.

pylon:
I would not put it to sleep. As you have an USB connection anyway it doesn't make sense to put the Micro to sleep. If you don't put it to sleep the connection stays active and you don't have the problems. There may be other solutions but then you have to tell us much more about your project.

The project needs to be put to sleep because once it's finished it will be completely on it's own, running off of a battery. I only need it connected to a USB right now so I can troubleshoot problems with the program; once the program is finished the board won't be connected to a computer. I was just hoping that there was a way to make my life a little easier

You could use Serial1 and a FTDI (or similar) TTL to USB adapter to debug your program. The adapter will not shut down when the micro sleeps and the USB port will not disappear.

I've actually found a solution, without changing the program. Tera Term is useful for keeping the serial monitor open, even when closing it and opening it again. Tera Term Project Top Page - OSDN - this is where it can be downloaded if anyone else has this problem.