How to write a string in eeprom ?

Hi,

I try to store some text string in the eeprom but using the eeprom library i don't find the way to store there text like a simple 'hello'

any idea ?

Thanks

seb

I think the only way is to read / write one byte at a time.

Yes. I just figured this out.

byte by byte.

Have a look at Halleys "read write anything in eeprom" solution. http://www.arduino.cc/playground/Code/EEPROMWriteAnything

Gordon

Gordon, I think Halleys template will have some problems with strings. I doubt code that uses string pointers would work:

char test[] = "hello world"; char *strptr = &test[6]; EEPROM_writeAnything(0, strptr);

For external (I2C) eeprom, use this tutorial: http://www.arduino.cc/playground/Code/I2CEEPROM

For internal eeprom, use this: http://arduino.cc/en/Reference/EEPROMWrite

mem is right-- the EEPROM_writeAnything() has to have a known size. An EEPROM_WriteString() derivative would not be hard, if you think about how strings are addressed.

The problem then becomes how to read it? Do you want to read it back into a fixed buffer of a known size? (If so, you probably should have written from a fixed buffer as well with the aforementioned function.) If you just want to read it character by character, then EEPROM_read() is fine.

You could do something like, find the length of the string, use the first byte or two to store the length. Then when reading back, you read the first two bytes, now you know the length of string buffer to create. Read in your string bytes and you're done.

Yes, but there is still the problem with knowing where to start reading.

Fixed length strings would be very easy to accmmodate as:

EEPROMstringOffset = StringIndex * sizeof(MyString);

If you had to support variable length strings, if you could make a decison about them maximum number of strings you wanted to save up front, you could create an array at the front of the EEPROM that saved the offset to the string which would be the sum of the lengths of the stings stored to date.

Once you know where to write a string, writing it would be easy:

int position; char *p;

position = EEPROMstringOffset; p = MyStr; while(*p){ EEPROM.write(position++,p++); } EEPROM.write(position,'\0'); // Save the NULL terminator NextEEPROMstringOffset = position + 1; // Next String will be stored here

I think Halleys template will have some problems with strings. I doubt code that uses string pointers would work:

char test[] = "hello world"; 
 char *strptr = &test[6];
 EEPROM_writeAnything(0, strptr);

Indeed, that would fail. However, for string arrays defined as you did, the sizeof(test) would give 12, and so EEPROM_writeAnything(0, test) would work.

I just brought up the routines because they're great for ints, floats, structures, etc. You can also expand on the idea for null-terminated strings as has been done in this thread; you will have to deal with the best allocation and indexing scheme to remember where strings start.

You can write any NUL-terminated string into EEPROM with one function call:

#include <avr/eeprom.h>

eeprom_write_block((void *)mystr,(void *)ee_addr,strlen(mystr));

where ‘mystr’ is a pointer to the NUL-terminated string, and ‘ee_addr’ is the starting address in EEPROM. This will work for constant strings or char or byte arrays.

For documentation on this and related functions, see avr-libc: <avr/eeprom.h>: EEPROM handling

Regards,

-Mike

Sorry, that should be:eeprom_write_block((void *)mystr,(void *)ee_addr,strlen(mystr)+1);So that the NUL gets written, too.

-Mike

I'm new here... and I'm kind of interested in this. cause I need to save 10 entries only in my EEPROM and I want to stick with Halley's template. So here are my ideas. I have a DS1307 which will give me timestamps therefore the length of the string will be constant. Beside each timestamp I have some float values I'm estimating that I'll need 12bytes per entry. As mentioned I may have problems with reading them out later. Any ideas guys? Thanks in Advance

Yes, but there is still the problem with knowing where to start reading.

how about using the first byte for the size of the string and the second byte for the starting address location? and allocate a fixed number of bytes in the eeprom for this lookup table.

Hi I am very new to this, so excuse the silly question- If you have 4 sensors- and you want to write and read e.g. byte senvals{0,1,2,3} to the eeprom if(something happens) { then could you write it something like this? I have made the array of readings into a a byte array is that right? EEPROM.write(index,sensval[0]); EEPROM.write(index +1,sensval[1]); EEPROM.write(index +2,sensval[2]); EEPROM.write(index +3,sensval[3]); EEPROM.write(index +4,sensval[4]);

The code may be correct. It IS how to write multiple bytes to EEPROM (although a for loop would have been better). But:

I have made the array of readings into a a byte array is that right?

How did you do this?

byte senval[] ={0,1,2,3,4} then I made a function to read the sensors: sensval[0] = analogRead(5) etc. The intention was to make a string out of the sensor values. But a string is an array of chars so i guess I made a mistake. So i must convert the int sensor values to chars instead of bytes? Once these values are in the EEPROM they are stored as bytes-so when they are read and printed to the serial monitor, how do I get the correct (# between 0,1023) sensor value back again? Should I serial.print (value, DEC) or (value, BYTE)?

The output from a function should be stored in an appropriately typed variable. The output from digitalRead is an integer in the range 0 to 1023. Since the largest value that can be stored in a byte is 255, byte is not the appropriate type of array to store the output from analogRead in.

The data stored in EEPROM is stored in bytes. Integers are two bytes long. In order to store in int in EEPROM, the most significant byte is extracted, then the last significant byte is extracted. Then, the two bytes are stored in EEPROM.

byte msb = intVal / 256;
byte lsb = intVal % 256;

Upon being read back out, the int can be recreated:

intVal = msb * 256 + lsb;

I just thought that doing this:
for(int x=0;x<5;x++)EEPROM.write(index+x,sensval[5]); would work but I am getting readings that don’t seem to make sense.

The EEPROM write tutorial example(on Arduino site) doesn’t address this,( I guess because only one analog sensor value is used? well I guess in a sense that example does because the readings are divided into four so they will fit into a byte.