Having problems with EEPROM, Read and Write fine to one address but not another.

Hi guys

I’m having problems with my project.

There are two main parts of this code:

  1. While in “programming mode” the Arduino waits for someone to send a text string via Serial connection, the Arduino then saves the text string to an EEPROM Address 1, through an array.
    When the next serial string arrives it gets sent to EEPROM Address 2 (also through array).

  2. The Arduino is then switched to the “play mode” where it shows the text strings on an LCD.
    The String from Address 1 is displaying perfectly, but the second address’ string is showing bunch of garbage characters.

Im stuck, please help.

#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
#include <EEPROM.h>

LiquidCrystal lcd(35, 34, 33, 32, 31, 30);

int modeSelect = 4;
long timeoutOver;
char data[40];
int songcounter = 1;
int partcount = 1;

void setup() 
{
  pinMode(INPUT, modeSelect);
  lcd.begin(40, 2);
  Serial.begin(9600); 
}

void loop() 
{
  while(digitalRead(modeSelect) == LOW)    
  {
    PlayMode();
  }
  while(digitalRead(modeSelect) == HIGH)    
  {
    ProgrammingMode();
  }
}

int ProgrammingMode()
{
        if(partcount == 1 && digitalRead(modeSelect) == HIGH)
        {
          Programming1();
        }
        if(partcount == 2 && digitalRead(modeSelect) == HIGH)
        {
          Programming2();
        }
}

int Programming1()
{
 boolean somethingReceived = false;
 int pos = 1;
 if(Serial.available())
 {
   somethingReceived = true;
   do
   {
     while(Serial.available())
     {
       data[min(pos,40-1)] = Serial.read();
       timeoutOver = millis()+10; 
       pos++;
     }
   }
   while(millis()<timeoutOver);
   //data[pos] = ' ';
 }
 if(somethingReceived)
 {
   int eepromcounter1 = 1;//(1 + (30 * (songcounter - 1))); 
   int eepromcounter2 = 40;//(30 * songcounter);
   int i;
   for (i = eepromcounter1; i <= eepromcounter2; i = i + 1) 
   {
    int a=(data[i]); 
    EEPROM.write(i, a);
    delay(2);
   }
   lcd.clear();
   lcd.print("Programming 1 done... ");
   delay(500);
   partcount = 2;
 }
}

int Programming2()
{
 boolean somethingReceived = false;
 int pos = 1;
 if(Serial.available())
 {
   somethingReceived = true;
   do
   {
     while(Serial.available())
     {
       data[min(pos,40-1)] = Serial.read();
       timeoutOver = millis()+10; 
       pos++;
     }
   }
   while(millis()<timeoutOver);
   //data[pos] = ' ';
 }
 if(somethingReceived)
 {
   int eepromcounter1 = 41;//(331 + (30 * (songcounter - 1))); 
   int eepromcounter2 = 72;//((30 * songcounter) + 331);
   int i;
   for (i = eepromcounter1; i <= eepromcounter2; i = i + 1) 
   {
    int a=(data[i]); 
    EEPROM.write(i, a);
    delay(2);
   }  
   lcd.clear();
   lcd.print("Programming 2 done... ");
   delay(500);
   partcount = 1;
 }
}

int PlayMode()
{
   int eepromcounter1 = 1;//(331 + (30 * (songcounter - 1))); 
   int eepromcounter2 = 33;//((30 * songcounter) + 331);
   int i;
   char a;
   lcd.clear();
   lcd.print("Song: ");
   lcd.setCursor(0, 1);
   lcd.print("Verse: "); 
   lcd.setCursor(6, 0);
   for (i = eepromcounter1; i <= eepromcounter2; i = i + 1) 
   {
    a = EEPROM.read(i);  
    a = char(a);
    lcd.print(a);                   
   }
   eepromcounter1 = 41;//(331 + (30 * (songcounter - 1))); 
   eepromcounter2 = 72;//((30 * songcounter) + 331);
   lcd.setCursor(7, 1);
   for (i = eepromcounter1; i <= eepromcounter2; i = i + 1) 
   {
    a = EEPROM.read(i);  
    a = char(a);
    lcd.print(a);                      
   }   
   delay(600);
}
    int a=(data[i]);
    EEPROM.write(i, a);

This is stupid. The write() method takes an address and a byte to write to EEPROM. data is the correct type. a is NOT.

The indices for the data array are NOT the same as the addresses that you want to write to in EEPROM.

The second function is even worse.

The two functions you have differ only in where they write to in EEPROM. You only need one function, with the start address as an input.

Dear asshole, thanks for not wasting only my time but also your own. I dont get your smart ass comments.

I dont get your smart ass comments.

Too f**king bad. Stop smoking the WEED, and RTFM.

Dear Pothead,

Yes, Paul is very direct. But he is right, so no need for you as a newbie to insult him :wink:

A int takes 2 bytes in EEPROM. So a write will drop the MSB. And you want to make a clear map to where to store what in EEPROM :slight_smile: