using EEPROM

hey all.

ive been reading the EEPROM library and have looked everywhere for examples on how EEPROM works.

here is the code I am using right now:

#include <EEPROM.h>

int address = 0;
int value;
int a=0;

void setup(){
  Serial.begin(9600);
}

void loop(){
   //Lets first write a value to the eeprom
  EEPROM.write(address,a);
  a++;
  delay(500);
  if(a>100){a=0;}

  //Lets read the value back and print it to the serial
  value = EEPROM.read(address);
    //Output the data to serial
  Serial.print(value,DEC);  // will print 123
  Serial.print("\n");
  //Serial.print(value,BIN);  // will print 1111011 which is 123 in binary
}

Now if I understand EEPROM right,
if i was to reset the arduino at the "a" count of say...53, when it starts up again, it should pick up where it left off.
instead it starts at 0 again.
this is my first ever experience with EEPROM, so I would like to understand the basic of the code before I implement it in my other projects.
cheers in advance for all your help guys.

:slight_smile:

Chris

The value that you are writing into EEPROM address #0 survives the reset but then when you power up again the first thing you do is overwrite with zero. You seem to be assuming that EEPROM gives you some kind of suspend/resume functionality when it does not - all it gives you is memory who's contents remain when the power is disconnected.

EEPROM is what is called non-volatile memory. Non-Volatile Memory, such as SD Cards, Hard Drives, and EEPROM, don't need a power source. The ATMega328's datasheet says it can store data for 100 years at room temp.!
When you reset your arduino, the program starts over. All variable states are erased, leaving you ONLY with your bare program.
If you want the arduino to pick up where it left off, write some simple code in the setup function that would set the variable a to the current value of address 0.

Good Luck!

I would recommend reading a button or something to control when the writes happen so you don't just sit there & pound the living daylights out of that 1 location by accident, they do have limited durability. As pointed out, you start writing to address 0 whenever the sketch starts - maybe change the sketch to read first, with "a = EEPROM.read(address);", then continue incrementing "a" from there.
Virgin EEPROM addresses will have 0xFF (255 decimal) when read back.
Keep in mind that EEPROM.write has built in 3.3mS time to complete, which you provide for with 500mS delay. If you capture the time written as part of a loop of doing other stuff, and check that 3.3ms have gone by before starting the next write, you can be doing other stuff while waiting (this is the heart of Blink Without Delay).

CrossRoads:
I would recommend reading a button or something to control when the writes happen so you don't just sit there & pound the living daylights out of that 1 location by accident, they do have limited durability. As pointed out, you start writing to address 0 whenever the sketch starts - maybe change the sketch to read first, with "a = EEPROM.read(address);", then continue incrementing "a" from there.
Virgin EEPROM addresses will have 0xFF (255 decimal) when read back.
Keep in mind that EEPROM.write has built in 3.3mS time to complete, which you provide for with 500mS delay. If you capture the time written as part of a loop of doing other stuff, and check that 3.3ms have gone by before starting the next write, you can be doing other stuff while waiting (this is the heart of Blink Without Delay).

that was all I needed to do, the problem is solved thank you very much, and yes I will be using buttons to activate the code in other projects
As just a little side note, thank you to the other two who also posted help and in refference to the second reply, pressding the reset button does not erase the EEPROM variables, they stay exactly the same.

for the benifit of others, here is the working code, I hope it helps in the future:

#include <EEPROM.h>
int address;
int value;
int a=EEPROM.read(address);

void setup(){
  Serial.begin(9600);
}

void loop(){
   //Lets first write a value to the eeprom
  if ((a==255)){
  a=0;
  }
  EEPROM.write(address,a);
  a++;
  delay(500);
  if(a>100){a=0;}

  //Lets read the value back and print it to the serial
  value = EEPROM.read(address);
    //Output the data to serial
  Serial.print(value,DEC);  // will print 123
  Serial.print("\n");
  //Serial.print(value,BIN);  // will print 1111011 which is 123 in binary
}

Peace out

Chris.

Bumping this thread 'cause I have a related question. I need to read and write several chunks of data from and to the EEPROM. I could fool around with structures and unions and cobble up something, but I found this code that does exactly what I want. Unfortunately, it doesn't work. When I paste it into my code the compiler barfs with bizarre errors in completely unrelated places. There appears to be something here that offends the C++ gods but such things are beyond me. Can anyone suggest a fix or some other source of code that does the same thing?

Thanks.

Create a header file (a Tab). Put the Write Anything code into the header file. Include the header file in your Sketch.

Thanks for the suggestion. I tried it and I get the following errors:

In file included from et218.cpp:50:
/writeanything.h: In function 'int EEPROM_writeAnything(int, const T&)':
writeanything.h:2: error: expected initializer before '*' token
writeanything.h:5: error: 'p' was not declared in this scope
/writeanything.h: In function 'int EEPROM_readAnything(int, T&)':
writeanything.h:11: error: 'byte' was not declared in this scope
writeanything.h:11: error: 'p' was not declared in this scope
writeanything.h:11: error: expected primary-expression before ')' token
writeanything.h:11: error: expected primary-expression before 'void'

While I took some highly enlightening courses in college I'm afraid mind reading was not one of them.

If you want help, post the header file and Sketch. Please use code tags.

tdc218:
Thanks for the suggestion. I tried it and I get the following errors:

In file included from et218.cpp:50:
/writeanything.h: In function 'int EEPROM_writeAnything(int, const T&)':
writeanything.h:2: error: expected initializer before '*' token
writeanything.h:5: error: 'p' was not declared in this scope
/writeanything.h: In function 'int EEPROM_readAnything(int, T&)':
writeanything.h:11: error: 'byte' was not declared in this scope
writeanything.h:11: error: 'p' was not declared in this scope
writeanything.h:11: error: expected primary-expression before ')' token
writeanything.h:11: error: expected primary-expression before 'void'

  1. Move the #include <EEPROM.h> from the header file tab to the main sketch tab.
  2. Add 'typedef unsigned char byte;' to the top of the header tab where you had the #include that you just moved.

What you had before wasn't wrong, it's just that the Arduino IDE does some processing of your files during the build process that can break perfectly good code for no obvious reason.

That did it. Thanks very much. One follow up question- the EEPROM docs say "... write cycle takes 3.3 ms to complete". Will interrupts still get responded to during EEPROM writes?

Yes.