Storing data to EEPROM [SOLVED]

Hi, The following code produces an odd result and I was wondering if someone can spot my error. The result I get is Aa 65 97 Aa 1 2. It’s the last two values which are wrong - should be 65 97 again. If I make fcppnSAdd = 0 and fcppnEAdd = 19 then it works, but why should changing the address range cause this effect? Thanks for any help you can give.

#include <EEPROM.h>

String inputString = "Aa";
boolean pnFlag = false; 
boolean fcppnFlag = false; 

//Start and end addresses for EEPROM. 
int pnSAdd = 0; 
int pnEAdd = 19;
int fcppnSAdd = 20; 
int fcppnEAdd = 39;

void setup(){
  Serial.begin(9600);
}

//Store input data in EEPROM.
void writeEEPROM(String storeMsg, int sAdd, int eAdd) {
  int msgLen = storeMsg.length();
  char buffer[msgLen + 1];
  storeMsg.toCharArray(buffer, msgLen + 1);
  Serial.println(buffer);
  //Clear EEPROM memory.
  for(int i = sAdd; i <= eAdd; i++) {
    EEPROM.write(i, 0);
  }
  //Write new data to EEPROM memory.
  for(int i = sAdd; i < msgLen + sAdd; i++) {
      EEPROM.write(i, buffer[i]);
      Serial.println(EEPROM.read(i), DEC);
  }
}

//Send first msg.
void pnf() 
{
  writeEEPROM(inputString, pnSAdd, pnEAdd);
  pnFlag = true;
}

//Receive first contact person's phone number. 
void fcppnf() 
{
  writeEEPROM(inputString, fcppnSAdd, fcppnEAdd);
  fcppnFlag = true;
}

void loop()
{
  if(pnFlag == false) pnf();
  else {
    if(fcppnFlag == false) fcppnf();
  }
}

Thanks PaulS, the light has come on! I used the same variable, i, to refer to two different ranges. Solved by the following modification:

//Write new data to EEPROM memory.
  int j = 0;
  for(int i = sAdd; i < msgLen + sAdd; i++) { 
    EEPROM.write(i, buffer[j]);
    j++;
    Serial.println(EEPROM.read(i), DEC);
  }
}

Is it an I2C device ? AFAIK the read adress differs 1 or 2 digits from the write adress

void writeEEPROM(String storeMsg, int sAdd, int eAdd) {
  int msgLen = storeMsg.length();
  char buffer[msgLen + 1];
  storeMsg.toCharArray(buffer, msgLen + 1);

Why not ditch the String class and use char arrays?

I added some stuff to your code. Try this. I think the light will go on.

#include <EEPROM.h>

String inputString = "Aa";
boolean pnFlag = false; 
boolean fcppnFlag = false; 

//Start and end addresses for EEPROM. 
int pnSAdd = 0; 
int pnEAdd = 19;
int fcppnSAdd = 20; 
int fcppnEAdd = 39;

void setup(){
  Serial.begin(9600);
}

//Store input data in EEPROM.
void writeEEPROM(String storeMsg, int sAdd, int eAdd) {
  int msgLen = storeMsg.length();
  char buffer[msgLen + 1];
  storeMsg.toCharArray(buffer, msgLen + 1);
  Serial.println(buffer);
  //Clear EEPROM memory.
  for(int i = sAdd; i <= eAdd; i++) {
    EEPROM.write(i, 0);
  }
  //Write new data to EEPROM memory.
  for(int i = sAdd; i < msgLen + sAdd; i++)
  {
    Serial.print("buffer[");
    Serial.print(i);
    Serial.print("] = ");
    Serial.println(buffer[i]);

    EEPROM.write(i, buffer[i]);
    Serial.print(i);
    Serial.print(") ");
    Serial.println(EEPROM.read(i), DEC);
  }
}

//Send first msg.
void pnf() 
{
  writeEEPROM(inputString, pnSAdd, pnEAdd);
  pnFlag = true;
}

//Receive first contact person's phone number. 
void fcppnf() 
{
  writeEEPROM(inputString, fcppnSAdd, fcppnEAdd);
  fcppnFlag = true;
}

void loop()
{
  if(pnFlag == false) pnf();
  else {
    if(fcppnFlag == false) fcppnf();
  }
}