-->
Page 1 of 1

EEPROM not working as expected

PostPosted: Tue Jun 16, 2015 7:59 am
by carlhesp
Hi

Here is an example sketch that reads the first 10 bytes of the eeprom and outputs it to the serial. At the same time it writes the value of the iterator to that byte. I expect that after a reset of the esp8266 the values are remembered.

is the eeprom wiped on reset?

Code: Select all/*
 * EEPROM Read
 *
 * Reads the value of each byte of the EEPROM and prints it
 * to the computer.
 * This example code is in the public domain.
 */

#include <EEPROM.h>

// start reading from the first byte (address 0) of the EEPROM
int address = 0;
byte value;

void setup()
{
  // initialize serial and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  EEPROM.begin(512);
}

int runOnce = 0;

void readAndStore(){
  if (runOnce == 0) {
    runOnce = 1;
    int i;
    Serial.println("read and then write the value of i to each block");
    for (i=0; i<10; i++){
      Serial.print(i);
      Serial.print(": ");
      Serial.println(EEPROM.read(i));
      EEPROM.write(i,i);
    }
    Serial.println("read back values straight after write");
    for (i=0; i<10; i++){
      Serial.print(i);
      Serial.print(": ");
      Serial.println(EEPROM.read(i));
    }
  }
   
}

void loop()
{
 readAndStore();
}


example output after hitting reset multiple times
Code: Select allread and then write the value of i to each block
0: 255
1: 255
2: 255
3: 255
4: 255
5: 255
6: 255
7: 255
8: 255
9: 255
read back values straight after write
0: 0
1: 1
2: 2
3: 3
4: 4
5: 5
6: 6
7: 7
8: 8
9: 9


You can see the values are read back correctly in the 2nd loop. If I hit reset the first loop is always reverting back to 256.

Any ideas what I am doing wrong?
thanks

Re: EEPROM not working as expected

PostPosted: Tue Jun 16, 2015 8:13 am
by carlhesp
lol

I forgot the

Code: Select allEEPROM.commit();