radio.sleep() inside while loop or not? [SOLVED]

Started by certeza, May 08, 2015, 12:09:18 PM

certeza

Hello guys,

I need some help here.
In the piece of code below, should the radio.sleep() statement be inside the while loop?
Thnx in advance.

   Serial.flush();
   radio.receiveDone();
   radio.sleep(); 
   while (--sleep_cycles > 0) {
     // Sleep for 8 secs with ADC module and BOD module off
     LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
   }
   sleep_cycles = MAX_SLEEP_CYCLES;

TomWS

Quote from: certeza on May 08, 2015, 12:09:18 PM
Hello guys,

I need some help here.
In the piece of code below, should the radio.sleep() statement be inside the while loop?
Thnx in advance.

   Serial.flush();
   radio.receiveDone();
   radio.sleep(); 
   while (--sleep_cycles > 0) {
     // Sleep for 8 secs with ADC module and BOD module off
     LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
   }
   sleep_cycles = MAX_SLEEP_CYCLES;

I don't use LowPower library, but I am pretty sure you would have radio.sleep outside of the loop.  I have a couple other observations as well:
1. the sleep_cycles variable is initialized AFTER the loop, it should be initialized BEFORE the loop.
2. you will probably lose any data that you receive with the radio.receiveDone() call just before you sleep.

certeza

Thank you for your help.

I was thinking maybe the atmega328 coming out of sleep would activate the radio again.

And I do initialize sleep_cycles before the loop, in setup().
That part was left out of this piece of code.
Here it is being reinitialized at the end of loop().

TomWS

Quote from: certeza on May 09, 2015, 03:22:06 AM
Thank you for your help.

I was thinking maybe the atmega328 coming out of sleep would activate the radio again.

And I do initialize sleep_cycles before the loop, in setup().
That part was left out of this piece of code.
Here it is being reinitialized at the end of loop().
You'll need to refresh sleep_cycles counter each time before you start the loop, so you can't do it in setup(), which is only called once. 

You could put it right before the loop or change the while loop to a for loop as in:
   sleep_cycles = MAX_SLEEP_CYCLES;
   while (--sleep_cycles > 0)  // this will give you MAX_SLEEP_CYCLES-1 since you 'pre' decrement.  Use post decrement to get the correct number of cycles
   {
     // Sleep for 8 secs with ADC module and BOD module off
     LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
   }

or...
   
   for(sleep_cycles = MAX_SLEEP_CYCLES; sleep_cycles > 0; sleep_cycles--)   // this will give you the correct number of cycles
   {
     // Sleep for 8 secs with ADC module and BOD module off
     LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
   }


The radio won't get activated again until you either send or receive after the radio.sleep() call.

Tom