Help! Can't save data in EEPROM

This code is working , can write and read data in EEPROM when i start to Address zero

#include <EEPROM.h>

 String code = "GGGG";
 String readkey ="";

void setup() {

  Serial.begin(9600);

  for (int i=0;i<code.length();i++){
    EEPROM.write(i,code[i]);
  }
  
  for (int i=0;i<code.length();i++){
     char a = EEPROM.read(i);

readkey = readkey+a;
  }
  Serial.println(readkey);
  delay(1000);


}

void loop() {

}

But when i used different address the data didn’t store in EEPROM , Help me guys thanks :slight_smile:

  for (int i=20;i<code.length();i++){
   EEPROM.write(i,code[i]);
  }
  
  for (int i=20;i<code.length();i++){
     char a = EEPROM.read(i);

readkey = readkey+a;
  }
  Serial.println(readkey);
  delay(1000);

Here's your problem:

jsteer:     EEPROM.write(i,code[i]);

In the first snippet, you're writing from the first element of code[]. In the second, you're writing from the 20th element of code[]. So you're writing and then reading different data in each one. The result of this depends on the definition of code, which you didn't provide.

...and if code is less than 20 characters in length, the for loops never run at all.

okay gets ! now i know the problem because code.length() is only 4 and my i = 20 ,
20 < 4 , so it didn’t store in EEPROM . Thanks bro :slight_smile:

I'm glad to hear you found the problem. Enjoy! Per