Go Down

Topic: using EEPROM (Read 3356 times) previous topic - next topic

Chrisferreira

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:
Code: [Select]
#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.

:)

Chris

Andy Brown

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.
Home of the Nokia QVGA TFT LCD hacks: http://andybrown.me.uk

baum

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!

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).

Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Chrisferreira


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:

Code: [Select]
#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.

tdc218

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.

Coding Badly

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

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'

Coding Badly

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.

Andy Brown


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.
Home of the Nokia QVGA TFT LCD hacks: http://andybrown.me.uk

tdc218

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?

Coding Badly


Go Up