1

I'm trying to store four items to EEPROM. I can do one item no problem, I just don't know how to make the jump to multiple items.

My code:

#include <EEPROM.h>
char sgrpID[31] = "acpilot's arduino";
char addrID[51] = "123 any street rd";
char zpcdID[11] = "12345";
char gwayID[31] = "what a cool toy!";


void setup()
{
  Serial.begin(9600);
  for (int i = 0; i < 6; i++) {
    EEPROM.write(i, sgrpID[i]);
    EEPROM.write(i, addrID[i]);
    EEPROM.write(i, zpcdID[i]);
    EEPROM.write(i, gwayID[i]);
  }
}

void loop() {
  Serial.println(sgrpID);
  Serial.println(addrID);
  Serial.println(zpcdID);
  Serial.println(gwayID);
  delay(99999);
}

I want to be able to call on these in a sketch and post them to a db along with dynamic data of some flavor (TBD).

Am on the right path? Any help, corrections, or general guidance is appreciated. I've moved beyond the "novice" stage of basic Arduino programming but an completely new to EEPROM. If in doubt, SLOWER AND LOUDER is by far the best way to communicate key EEPROM concepts to me.

I have reviewed the Arduino EEPROM library but I was reading Greek. I keep reading it and I'm sure at somepoint I'll recognize a pattrn and start to "get it."

5

Some things I noticed in your code:

  • You are writing everything to the same address (is that intentional?)
  • You are only writing the first 6 six bytes of your arrays (I am pretty sure that is intentional)
  • You are declaring these arrays to be bigger than what you are putting in them (this is again probably intentional, but I am just guessing)

So here is an example of how I would handle the eeprom:

#include <EEPROM.h>

char sgrpID[31] = "acpilot's arduino";
char addrID[51] = "123 any street rd";
char zpcdID[11] = "12345";
char gwayID[31] = "what a cool toy!";

//These are the definitions of what goes where in memory
//You do not want everything to have the same number
const size_t SGRP_ID_EE_START = (0);  //Zero is good address to start
//To get the next address we merely add the size of the first item
//With the address of the first item
const size_t ADDR_ID_EE_START = (sizeof(sgrpID) + SGRP_ID_EE_START);
//And so on and so forth
const size_t ZPCD_ID_EE_START = (sizeof(addrID) + ADDR_ID_EE_START);
const size_t GWAY_ID_EE_START = (sizeof(zpcdID) + ZPCD_ID_EE_START);

//Use this function to write a char array to the EEPROM at
//The address "address" (and the size of "len")
void WriteEepromArray(size_t address,char* arr,size_t len){
  for (size_t x = 0; x < len; x++){
    EEPROM.write(address+x,arr[x]);
  }
}
//Use this function to load your char array with values from
//The EEPROM
void LoadFromEeprom(size_t address,char* arr,size_t len){  //Be aware that this function modifys the data pointed to by "arr"
  for (size_t x = 0; x < len; x++){
    arr[x] = EEPROM.read(address+x);
  }
}

void setup()
{
  Serial.begin(9600);
  //Some sample writes
  //               starting address----array-----size of the array
  WriteEepromArray(SGRP_ID_EE_START   ,sgrpID   ,sizeof(sgrpID));
  WriteEepromArray(ADDR_ID_EE_START   ,addrID   ,sizeof(addrID));
  WriteEepromArray(ZPCD_ID_EE_START   ,zpcdID   ,sizeof(zpcdID));
  WriteEepromArray(GWAY_ID_EE_START   ,gwayID   ,sizeof(gwayID));
  //Use the following to load:
  LoadFromEeprom(SGRP_ID_EE_START,sgrpID,sizeof(sgrpID));
  Serial.println(sgrpID);   //Now it should be loaded

}

The comments I hope help....otherwise comment below with what I left out.

  • Answers to your bullets: no; that's an error, should be the length of the string, not just 6 bytes; yes, I'm allowing for longer names than what I'm using. I follow the flow but some of the commands are new. I don't know what this means, for example: for (size_t x = 0; x < len; x++){ arr[x] = EEPROM.read(address+x);. If I need to recall the "sgrpID", for example, and use it in a sketch do I simply use: for (size_t x = 0; x < len; x++){ arr[x] = EEPROM.read(sgrpID+x);? – acpilot May 15 '16 at 4:14
  • I've spent many hrs trying to get a Serial.println() out of this and i haven't a clue as to what I'm doing wrong. Can you dumb it down more than you already have? I'm doing something like "WriteEepromArray(size_t GWAY_ID_EE_START,char* gwayID,size_t len)"...with no luck. I understand for the most part what's going on (or supposed to happen), I just can't make it spit a string back out. – acpilot May 16 '16 at 7:27
  • 1
    @acpilot To read the eeprom do LoadFromEeprom(SGRP_ID_EE_START ,sgrpID ,sizeof(sgrpID));. This will then cause the sgrpID array to be loaded with whatever was in the eeprom. Try that.....if that doesn't work let me know...... – DarthRubik May 17 '16 at 3:05
  • 1
    @acpilot PS: If you were wondering what the size_t was all about.....that is just a type (like int or char) whose max value is the maximum size of an array.....so it makes sense to use that in a loop.....but in order to have a simpler view of it just imagine that it says int there. – DarthRubik May 17 '16 at 3:08
  • 1
    @acpilot Added an example of reading....hope that helps – DarthRubik May 17 '16 at 23:47

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.