-->
Page 1 of 8

EEPROM put and get methods

PostPosted: Fri Apr 10, 2015 12:55 pm
by draco
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.

Re: EEPROM put and get methods

PostPosted: Tue May 05, 2015 1:15 am
by biobier
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.

Re: EEPROM put and get methods

PostPosted: Tue May 05, 2015 10:43 am
by gerardwr
[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.

Re: EEPROM put and get methods

PostPosted: Tue May 05, 2015 12:07 pm
by biobier
Not sure what you mean! All what is stated in the doc is taken care in the code posted by me.