Go Down

Topic: Arduino Zero Storing Vars to Flash with "const" (Read 270 times) previous topic - next topic

androidfanboy

Jun 05, 2018, 04:53 am Last Edit: Jun 05, 2018, 05:00 am by androidfanboy
Hi,

From this Adafruit article variables are stored to flash memory simply by using "const". However, how can I store variables in flash since there's no EEPROM function? I want to be able to store values in flash, read them, and change them as needed for future use. However, I can't seem to figure out how to do that. For example, the code below yields an error (in the comment):

Code: [Select]
#if defined(ARDUINO_SAMD_ZERO) && defined(SERIAL_PORT_USBVIRTUAL)
  // Required for Serial on Zero based boards
  #define Serial SERIAL_PORT_USBVIRTUAL
#endif

const int temp = 1.23;

void setup() {
  Serial.print("Stored value: "); Serial.println(temp); // Read from flash
  Serial.print("Address of str $"); Serial.println((int)&temp, HEX);
  temp++; // Error: increment of read-only variable 'temp'
  Serial.print("New value: "); Serial.println(temp); // Read from RAM
}

void loop() {
  
}


How do I properly go about doing the following:
- Initialize the variable "temp" so that I can use it in other places of the sketch
- Read the variable's value (Adafruit's document says the variable can be called without anything special)
- Update the variable's value and store it to flash so that it persists even when the MCU is power cycled.

Would I have to use something like the FlashStorage library?

Thanks!

MartinL

Hi androidfanboy,

As you point out the SAMD21 has no EEPROM.

The solution depends really on how often you intend to write to the variables in question?

The FlashStorage library can be used for up to 10,000 writes before the microcontroller's flash memory starts to wear out.

There are also many small, external flash and EEPROM devices available with I2C and SPI interfaces that allow for in excess of 1,000,000 write cycles.

Normally, I just use a basic 24LC256 32kB EEPROM with an I2C interface, it's cheap and is available in an 8-pin DIP, as well surface mount options. There's also a 25LC256 option for SPI.

Here's a link an article by Hobbytronics on the 24LC256 device: http://www.hobbytronics.co.uk/arduino-external-eeprom.

MartinL

Furthermore, here's a link to an earlier discussion about using EEPROM on the SAMD21: https://forum.arduino.cc/index.php?topic=358648.0.

In reply #7 of this discussion, I attached a zip file (EEPROM.zip) that contains two small libraries that are modified versions of EEPROM and EEPROM_Anything libraries for use with the 24LC256.

These libraries allow you to read or write any data type or structure to and from the EEPROM, as shown in the example code.

androidfanboy

Thanks, but since I'll only be writing to flash once in a while I think I'll just use the library I referred to.

Go Up