1

I am working on an Arduino Mega 2560. I want to read and write the internal EEPROM.

Can I use EEPROM.get()/EEPROM.put()?

I want to store 3 things in EEPROM:

  1. Boolean status.

  2. String password: length 5 digits.

  3. String Mobile number: length 10 digits.

  • 2
    Yes. Is your question how you can do it? – Mark Smith Mar 16 '17 at 8:06
  • the easy way to get that working is to simply read/write them all each time, and use a char as a delimter, like "~", to split the loaded values. – dandavis Mar 16 '17 at 20:08
1

How do I write and read internal EEPROM? Can I use EEPROM.get()/EEPROM.put()?

Yes, the Arduino Reference for EEPROM contains an example of what you are asking for. Below is a simple rewrite of the example for EEPROMGet for your info.

struct MyObject{
  bool status;
  char password[6];
  char mobileNumber[11];
};

void setup() 
{

  int eeAddress = 0; //EEPROM address to start reading from

  Serial.begin( 9600 );
  while (!Serial) ;

  MyObject customVar; //Variable to store custom object read from EEPROM.
  EEPROM.get(eeAddress, customVar);

  Serial.println(F("Read custom object from EEPROM: "));
  Serial.println(customVar.status);
  Serial.println(customVar.password);
  Serial.println(customVar.mobileNumber);
}

For more details and examples see https://www.arduino.cc/en/Reference/EEPROM.

Cheers!

3

Avoid the 'String' object, use a character array of fixed length. You might want to add one extra byte for the zero-terminator. Put everything in a 'struct' and then you can use the EEPROM.get() and EEPROM.put() to read and write the complete struct. I prefer a struct to keep the data from EEPROM together. That makes it easier to add a checksum and a version number for the data. When something is wrong with the data from EEPROM then default values could be used.

Instead of a struct, you can also read and write the seperate items. The EEPROM.get() and EEPROM.put() should be able to use a character array of fixed length.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.