Can a sketch write to EEPROM while it is running.?

Hi there,

I know now thanks to the help I was given in this forum that I can upload values to the EEPROM memory and then retrieve them by uploading another code. However, I would like to know if there is a way to store in the EEPROM memory in real time when the array values are generated.

What I am trying to do is a line follower maze solver. I would like for the robot to save the correct turns in an array directly to the EEPROM memory, so when the robot has finally solved the maze the correct turns remain stored even if the robots is switched off.

Any help or ideas are more than welcome.

Yes no problem, just use the EEPROM read and write functions as normal.
Saving to EEPROM is not as quick as SRAM but it is in the few milliseconds range.

Yes, a sketch can write to EEPROM while it is running.
It takes 3.3mS to complete a write.
The '328P datasheet has more info, see
8.4 EEPROM Data Memory

Thank you CrossRoads for the very useful link. I coudn´t find the 8.4 EEPROM Data Memory topic though. I found Memory Types on page 61 instead.

Please Grumpy_Mike and Cross Roads, tell me if the following reasoning is correct. Look at this code:

#include <EEPROM.h>    
void setup(){
EEPROM.write(0, 20);  //
EEPROM.write(1, 21);
EEPROM.write(2, 22);
}
void loop(){
//code for line follower maze solver in here
}

Let´s supose that in the loop I put the code for a line follower maze solver. At the end of the maze an array has all the right turns in it ( as the wrong turns had been eliminated ). So lets say that the code I uploaded for the robot to solve the maze tells the Arduino that when the array with the right turns is completed the program has to go back to void setup and save those values in the EEPROM memory with EEPROM.write. Is this the right way to write in EEPROM memory while the sketch is running?

Thanks to you both.

You need to check on startup if the prior EEPROM data has been stored, and then not overwrite it. That code will overwrite it every time.
Do a test for 255 (0xFF), that is the value read back if it hasn''t been written to yet.
Add some method to tell the program to store when the maze is done. Setup is not run again at the end, you need something that runs from loop, or a function that loop calls.

I have overwritten EEPROM data while trying that code. Is there a problem or risk for memory if I do so?

There is no way to erase all I have written in EEPROM to start from scratch?

Sorry CrossRoads but I don´t know how to do the test for 255 (0xFF). Can you tell me to do it please?

If I am not wrong you should never try to writte in EEPROM from loop as it would damage the memory. So it looks like a dead end for my project. Sure enough you know something that I don´t so it can be done. ¡Help please to understand this!

Sorry for so many things at once.

Stick and wrong end.

You can write in the loop function but not every time round the loop only when data needs to be written, like when you find a new turning.

To test for any value use an if statement.

Erase is an odd concept in this context, writing zeros is erasing as is writing all ones in a byte.
There is no such thing as erasing memory always has to have something in it.

I understand now that it´s possible to write in EEPROM from the loop but only when data needs to be written.

Regarding the way to test values, if you can give me an example it would help me a lot. I just don´t figure out the way to do it myself.

if (EEPROM.read(0) == 255){ 
// blank, no initial data stored, so put some in
EEPROM.write (0,21);
// etc for initial positions or something
}
else {
// move the the existing start data into variables
start1 = EEPROM.read(0);
start2 = EEPROM.read(1);
start2 = EEPROM.read(2);
// or whatever
}

The example code is very useful CrossRoads. However, I have a few question about it. Suppose that I'm doing a test for 255 (0xFF), which is the value read back if EEPROM hasn't been written to yet . Why then in the code you sent me shall I store data after the if statement? Why I have to put EEPROM.write (0,21) if I am just trying to know what values were already stored? And yet another question please: that if statement should go In the setup or in the loop?

I tried other retrieving code that I have used before, but now in an Arduino where I have never written anything in the EEPROM before:

 #include <EEPROM.h>
void setup(){
Serial.begin(9600);
}
void loop(){
byte x;
for(int i=0; i<3; i++){
x = EEPROM.read(i);
Serial.println(x);
}
delay(800);
}

On serial monitor the values shown where like this:
105
255
255
105
255
255
......
......
Is the above the 255 (0xFF) message? If it´s not, why I don´t get it as I should?

Thanks your for all the help given so far.

Do you not think that you are trying to build something here on little or no foundations.
Making a robot learn a maze is not a trivial problem yet you ask the most trivial questions. Not that trivial questions are a problem they are an essential part of learning but by the time you get onto an advanced task you need to have all this stuff under your belt.

Why then in the code you sent me shall I store data after the if statement?

You don't have to that was just an illustration of what you might want to do and store stuff.

Why I have to put EEPROM.write (0,21)

You don't.

that if statement should go In the setup or in the loop

See what I mean, you don't have the basic grasp of how a program is laid out.

Is the above the 255 (0xFF) message?

Yes 255 is exactly the same as the bit pattern 0xff - the 0xff means the number ff in hex.

but now in an Arduino where I have never written anything in the EEPROM before:

Just because you have never written to it does not mean that there have been no writes to it. It could have been a factory test to see the EEPROM was OK in a finished arduino or anything.

You might be better putting a switch on the robot that indicates if you want to follow a path already in EEPROM or you want to forge a new path.

However I would do a whole lot more tutorials before you play about on your own.

Thanks for answering my questions.

Grumpy_Mike I agree with you in the fact that I am not an expert. However, I don´t consider myself a beginer either. I build my own robots with Arduino and know how to make them avoid obstacules or follow lines, interact with leds, how to drive them with infrared or radio signals, interact with visual programs like Scratch and some other things as well. So I did the "Hello World" of Arduino by lighting the led 13 and other tutorials a long time ago. All the codes I have written or adapted are available of course to prove what I´m saying.

I am sorry that You think that I ask "the most trivial questions". I haven´t got experience handling EEPROM memory as I mentioned. Regarding where to put the if statemen, I always put them on the loop part of the program. However, a few days ago you told me not to write in the EEPROM memory on a loop as I could damage the memory. Before that I always did this kind of things in SRAM memory also on a loop. So even if this was for reading values not writting, I just wanted to be sure that this if statement went as I thought on the loop part.

I haven´t got experience handling EEPROM memory as I mentioned.

That is not a problem, that is what you are asking about and that is fine.

Regarding where to put the if statemen, I always put them on the loop part of the program.

It is things like this that make me thing you haven't get got a handle on programming. Maybe you didn't play about with those blink examples enough. This is what I consider to be trivial. Like I say, ask away with them but it does bring one up short when explaining about more complex matters.
If statements should be put where ever they are needed, not "always" in one place.

It is a bit like explaining to some one how to cook a cheese cake an they say, "so where does the food go when you want to eat it"... "err your mouth."

I just find the things you know and those you don't to be a very odd mix.

Now this might look like I am getting at you but this is not the case. You obviously don't see the inconsistency in your knowledge. Maybe it is the tinkering around with other peoples code that has robbed you of the experience of actually writing and formulating your own code and program layout.

Please do keep on asking questions though.

I do see the inconsistency on my knowledge. I have a lot to learn of course!!! I just wanted to point out that I had done a few simple Arduino projects in the past.

As I said before, thanks to you and CrossRoads now I know the basics about Arduino memories and how to interact with them. So I know your are not getting at but giving me good advise instead.

At the moment I have enough to start playing a bit with some code and keep on learning how to program correctly. Sure enough I will keep coming back to the forum for the great support.

Thanks!!!