Pages: [1]   Go Down
Author Topic: Persisting variable when going "low power"/sleep (watchdog timer)  (Read 1119 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 1
Posts: 48
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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:
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
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18777
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

That will work. Sleeping isn't the same as resetting.
Logged


Offline Offline
Newbie
*
Karma: 1
Posts: 48
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

I'm going to try this asap.
J
Logged

Pages: [1]   Go Up
Jump to: