Write and Read the Arduino EEPROM

Hi All,

I’m just trying to write and read a String from EEPROM Arduino.

Yes I CAN write my string but I can’t recover it and I know it is there.

Please just take a look and tell me where I’m wrong:

#include <EEPROM.h>

String A = "";
String B = "";
String C = A + B; 


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


 // Let's write in EEPROM string C:
  A = "1";
  B = "+351934567890";
  C = A + B;
  
  for(int i=0 ; i < C.length() ; i++)
  {
    EEPROM.write( i,C[i] );
  }

  Serial.println ("Eeprom write OK");
  Serial.println(A);  
  Serial.println(B);
  Serial.println(C);

  // Let's clean all vars:
  
  A = "0";
  B = "0000000000000";
  C = A + B;
  Serial.println ("All Clean");
  Serial.println(A);  
  Serial.println(B);
  Serial.println(C);  
  
  // Let's read from EEPROM
  
  Serial.println();
  Serial.println("Dump of EEPROM:");
  Serial.println(C.length()); 
  for(int i=0 ; i < C.length() ; i++)
  {
      int xx = EEPROM.read(i);
      C = C + char(xx);
      Serial.print(i);
      Serial.print(" = ");
      Serial.print(char(EEPROM.read(i)));
      Serial.print(" = ");
      Serial.println(xx);
  }

  Serial.println ("There is what we got:");
  A = C.substring(1,2);
  B = C.substring(2,C.length());
  Serial.println(A);  
  Serial.println(B);
  Serial.println(C);  
  
}

void loop() 
{}

Other thing, why my cycle FOR goes beyond the C.length() value???

Thanks in advance.

samueljanes:
Other thing, why my cycle FOR goes beyond the C.length() value???

You keep appending characters to C in your loop, making the length increase.

YEAP.........!!!!!!

Tkx man. You're right.

Then I have the secondary problem solved.

But about the main problem?

Any help on that?

GOT IT!!!

Tkx again.

The secondary problem was the problem.

Now i get the final results with this code:

#include <EEPROM.h>

String A = "";
String B = "";
String C = A + B; 


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


 // Let's write in EEPROM string C:
  A = "1";
  B = "+351934567890";
  C = A + B;
  
  for(int i=0 ; i < C.length() ; i++)
  {
    EEPROM.write( i,C[i] );
  }

  Serial.println ("Eeprom write OK");
  Serial.println(A);  
  Serial.println(B);
  Serial.println(C);

  // Let's clean all vars:
  
  A = "0";
  B = "0000000000000";
  C = A + B;
  Serial.println ("All Clean");
  Serial.println(A);  
  Serial.println(B);
  Serial.println(C);  
  
  // Let's read from EEPROM
  
  Serial.println();
  Serial.println("Dump of EEPROM:");
  Serial.println(C.length()); 
  int limit = C.length();
  C = "";
  for(int i=0 ; i < limit ; i++)
  {
      int xx = EEPROM.read(i);
      C = C + char(xx);
      Serial.print(i);
      Serial.print(" = ");
      Serial.print(char(EEPROM.read(i)));
      Serial.print(" = ");
      Serial.println(xx);
  }

  Serial.println ("There is what we got:");
  A = C.substring(0,1);
  B = C.substring(1,limit);
  Serial.println(A);  
  Serial.println(B);
  Serial.println(C);  
  
}

void loop() 
{}

PROBLEM SOLVED!!!