Go Down

Topic: IDE 1.0 and sleep question (Read 321 times) previous topic - next topic

fbriggs4

I have a sketch that I developed in IDE 0023 and I have transitioned it to IDE 1.0.1. I have it working now but I found a discrepancy that I don't understand and hope that someone could help explain. I trimmed out all my code except for the area that gives me trouble. My code below is a simple for loop that prints a variable and right after the loop I put the unit to sleep. The loop should run 25 times but it only prints 20 times and then goes to sleep. If you uncomment the delay between the loop and going to sleep it completes all 25 print statements and then goes to sleep. I did not encounter this in IDE 0023 and it took me a while to figure out what was going on. It is a simple thing to add a small delay but shouldn't the loop complete first before it moves to the next thing? Just to clarify I am running the breadboard boot loader using the internal 8mhz clock with an external 32khz crystal for timer2 when in sleep mode. Thanks in advance for any help explaining why I need the delay.

Code: [Select]


#include <avr/sleep.h> //Needed for sleep_mode

int time[30] = {
  2,5400,6300};


void setup() {


  Serial.begin(9600);
  Serial.println("this is setup");

  for(int i = 1; i < 26; i++){
    int j = i * 2;
    Serial.print("time ");
    Serial.println(i);
    Serial.println(time[i]);
   
  }
 
  //delay(200); //with this commented out it only prints until 20. With the delay it prints everything.

 
  set_sleep_mode(SLEEP_MODE_PWR_SAVE);
  sleep_enable();


  //Power down various bits of hardware to lower power usage
  ADCSRA &= ~(1<<ADEN); //Disable ADC
  ACSR = (1<<ACD); //Disable the analog comparator
  DIDR0 = 0x3F; //Disable digital input buffers on all ADC0-ADC5 pins
  DIDR1 = (1<<AIN1D)|(1<<AIN0D); //Disable digital input buffer on AIN1/0


  //Setup TIMER2
  TCCR2A = 0x00;
  TCCR2B = (1<<CS22)|(1<<CS21)|(1<<CS20); //Set CLK/1024 or overflow interrupt every 8s
  ASSR = (1<<AS2); //Enable asynchronous operation, 32kHz xtal needed
  TIMSK2 = (1<<TOIE2); //Enable the timer 2 interrupt

  sei(); //Enable global interrupts


}


void loop(){


  sleep_mode(); //Stop everything and go to sleep.


}



PaulS

Prior to 1.0, serial output was blocking. With 1.0, serial output is buffered, and output as time permits.

You are going to sleep before the output has completed. If you had code to wake up regularly, you should see serial data resume when you woke up.

The Serial.flush() function now blocks until the last character has shifted into the output register (not until it has been sent). You might need to use that (and a microscopic delay to get that last byte out) to get all the data out before napping.

fbriggs4

that makes sense, thanks for explaining it to me.

Go Up