Flash memory acces and floats

Hi everyone,

can someone help me to write and read data to/from flash memory?
I have tried to save two arrays into programspace, but my code won’t compile. Do I have to declare the datatype again when I want to write to the array?
Also, can floating point numbers be stored in flash memory?

Thanks a lot for your help!

#include <stdio.h>
#include <math.h>
#include <avr/pgmspace.h>

int PROGMEM SensP[3];
int PROGMEM OffsetP[3];
float Sens[3] = {0}, Offset[3] = {0};

//in the loop:

for(int i = 0; i < 3; i++){
OffsetP PROGMEM = (int)round(Offset*);*
SensP PROGMEM = (int)round(Sens*);*
* }*

Generally speaking data compiled into flash memory is constant and cannot be written to at runtime unless you happen to be a bootloader.

me again,

I removed the declaration with global scope and put just this in the local scope:

prog_int16_t OffsetP[3] = myInteger;

and it compiled.

How can I access and read this array after restarting the arduino?

Hi Andy, thanks for your reply. I need to store some calibration data in non.volatile memory. The reference section says:

"Data in Program Space PROGMEM Store data in flash (program) memory instead of SRAM. There's a description of the various types of memory available on an Arduino board.... " and it describes how to store data there.

I tried the above code and it compiles all right.

So I don't exactly understand what you mean. Would you mind explaining me in more detail ?

I can do one better. Storing the calibration data in EEPROM is a much better choice.

It looks like you are still trying to write to flash at runtime. You can’t do that, you can only access constants. e.g. data declared at global scope like this:

#include <avr/pgmspace.h>

prog_int16_t myProgArray[] PROGMEM={ 0,1,2,3 };

May be read in your code at runtime like this:

for(int i=0;i<=3;i++)
  int16_t myProgInt=pgm_read_word(&myProgArray[i]);

…and yes, CodingBadly’s suggestion to use EEPROM for persistent data that will change at runtime is the best option.

Hi Andy,

I understand what you mean. So the programspace can only be written to when the program is uploaded.

Is there any way to store data at runtime to non-volatile memory at all?

How could I store and retrieve the calibration data?

Is there any way to store data at runtime to non-volatile memory at all?

Yes. That's what EEPROM is for. Here, take this clue by four, and whack yourself a couple of times.

A good place to start reading would be the EEPROM tutorials in the Arduino playground. Here's a link:


thanks for your answers! I am still new to arduino and programming. I will store the data in Eeprom.