saving data to flash

Is there a way to save some data to the flash memory area so that it will persist across power cycles? In my application, I need to specify the resolution in cps of quadrature encoders. When that value changes via a command from the serial port when the user connects the device to a new encoder, I would like to be able to retrieve the new value next time the device is powered on.

Don Leuenberger

Hi Don,
There is a little bit of EEPROM memory available where you can store stuff which won’t be lost when you turn your device off. (Just 512 bytes, but should be fine for the use you describe.)

See the following page on the playground for the growing documentation on this:

Here, quickly, are the relevant bits of code.

#include <avr/eeprom.h>

//location in eeprom to read/write to.  0-511.
unsigned int addr = 0;        

//To write...
byte val_to_store = 57;
eeprom_write_byte((unsigned char *) addr, val_to_store);

//To read back...
byte val_retrieved = eeprom_read_byte((unsigned char *) addr);

Values stored in this area will also survive the download of new programs to the arduino so long as you’re using the standard arduino bootloader (which doesn’t touch the eeprom, currently).

I’d prefer storing this kind of “high-score” information in the eeprom rather than in the flash, as you’re never sure how much space you’re going to have left, if any, in the flash. Note that you get around 100,000 writes to the eeprom, which is better than to the flash (1/10th of that), but still a finite number capable of biting you…

Hope this solution fits.

Perfect! That's exactly what I need. Apologies for forgetting to look at the Playground for an answer but thank you very much.