Problems with Serial.print after waking from sleep

Hi all,

Was playing around with Nick Gammon's work on AVR sleep modes (Gammon Forum : Electronics : Microprocessors : Power saving techniques for microprocessors - Sketch I near the bottom of the page). I wanted to have a Serial.print statement while the processor is awake, but I think I'm missing something. The serial monitor has gibberish, and I can't figure out why.

Here's the code. Runs on a standard UNO (IDE 1.6.4):

#include <avr/sleep.h>
#include <avr/wdt.h>
#include <avr/power.h>

const byte LED = 13;


void setup () {

  pinMode (LED, OUTPUT);
}

void loop ()
{
  digitalWrite (LED, HIGH);
  delay (50);
  Serial.begin(9600);
  Serial.println("test");
  Serial.end();
  digitalWrite (LED, LOW);
  //pinMode (LED, INPUT);

  // disable ADC
  ADCSRA = 0;

  // clear various "reset" flags
  MCUSR = 0;
  // allow changes, disable reset
  WDTCSR = bit (WDCE) | bit (WDE);
  // set interrupt mode and an interval
  WDTCSR = bit (WDIE) | bit (WDP1) | bit (WDP2);   // set WDIE, and 1 second delay
  wdt_reset();  // pat the dog

  set_sleep_mode (SLEEP_MODE_STANDBY);
  noInterrupts ();           // timed sequence follows
  sleep_enable();

  // turn off brown-out enable in software
  MCUCR = bit (BODS) | bit (BODSE);
  MCUCR = bit (BODS);
  interrupts ();             // guarantees next instruction executed
  sleep_cpu ();


  
  // cancel sleep as a precaution
  sleep_disable();
  
} // end of loop



// watchdog interrupt
ISR (WDT_vect)
{
  wdt_disable();  // disable watchdog
}  // end of WDT_vect

I have attached the serial output (sorry, couldn't figure out how to simply copy/paste).
The first one (test Catest Catest...) is with the Serial.end() statement included.
The second one (WON5) is with the Serial.end() statement commented out.

Many thanks.

Move Serial.begin() into setup and replace Serial.end() with Serial.flush().

Ah, thank you. I knew it had to be something simple!