Why are my EEProm memory values being overwritten?

I'm using the code at the bottom, an example of what I am seeing is shown with the quoted Serial Out Data. When I send a command that updates an eeprom address (12), the previous address (11) becomes NULL . I've bolded where it happens.

What am I doing wrong with my EEProm writing that is causing this?
(Arduino Mega 2560)

Serial Out Data

20:11:28.630 -> dmEEPromValue: Sp

20:11:28.630 -> tcEEPromValue: NULL

20:11:28.640 -> tsEEPromValue: NULL

20:11:28.640 -> Sending Bluetooth Data

20:11:28.644 ->

20:11:33.543 ->

20:11:33.543 -> dmEEPromValue: Sp

20:11:33.543 -> tcEEPromValue: NULL

20:11:33.574 -> tsEEPromValue: NULL

20:11:33.606 -> Sending Bluetooth Data

20:11:33.649 ->

20:11:37.593 -> New data (serialInString): TCEnabled

20:11:37.625 ->

20:11:37.625 -> serialInString: TCEnabled

20:11:37.656 ->

20:11:37.656 -> TC selection: Enabled

20:11:37.689 -> writeToEEProm, settingName: tc , settingValue: Enabled

20:11:38.534 ->

20:11:38.534 -> dmEEPromValue: NULL

20:11:38.569 -> tcEEPromValue: Enabled

20:11:38.599 -> tsEEPromValue: NULL

20:11:38.599 -> Sending Bluetooth Data

20:11:38.636 ->

20:11:43.555 ->

20:11:43.555 -> dmEEPromValue: NULL

20:11:43.555 -> tcEEPromValue: Enabled

20:11:43.669 -> tsEEPromValue: NULL

20:11:43.669 -> Sending Bluetooth Data

20:11:43.669 ->

20:11:48.533 ->

20:11:48.533 -> dmEEPromValue: NULL

20:11:48.687 -> tcEEPromValue: Enabled

20:11:48.687 -> tsEEPromValue: NULL

20:11:48.687 -> Sending Bluetooth Data

Code


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

SoftwareSerial BTSerial(12, 13);   // RX , TX

unsigned long currentTime;
unsigned long btLastSendTimeStamp;
unsigned long currentTimeStamp;

// Serial In/Out Variables
String serialInString;
boolean newData = false;

String readFromEEPROM(String settingName)
{
    if (settingName == "dm")
    {
        int address = 11;
        int value = -1;
        int EEPromValue = EEPROM.get(address, value);
        String str_EEPromValueValidate = String(EEPromValue);
        String str_EEPromValue = "NULL";
        if (validateDataType("int", str_EEPromValueValidate.c_str()) == 0)
        {
            return (str_EEPromValue);
        }
        
        if (EEPromValue == 1)
        {
            str_EEPromValue = "Ec";
        }
        else if (EEPromValue == 2)
        {
            str_EEPromValue = "Std";
        }
        else if (EEPromValue == 3)
        {
            str_EEPromValue = "Sp";
        }
        else if (EEPromValue == 4)
        {
            str_EEPromValue = "Cu";
        }
        
        return (str_EEPromValue);
    }
    else if (settingName == "tc")
    {
        int address = 12;
        int value = -1;
        int EEPromValue = EEPROM.get(address, value);
        String str_EEPromValueValidate = String(EEPromValue);
        String str_EEPromValue = "NULL";
        if (validateDataType("int", str_EEPromValueValidate.c_str()) == 0)
        {
            return (str_EEPromValue);
        }

        if (EEPromValue == 1)
        {
            str_EEPromValue = "Enabled";
        }
        else if (EEPromValue == 2)
        {
            str_EEPromValue = "Disabled";
        }

        return (str_EEPromValue);
    }
    else if (settingName == "ts")
    {
        int address = 13;
        int value = -1;
        int EEPromValue = EEPROM.get(address, value);
        String str_EEPromValueValidate = String(EEPromValue);
        String str_EEPromValue = "NULL";
        if (validateDataType("int", str_EEPromValueValidate.c_str()) == 0)
        {
            return (str_EEPromValue);
        }
        
        if (EEPromValue == 1)
        {
            str_EEPromValue = "Fr";
        }
        else if (EEPromValue == 2)
        {
            str_EEPromValue = "Re";
        }
        else if (EEPromValue == 3)
        {
            str_EEPromValue = "Trk";
        }

        return (str_EEPromValue);
    }    
} // readFromEEPROM

String writeToEEProm (String settingName, String settingValue)
{
    Serial.print("writeToEEProm, settingName: "); Serial.print(settingName); Serial.print(" , settingValue: "); Serial.println(settingValue);
    if (settingName == "dm")
    {
        int setting = NULL;
        if (settingValue == "Ec")
        {
            setting = 1;
        }
        else if (settingValue == "Std")
        {
            setting = 2;
        }
        else if (settingValue == "Sp")
        {
            setting = 3;
        }
        else if (settingValue == "Cu")
        {
            setting = 4;
        }

        int address = 11;
        EEPROM.put(address, setting);
    }
    else if (settingName == "tc")
    {
        int setting = NULL;
        if (settingValue == "Enabled")
        {
            setting = 1;
        }
        else if (settingValue == "Disabled")
        {
            setting = 2;
        }

        int address = 12;
        EEPROM.put(address, setting);
    }
    else if (settingName == "tS")
    {
        int setting = NULL;
        if (settingValue == "Fr")
        {
            setting = 1;
        }
        else if (settingValue == "Re")
        {
            setting = 2;
        }
        else if (settingValue == "Trk")
        {
            setting = 3;
        }
        
        int address = 13;
        EEPROM.put(address, setting);
    }

    return "1";
} // writeToEEProm

bool validateDataType(String dataType, char* data) // true is indeed 1, and false is 0.
{
    if (dataType == "string")
    {
        for (int i = 0; i < strlen(data); i++)
        {
            if (isAlpha(data[i]))
            {
                // continue
            }
            else
            {
                return false;
            }
        }
        return true;
    }    
    else if (dataType == "int")
    {
        for (int i = 0; i < strlen(data); i++)
        {
            if (isDigit(data[i]))
            {
                // continue
            }
            else
            {
                return false;
            }
        }
        
        return true;
    }
    else if (dataType == "float")
    {
        int decimalPointCount = 0;
        for (int i = 0; i < strlen(data); i++)
        {
            if (isDigit(data[i]))
            {
                // continue
            }
            else if (data[i] == '.')
            {
                decimalPointCount++;
                if (decimalPointCount > 1)
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        if (decimalPointCount != 1)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
} // validateDataType


void setup() {

    Serial.begin(9600);
    //BTSerial.begin(38400);  // HC-05 default speed in AT command mode
    BTSerial.begin(9600);

    while (!Serial)
    {
      ; // Wait for serial to connect
    }

    while (!BTSerial)
    {
      ; // Wait for BTSerial to connect
    }

    delay(2000);
    Serial.println("Bluetooth Started");

    btLastSendTimeStamp = millis();
} // Setup

void loop() 
{
    serialInString = "";
    while(Serial.available()>0 && newData == false)
    { 
        serialInString = Serial.readString();
        Serial.print("New data (serialInString): "); Serial.println(serialInString);
       
        newData = true;

        delay(15);   
    }

    /*while(BTSerial.available()>0 && newData == false)
    { 
        serialInString = BTSerial.readString();
        Serial.print("New data: "); Serial.println(serialInString);       
        newData = true;

        delay(15);
    }*/

    String getEEPromValue = "NULL";

    getEEPromValue = readFromEEPROM("dm");
    String dmEEPromValue = "NULL";
    if (getEEPromValue != "NULL")
    {
        dmEEPromValue = getEEPromValue;
    }
    else
    {
        dmEEPromValue = getEEPromValue;
    }

    getEEPromValue = "NULL";
    getEEPromValue = readFromEEPROM("tc");
    String tcEEPromValue = "NULL";
    if (getEEPromValue != "NULL")
    {
        tcEEPromValue = getEEPromValue;
    }

    getEEPromValue = "NULL";
    getEEPromValue = readFromEEPROM("ts");
    String tsEEPromValue = "NULL";
    if (getEEPromValue != "NULL")
    {
        tsEEPromValue = getEEPromValue;
    }

    currentTimeStamp = millis(); 
    if ((currentTimeStamp - btLastSendTimeStamp) > 5000)
    {
        Serial.println("");
        Serial.print("dmEEPromValue: "); Serial.println(dmEEPromValue);
        if (dmEEPromValue != "NULL")
        {
            //Serial.print("(!=NULL)lblDMV="); Serial.println(dmEEPromValue);
        }

        Serial.print("tcEEPromValue: "); Serial.println(tcEEPromValue);
        if (tcEEPromValue != "NULL")
        {
            //Serial.print("(!=NULL)lblTCV="); Serial.println(tcEEPromValue);
        }

        Serial.print("tsEEPromValue: "); Serial.println(tsEEPromValue);
        if (tsEEPromValue != "NULL")
        {
            //Serial.print("(!=NULL)lblTSV="); Serial.println(tsEEPromValue);
        }

        btLastSendTimeStamp = millis();
        Serial.println("Sending Bluetooth Data");
        Serial.println("");
    }

    if (serialInString != "")
    {
        Serial.print("serialInString: "); Serial.println(serialInString);
    }

    if ((serialInString == "Ec" || serialInString == "Ec\r\n") && (newData == true))
    {
        Serial.print("DM selection: ");
        Serial.println("Ec");
        writeToEEProm("dm", "Ec");
        newData = false;
    }
    else if ((serialInString == "Std" || serialInString == "Std\r\n") && (newData == true))
    {
        Serial.print("DM selection: ");
        Serial.println("Std");
        writeToEEProm("dm", "Std");
        newData = false;
    }
    else if ((serialInString == "Sp" || serialInString == "Sp\r\n") && (newData == true)) 
    {
        Serial.print("DM selection: ");
        Serial.println("Sp");
        writeToEEProm("dm", "Sp");
        newData = false;
    }
    else if ((serialInString == "Cu" || serialInString == "Cu\r\n") && (newData == true)) 
    {
        Serial.print("DM selection: ");
        Serial.println("Cu");
        writeToEEProm("dm", "Cu");
        newData = false;
    }
    else if ((serialInString == "TCEnabled" || serialInString == "TCEnabled\r\n") && (newData == true))
    {
        Serial.print("TC selection: ");
        Serial.println("Enabled");
        writeToEEProm("tc", "Enabled");
        newData = false;
    }
    else if ((serialInString == "TCDisabled" || serialInString == "TCDisabled\r\n") && (newData == true))
    {
        Serial.print("TC selection: ");
        Serial.println("Disabled");
        writeToEEProm("tc", "Disabled");
        newData = false;
    }
    else if (serialInString == "Fr" && newData == true)
    {
        Serial.print("TS selection: ");
        Serial.println("Fr");
        writeToEEProm("ts", "Fr");
        newData = false;
    }
    else if (serialInString == "Re" && newData == true)
    {
        Serial.print("TS selection: ");
        Serial.println("Re");
        writeToEEProm("ts", "Re");
        newData = false;
    }
    else if (serialInString == "Trk" && newData == true)
    {
        Serial.print("TS selection: ");
        Serial.println("Trk"); // Send back, to the phone, the String "LED: ON"
        writeToEEProm("ts", "Trk");
        newData = false;
    }
}

On a Mega, an int is two bytes, so you need to allow for that much space in the EEProm. Alternatively, you could use a byte instead of int if you only want to use a single byte of EEProm.

Thanks, that fixed it. How would I know that and have saved myself the 4 hours of trying to figure it out and another 1 hour putting together a simplistic example that reproed it? Is there a reference that lists quirks like that?

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.