Go Down

Topic: Help! Can't save data in EEPROM  (Read 166 times) previous topic - next topic

jsteer

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

Code: [Select]
#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 :)

Code: [Select]

  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);

pert

#1
Oct 09, 2018, 10:23 am Last Edit: Oct 09, 2018, 10:24 am by pert
Here's your problem:
Code: [Select]

    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.

pert

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

jsteer

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 :)

pert

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

Go Up