Okay so the problem was that my write function itself was a String and my messages were too big. So changed it to an int type and am outputting either 1 or 0 to indicate that the string has been successfully written or not.
here's the code just in case any of you need it.
Write function:
int EEPROM_stringwrite(int addr, const char *char_array) {
int address = 0, counter = 0, eeprom_value;
size_t len = strlen(char_array);
do
{
eeprom_value = EEPROM.read(address);
if (eeprom_value == addr)
{
++address;
eeprom_value = EEPROM.read(address);
if (eeprom_value > len)
{
int current_address = address + eeprom_value + 1, new_address = address + len + 1, data_length, temp_value;
//new_address = current_address - eeprom_value + len;
while (EEPROM.read(current_address + 1) != 0)
{
EEPROM.put(new_address, EEPROM.read(current_address));
++new_address;
++current_address;
data_length = EEPROM.read(current_address);
EEPROM.put(new_address, data_length);
++new_address;
++current_address;
temp_value = current_address;
for (current_address; current_address <= temp_value + data_length - 1; current_address++)
{
char char_value;
EEPROM.get(current_address, char_value);
EEPROM.put(new_address, char_value);
++new_address;
}
}
}
else if (eeprom_value < len)
{
int current_address, new_address = address + eeprom_value + 2, previous_address, data_length;
previous_address = new_address;
while (1)
{
data_length = EEPROM.read(new_address);
if (data_length == 0)
{
new_address = new_address + len - eeprom_value - 2;
if (new_address > EEPROM.length())
{
return 1;
}
else
{
break;
}
}
previous_address = new_address;
new_address = new_address + data_length + 2;
}
do
{
data_length = EEPROM.read(previous_address);
current_address = previous_address + data_length;
for (current_address; current_address >= previous_address + 1; current_address--)
{
char char_value;
EEPROM.get(current_address, char_value);
EEPROM.put(new_address, char_value);
--new_address;
}
EEPROM.put(new_address, EEPROM.read(current_address));
--new_address;
--current_address;
EEPROM.put(new_address, EEPROM.read(current_address));
int address_value = address + eeprom_value + 2, temp_value = previous_address;
while (address_value != temp_value)
{
data_length = EEPROM.read(address_value);
previous_address = address_value;
address_value = address_value + data_length + 2;
}
--new_address;
} while (current_address != address + eeprom_value + 1);
}
EEPROM.put(address, len);
for (int i = address + 1; i <= address + len; i++)
{
EEPROM.put(i, char_array[counter]);
++counter;
}
return 0;
}
eeprom_value = EEPROM.read(address + 1);
address = address + eeprom_value + 2;
} while (eeprom_value != 0);
address = 1;
while (1)
{
eeprom_value = EEPROM.read(address);
if (eeprom_value == 0)
{
break;
}
address = address + eeprom_value + 2;
}
if (address + len < EEPROM.length())
{
EEPROM.put(address - 1, addr);
EEPROM.put(address, len);
for (int i = address + 1; i <= address + len; i++)
{
EEPROM.put(i, char_array[counter]);
++counter;
}
return 0;
}
else
{
return 1;
}
}
returns 0 if string was successfully written. returns 1 if there is no space in EEPROM to fit string.
Read function using string:
String EEPROM_stringread(int addr) {
int address = 0, eeprom_value, counter = 0;
while (address <= EEPROM.length())
{
eeprom_value = EEPROM.read(address);
if (eeprom_value == addr)
{
break;
}
eeprom_value = EEPROM.read(address + 1);
address = address + eeprom_value + 2;
}
if(address > EEPROM.length())
{
return;
}
eeprom_value = EEPROM.read(address + 1);
String eeprom_data = "";
for (int i = address + 2; i <= address + eeprom_value + 1; i++)
{
char char_value;
EEPROM.get(i, char_value);
eeprom_data = eeprom_data + char_value;
}
return eeprom_data;
}
Reading an address that wasn't written to will give you no value. I have learned that using strings is not good so if you can work with char arrays(which i will also be using) here's the code:
char* EEPROM_stringread(int addr) {
int address = 0, eeprom_value, counter = 0;
while (address <= EEPROM.length())
{
eeprom_value = EEPROM.read(address);
if (eeprom_value == addr)
{
break;
}
eeprom_value = EEPROM.read(address + 1);
address = address + eeprom_value + 2;
}
if(address > EEPROM.length())
{
return;
}
eeprom_value = EEPROM.read(address + 1);
char eeprom_data[eeprom_value + 1];
for (int i = address + 2; i <= address + eeprom_value + 1; i++)
{
char char_value;
EEPROM.get(i, char_value);
eeprom_data[counter] = char_value;
++counter;
}
eeprom_data[counter] = '\0';
return eeprom_data;
}
Thanks for all the help
![]()