EEPROM Strings

Good morning!! I´m trying to write and then read five strings into the EEPROM memory. So far I´ve been able to write and read one string, which is ended by a null character. However, I don´t know how to start a new string in the address next to the null character, and how to read it as well. Any advice or help would be so much appreciated.

#include <EEPROM.h>

void writeString(char add,String data);
String read_String(char add);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  EEPROM.begin(512);
  String data = "Formatted Time: 15:56:31 Current date: 2020-5-7";
  Serial.print("Writing Data:");
  Serial.println(data);

  writeString(46, data);  
  delay(10);
}

void loop() {
  String recivedData;
  recivedData = read_String(46);
  Serial.print("Read Data:");
  Serial.println(recivedData);
  delay(10000);
}

void writeString(char add,String data)
{
  int _size = data.length();
  int i;
  for(i=0;i<_size;i++)
  {
    EEPROM.write(add+i,data[i]);
  }
  EEPROM.write(add+_size,'\0'); 
  EEPROM.commit();
}

String read_String(char add)
{
  int i;
  char data[100]; //Max 100 Bytes
  int len=0;
  unsigned char k;
  k=EEPROM.read(add);
  while(k != '\0' && len<500)   //Read until null character
  {    
    k=EEPROM.read(add+len);
    data[len]=k;
    len++;
  }
  data[len]='\0';
  return String(data);
}

Why not use EEPROM.put() and EEPROM.get() ?

Incidentally, you refer to strings in your post and Strings in the code. There is a difference

If the first thing you save takes say 10 bytes of storage and starts at address 0 (addresses 0 to 9) then the next place that you can store anything is address 10

UKHeliBob:
Why not use EEPROM.put() and EEPROM.get() ?

Incidentally, you refer to strings in your post and Strings in the code. There is a difference

If the first thing you save takes say 10 bytes of storage and starts at address 0 (addresses 0 to 9) then the next place that you can store anything is address 10

I read that those functions are used for writing and reading one byte only, and I believe each word´s value in the string is one byte. Perhaps I´m wrong, I´ve never worked with the EEPROM. If I use the functions you mention, I´d have to send every character in the string separately, right? Also, I didn´t know there was a difference between string and String!!

UKHeliBob:
Why not use EEPROM.put() and EEPROM.get() ?

Incidentally, you refer to strings in your post and Strings in the code. There is a difference

If the first thing you save takes say 10 bytes of storage and starts at address 0 (addresses 0 to 9) then the next place that you can store anything is address 10

You were right! I used the functions EEPROM.put() and EEPROM.get() and the code did exactly what I needed. Thank you very much, I post here the code in case anyone has the same doubt, it's pretty basic, but it works. :smiley:

#include <EEPROM.h>
char evento1[48] = "Formatted Time: 15:56:31 Current date: 2020-5-2";
char evento2[48] = "Formatted Time: 09:13:02 Current date: 2020-5-1";
char evento3[48] = "Formatted Time: 17:33:12 Current date: 2020-5-4";
char evento4[48] = "Formatted Time: 21:29:53 Current date: 2020-5-5";
char evento5[48] = "Formatted Time: 00:00:00 Current date: 2020-5-8";
char guardado1[48];
char guardado2[48];
char guardado3[48];
char guardado4[48];
char guardado5[48];
int addr1 = 0;
int addr2 = 49;
int addr3 = 97;
int addr4 = 145;
int addr5 = 193;

void setup() {
  Serial.begin(115200);
  EEPROM.begin(512);
  EEPROM.put(addr1,evento1);
  EEPROM.put(addr2,evento2);
  EEPROM.put(addr3,evento3);
  EEPROM.put(addr4,evento4);
  EEPROM.put(addr5,evento5);
}

void loop() {
  EEPROM.get(addr1,guardado1);
  EEPROM.get(addr2,guardado2);
  EEPROM.get(addr3,guardado3);
  EEPROM.get(addr4,guardado4);
  EEPROM.get(addr5,guardado5);
  Serial.println(guardado1);
  Serial.println(guardado2);
  Serial.println(guardado3);
  Serial.println(guardado4);
  Serial.println(guardado5);
  Serial.println("************************************************");
  delay(5000);
}