Chat here about code rewrites, mods, etc... with respect to the github project https://github.com/esp8266/Arduino

Moderator: igrr

User avatar
By draco
#14131 I haven't figured out how to contribute directly via github yet, but I modified the EEPROM code with some convenience methods to add get/put methods.
I tested it with all primitive data types and arrays, plus some typedef'd struct data, and it all worked fine.

Example code to store data:
Code: Select allEEPROM.begin(512);
int addr=0;
addr += EEPROM.put(addr, myFloat);
addr += EEPROM.put(addr, myInt);
EEPROM.end()

And to retrieve data:
Code: Select allEEPROM.begin(512);
int addr=0;
addr += EEPROM.get(addr, myNewFloat);
addr += EEPROM.get(addr, myNewInt);
EEPROM.commit();
EEPROM.end()

You pass the variable name where you want the data to end up into the function.
Both put and get will return the number of bytes stored/retrieved, so you can easily increment your address counter to store many variables.
You do not have the required permissions to view the files attached to this post.
User avatar
By biobier
#16523 Hi,

any idea why this does not work with this lib?

Code: Select all#include <EEPROM.h>

void setup()
{
  EEPROM.begin(512);
  // write a 0 to all 512 bytes of the EEPROM
  int i=0;
  int data=0;
  while(i<512){
   i+= EEPROM.put(i, data);
   Serial.println(i);
  }
  // turn the LED on when we're done
  Serial.println("Done!");
  digitalWrite(13, HIGH);
  EEPROM.end();
}

void loop()
{
}


it does a reset all the time.
User avatar
By gerardwr
#16581 [quote="biobier"][/quote]

Read the doc:
Code: Select allEEPROM

This is a bit different from standard EEPROM class. You need to call EEPROM.begin(size) before you start reading or writing, size being the number of bytes you want to use. Size can be anywhere between 4 and 4096 bytes.

EEPROM.write does not write to flash immediately, instead you must call EEPROM.commit() whenever you wish to save changes to flash. EEPROM.end() will also commit, and will release the RAM copy of EEPROM contents.