-->
Page 5 of 8

Re: EEPROM put and get methods

PostPosted: Tue Oct 11, 2016 4:58 am
by tceel
When I write to eeprom with the following code:
Code: Select allchar test[8]="TESTi10";
void setup(){
   EEPROM.begin(20);                             
  for(int cc=0 ;cc<9;cc++){
    int value = test[cc]-'0'; //convert char to int
    EEPROM.put(cc,value);
    EEPROM.end();
    delay(20);
    Serial.println(value);
    delay(500);
  }
}

I get this in terminal(not bad):
Code: Select all36
21
35
36
57
1
0
-48
-39

But when I read the stored data with the following code:
Code: Select allvoid loop(){
EEPROM.begin(20);
  for(int cd=0;cd<9;cd++){
    EEPROM.get(cd,raw[cd]);
   
  }
  EEPROM.commit();
    EEPROM.end();
  for(int cd=0;cd<9;cd++){
    Serial.println(raw[cd]);
  }
   
}

I get this in terminal:
Code: Select all36
956301312
20512768
80128
-805306055
-640679935
-2502656
-9776
-39

as you can see only the first character saved on eeprom and others are rubish!
WHY ?? :cry:

Re: EEPROM put and get methods

PostPosted: Wed Oct 12, 2016 9:52 am
by martinayotte
Your above code doesn't show up how the "raw" array is declared.
The EEPROM put/get methods are templated, so if you put chars and read them back into integers, it will be distorded since chars and integers are not the same size.
If you only wish to deal with chars, use read/write methods instead.

Re: EEPROM put and get methods

PostPosted: Wed Oct 12, 2016 12:35 pm
by tceel
Thank you martinayotte.
With following code I can write once and read many times.
But when I turn off module and turn it on again and press read button it shows me this:
Code: Select all#$9ےےےےے


Code: Select allvoid setup(){
EEPROM.begin(512);
}

void loop(){
  if (digitalRead(writeBtn)==0) {             
   delay(1000);
   if (digitalRead(WriteBtn)==0) {
  //write to eeprom
  String qsid="Global";
  int charLength=qsid.length();
 
  Serial.println("writing eeprom:");
          for (int i = 0; i < qsid.length(); ++i)
            {
              EEPROM.write(i, qsid[i]);
              Serial.print("Wrote: ");
              Serial.println(qsid[i]);
            }

   }
  }

  if (digitalRead(readBtn)==0) {             //delete previous saved SSID and password value
   delay(1000);
   if (digitalRead(readBtn)==0) {
//read to eeprom
  Serial.println("Reading EEPROM:");
  String esid;
  for (int i = 0; i < 15; ++i)
    {
      esid += char(EEPROM.read(i));
    }
    //esid.trim();
    Serial.println(esid.length());
 
  Serial.println(esid);
  Serial.println("Reading EEPROM pass");
}
}
}


Can you please tell me what is wrong in my code?
Thank you in advance ;)

Re: EEPROM put and get methods

PostPosted: Fri Oct 14, 2016 2:43 pm
by martinayotte
Actually, all your write() are cached in RAM.
To be written to real EEPROM emulated in Flash, you need to do a EEPROM.commit() when you finished your write(), it will then be persisted.