Retained Memory not able to build

Hello,

I was hoping to add the setting to retain a variable for a counter to a project , so when power is lost the count is not reset - thought about trying to save to Flash , but didn’t want to wear it out counting pulses.
Anyway:
If I add the code to use retained memory the project wont build, so I made a new project and just copied the example code in it, and I get the same problem,

Example Code trying to use as reference -

// EXAMPLE USAGE
STARTUP(System.enableFeature(FEATURE_RETAINED_MEMORY));

retained int value = 10;

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

void loop() {
    Serial.println(value);
    value = 20;
    Serial.println(value);
    delay(100); // Give the serial TX buffer a chance to empty
    System.sleep(SLEEP_MODE_DEEP, 10);
    // Or try a software reset
    // System.reset();
}

/* OUTPUT
*

  • 10
  • 20
  • DEEP SLEEP for 10 seconds
  • 20 (value is retained as 20)
  • 20

*/

Results from Particle Build -
vbat.cpp:4:1: error: ‘retained’ does not name a type
void loop();
^

vbat.cpp: In function ‘void loop()’:
vbat.cpp:11:20: error: ‘value’ was not declared in this scope
void setup() {
^

make[2]: *** […/build/target/user/platform-0-lvbat.o] Error 1
make[1]: *** [user] Error 2
make: *** [main] Error 2
Error: Could not compile. Please review your code.

Thanks for any help, I am very new to this type of code .

How are you building the project? If it’s in the Web IDE be sure to have selected 0.4.6 as the firmware version.

  • Click the “devices” draw then the arrow to the right of your currently selected device. That will reveal a dropdown allowing you to select the version.

Also be sure you’re building for a Photon - this isn’t supported on the Core.

1 Like

I double checked that the Photon was selected and verified 0.4.6 was the firmware in the Web IDE, and Bamm, it worked !

Thank you,

1 Like

I was able to play with the retained memory feature, in the project I was messing with - I think I was hoping for it to be like a NVRAM back up, seems like it’s not . From what I read : I think it only holds the variable for things like a soft power off or reset type of events , not able to keep the variable if I loose power from device. ? ( I have a cr2032 between VBAT and ground)

Is there any other way to save a variable from resetting after a power cycle , I was hoping not to involve external mem chip , the variable is something that will increase from pulses counted , and can be reset from a input button . The pulses could increment as fast as 1 per 25ms in random spurts , and I think that saving the counter over and over might damage the Flash (from what I have read)

Any suggestions ? Thanks for info

I don’t know if you have seen the post , but I remember seeing one which said you must make your photon go into the deep sleep at least once to make the retained ram work.
The battery backup ram should be like normal ram without any limits to the amount of writes , it should be only the flash and eeprom with that probmem of limited writes.

What do you mean by this?
Do you mean loosing supply power but still got juice in your battery or do you mean when your backup battery is fully drained too?

Sound like one of my slight English malfunctions, Just replace the “from” to a “to the”

Do you mean loosing supply power but still got juice in your battery or do you mean when your backup battery is fully drained too?

Sorry but I would assume when someone says they have connected a battery , that the battery still got juice , Surely no one says I have connected a dead battery and the item doesn`t work

Well then maybe someone less dim-witted than me might have to answer

BTW: Are you @hermanrock (whom I quoted) or @peter_a, who answered the post containing the quoted sentence?
And I’d guess @hermanrock seems to have seen the post about DEEP_SLEEP since he’s got this in his initial code

  ...
  System.sleep(SLEEP_MODE_DEEP, 10);
  ...

@ScruffR

seems to have seen the post about DEEP_SLEEP since he’s got this in his initial code

oops !!!

Well then maybe someone less dim-witted than me might have to answer

Sorry !!!

1 Like

The need for a sleep with retained variables will be fixed in 0.4.7.

The backup ram is like NVRAM - it will retain the contents so long as there is power supplied to either VIN or VBAT. For permanently persisted data you can use the EEPROM library. This is explained in more detail in the docs.

1 Like

I had an instance were the Ram was saved and couldn’t figure out
what I did that made it work. I must of missed that the deep sleep is a
necessary line of code for this to work. I ended up removing that line at some point after I copied some of the example code back to the project I have been trying to get this feature to work in.

I look forward to the 0.4.7 firmware!

Thank you again for the information.