hello
I need to write and then read a value in eeprom, bigger than I byte.From what I have read I can just write or read a byte to eeprom at each time.
My question is how can I write a value like a string to it like "I love Arduino"
Does I have to split all the characters to then write it to a index position on eeprom or is there another way of writing it ?
I
My question is "Why didn't you search before repeating one of the most-asked questions on the forum?"
Does I have to split all the characters to then write it to a index position on eeprom or is there another way of writing it ?
The EEPROM is a byte-wide device.
So I have to split it and then write the splited values to its on byte eeprom right?
You could go here:
http://arduino.cc/playground/Main/InterfacingWithHardware#Storage
and see what other solutions people have come up with.
So I have to split it and then write the splited values to its on byte eeprom right?
In the end, yes. With a for loop, that's nearly trivial. 4 lines of code, including the two curly braces.
Like this:
char *test = "Hello";
int i = 0;
while (test[i] != '\0')
{
EEPROM.write(i, test[i]);
i++;
}
To read the string back into a character array, we can do something like this:
char test[10];
int i = 0;
char ch;
ch = EEPROM.read(i);
while (ch != '\0' && i < 10)
{
test[i] = ch;
ch = EEPROM.read(i);
i++;
}
From my book: 'Programming Arduino: Getting started with sketches'
Your first isn't the best example I can think of - there's no indication in EEPROM of how long the string is, and the second doesn't terminate the string.
Better to write the terminator into EEPROM, IMO.
AWOL, thanks for noticing that.
The terminator should be written, you are right.
char *test = "Hello";
int i = 0;
while (test[i] != '\0')
{
EEPROM.write(i, test[i]);
i++;
}
EEPROM.write(i, '\0');
And then there is the EEPROManything function that can handle all the messy details for you, that may work for strings also? Not sure it works with arduino 1.0 or needs updating?
http://arduino.cc/playground/Code/EEPROMWriteAnything
Lefty
thanks for the tips
Si looking for your code I understand all parts except char *test = "Hello";
Why does I need to use a pointer?
My question is more about the pointer itself. Can you explain me what will happen on this line of code?
Why does I need to use a pointer?
You don't. That could just as easily be:
char test[] = "Hello";
In this case test is an implicit pointer, to the first memory location used. In Si's code, test is an explicit pointer.