EEprom pre loaded values

I want to preload values to eeprom.
I know the command write, but is there any other way to load values to eerprom once, before the beggining of the code ?

You could write a function to initialise the EEPROM to the values that you want and call it from setup() if it is not already initialised to those values. Use an EEPROM location to hold a value, say 255, indicating that the values held are valid and should not be written again.

If you only want to write the values once and have no chance of them being written again by the main program, just write a small program to write the values to EEPROM, run it, then load and run the main program.

To program a PIC, microcontroller of microchip, you can easily add the values to the program, at the programming software, like the attachement.

So, before the code begin to “run”, for first time, there are already some values stored in memory. Then the program can change them.

I am asking, if there is a similar way to do the same in arduino.

Otherwise i will do what you suggest me. Ιf i understood right, i will write a function to check the status in an eeprom location, and if the value for example is x then write bla…bla otherwise not write. This function will be on the setup().
Correct?

Arduino IDE hasn’t this option.
You can program EEPROM via avrdude -U EEPROM:w: (see help).
It is able to use some other tool like extremeBurner to do this.

Is there a way to define the values and the address in arduino ?

How many bytes do you want to write to the EEPROM ? If you have them in a file it would be the work of only a few minutes to write a program to write them to the EEPROM starting at an address of your choice.

Copy/paste the values from the file into an editor. A bit of find/replace and you have a function to write them. If you need to do this morean a few times you could even write a macro in Word to convert the values in the file into a function.

I will need around 40 bytes.
So i am going to use it like this:

#include<EEPROM.h>
void setup()
{

if (EEPROM.read (1) == 0)
{EEprom.write bla…bla}
}

Is it correct?

Is it correct?

It would work,

I have a fleshed out version here: http://forum.arduino.cc/index.php?topic=229222.0

It uses the compilation time as a marker to check weather the EEPROM has been set.

The bonus part is: If you re-upload the code with new EEPROM values, it will run once and update the values due to the different time stamps. But not on power ups or restarts.

Thank you!

billys7:
I will need around 40 bytes.
So i am going to use it like this:

#include<EEPROM.h>
void setup()
{

if (EEPROM.read (1) == 0)
{EEprom.write bla…bla}
}

Is it correct?

That should work as a one off initialisation so long as you write something else other than zero to address 1 in the EEPROM. Note, however, that the EEPROM addresses start from zero, not one.

As I said in my previous reply, it is easy to manipulate a text file containing the values to be written into a form that can be used directly in the program if you already have the values in such a file and you want to avoid typing them into the Arduino IDE.

UKHeliBob:
As I said in my previous reply, it is easy to manipulate a text file containing the values to be written into a form that can be used directly in the program if you already have the values in such a file and you want to avoid typing them into the Arduino IDE.

How can i do this?

You need the program to do this

EEPROM.write(0, 123);
EEPROM.write(1, 100);
EEPROM.write(2, 45);
//etc

This would actually be better as

byte data[] = (123, 100, 45});
for (int x = 0; x < 3; x++
{
    EEPROM.write(x, data[x]);
}

You don’t say what format your list of values is in but let’s suppose it’s
123
100
45

What I would do is use an editor to search and replace the line terminators (probably Carriage Returns but maybe also Linefeeds) with commas. Then you would have
123,100,45
Add in the declaration portion manually, or by copy/paste and the job is nearly done. Only the for loop to type in or copy/paste.

Thank you very much.

I will use it.

You do realise that the whole point of eeprom is that you only have to write it once, and it will be there for evermore until you change it. You could just upload a sketch that writes the default values you want, and then upload a different sketch that will use those values.

KenF:
You do realise that the whole point of eeprom is that you only have to write it once, and it will be there for evermore until you change it. You could just upload a sketch that writes the default values you want, and then upload a different sketch that will use those values.

Quote from reply #1

If you only want to write the values once and have no chance of them being written again by the main program, just write a small program to write the values to EEPROM, run it, then load and run the main program.

UKHeliBob:
Quote from reply #1

Great minds think alike :wink:

You mean that when i upload a program with Arduino software, it is not erasing the values of the EEPROM of a previous program ?

Off Topic,
I can't manage it to get email, when there is an answer.
How can i do this ?

billys7:
You mean that when i upload a program with Arduino software, it is not erasing the values of the EEPROM of a previous program ?

EXACTLY! It'll hold whatever was last written to it, whether it's sketch has been updated or whatever.

billys7:
You mean that when i upload a program with Arduino software, it is not erasing the values of the EEPROM of a previous program ?

No. What we mean is that you can run a program to populate the EEPROM with the required values. This will overwrite the existing values. Then you can run a separate program that reads the values previously stored.