Go Down

Topic: Persisting variable when going "low power"/sleep (watchdog timer) (Read 1 time) previous topic - next topic

jtlns

Hi all,

I'd like to create a battery powered Arduino that sleeps most of the time (to preserve the battery life) and that wakes up like every couple of minutes to do some measurements. To accomplish this I found the Rocketscream Low Power library (https://github.com/rocketscream/Low-Power) that can put the Arduino in "sleep"/power down mode for 8 seconds using the following code:

Code: [Select]
#include "LowPower.h"

void setup()
{
    // No setup is required for this library
}

void loop()
{
    // Enter power down state for 8 s with ADC and BOD module disabled
    LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF); 
   
    // Do something here
    // Example: Read sensor, data logging, data transmission.
}


My issue is that the 8 second sleep period is not long enough. So I'd like to keep track of how many times the sleep has been executed, for example using the following code:

Code: [Select]
int i = 0;
void loop()
{
    if(i > 10) {
        // do measurements
        i = 0;
    }
    else {
        i++;
    }

    LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);     
}


Obviously the code snippet will not work since i will be reset to 0 every time the Arduino wakes up. So my question is if there is a way to persist the value of i, or another technique to keep track of how many times the loop has been executed in this scenario.

Thanks a lot!
Jan

Nick Gammon

Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

jtlns

Thanks a lot Nick, that would be is awesome! (and very easy/convenient)

I'm going to try this asap.
J

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy