Serial print just before sleep_cpu()

I can't seem to get my Serial debugging commands to print correctly before putting the cpu asleep. So they end up printing after it wakes up. I put a 4 second delay right before going to sleep, thinking that would give plenty of time for the serial port to finish printing, but it didn't help. I read one person said they used Serial.flush(), so I added that in there, but with that in there, my interrupt won't even wake the cpu. Not sure why. It's no big deal, it's just debugging serial stuff, but it would be nice to understand it better and get it working properly.

void sleep_now() { // disable ADC ADCSRA = 0; set_sleep_mode (SLEEP_MODE_PWR_DOWN); sleep_enable(); // Do not interrupt before we go to sleep, or the // ISR will detach interrupts and we won't wake. noInterrupts (); if (!systemError) { //if we get an error on the motor or the wireless signal we will shutdown until user intervention attachInterrupt (0, wake, CHANGE); // will be called when pin D2 goes low } Serial.println("Going to sleep"); Serial.flush(); // turn off brown-out enable in software // BODS must be set to one and BODSE must be set to zero within four clock cycles MCUCR = bit (BODS) | bit (BODSE); // The BODS bit is automatically cleared after three clock cycles MCUCR = bit (BODS); // We are guaranteed that the sleep_cpu call will be done // as the processor executes the next instruction after // interrupts are turned on. interrupts (); // one cycle sleep_cpu (); // one cycle delay(100); sleep_disable(); //wake up here

}

Serial needs interrupts

Serial will not work with interrupts turned off. Turn interrupts off after the Serial.flush() and also put a delay(10) between the two so that there's time to send the last character.

Pete

Great! That's really good to know. Thanks.