Deep sleep problem

Hi,

below is whole code. Im getting into low power. Like that code will run smoot and print "test" every ~2 sec.

but when you remove that 20ms delay then it get weird. Serial print doesnt really work etc..

Anybody have idea?

Running on fresh and empty arduino nano. I tried another board also same thing..

void setup() {
  Serial.begin(9600);

  //SETUP WATCHDOG TIMER
  WDTCSR = (24);//change enable and WDE - also resets
  WDTCSR = (7);//prescalers only - get rid of the WDE and WDCE bit
  WDTCSR |= (1 << 6); //enable interrupt mode

  //Disable ADC - don't forget to flip back after waking up if using ADC in your application ADCSRA |= (1 << 7);
  ADCSRA &= ~(1 << 7);

  //ENABLE SLEEP - this enables the sleep mode
  SMCR |= (1 << 2); //power down mode
  SMCR |= 1;//enable sleep

}

void loop() {
  __asm__ __volatile__("sleep");//in line assembler to go to sleep
  Serial.println("test");
  delay(20);
}


ISR(WDT_vect) {
  //DON'T FORGET THIS!  Needed for the watch dog timer.  This is called after a watch dog timer timeout - this is the interrupt function called after waking up
}// watchdog interrupt

When you use the serial port it takes some time to write the data.
So if the mcu enters sleep so fast, the uart could have not enough time for writing it and the you can't receive it.
Writting 4 bytes at 9600 baud takes 4ms aprox.

How it's possible to go back to loop start. Is it able to program go forward while serial data is transmitted?

pacoandres:
When you use the serial port it takes some time to write the data.
So if the mcu enters sleep so fast, the uart could have not enough time for writing it and the you can't receive it.
Writting 4 bytes at 9600 baud takes 4ms aprox.

Thank you!

If you want to print text just before you put the micro to sleep then after the Serial.print() put this command;

Serial.flush();

This will wait till the text has actually been printed before continuing.