Hello, everyone, I wrote a couple of functions to read and write strings to EEPROM. The code works perfectly but the problem is when I run it a second time by either pressing the reset button on the Arduino or re-upload the code it is freezing after just calling the function once or twice (I know because I made it print messages in different sections of the function). It was supposed to run 5 times. What is this problem? Why does it work the first time only? I have run the code on multiple Arduino's with the same result. A couple of times it worked even after the first time but then it stops suddenly. What is the problem
? Please help. Thank you in advance.
Here's the code:
#include<EEPROM.h>
String EEPROM_stringread(int addr) {
int address = 0, eeprom_value;
String eeprom_data = "";
while (1)
{
eeprom_value = EEPROM.read(address);
if (eeprom_value == addr)
{
break;
}
eeprom_value = EEPROM.read(address + 1);
address = address + eeprom_value + 2;
}
eeprom_value = EEPROM.read(address + 1);
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;
}
String EEPROM_stringwrite(int addr, String data) {
int len = data.length(), address = 0, counter = 0, eeprom_value;
//data = data + " ";
++len;
char char_array[len];
data.toCharArray(char_array, len);
--len;
do
{
eeprom_value = EEPROM.read(address);
Serial.println("yass");
if (eeprom_value == addr)
{
Serial.println("match");
++address;
eeprom_value = EEPROM.read(address);
if (eeprom_value > len)
{
Serial.println("small");
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)
{
Serial.println("large");
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 "String too large";
}
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;
}
Serial.println("cmon");
return "Sucess";
}
eeprom_value = EEPROM.read(address + 1);
address = address + eeprom_value + 2;
} while (eeprom_value != 0);
Serial.println("normal");
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;
}
}
else
{
return "String too large";
}
Serial.println("cmon!");
}
void setup() {
Serial.begin(9600);
for (int i = 0 ; i < EEPROM.length() ; i++) {
EEPROM.write(i, 0);
}
Serial.println("done!");
EEPROM_stringwrite(11, "hello");
EEPROM_stringwrite(15, "world");
EEPROM_stringwrite(9, "monster");
EEPROM_stringwrite(23, "saiyan");
EEPROM_stringwrite(30, "goku");
String a;
a = EEPROM_stringread(11);
Serial.println(a);
a = EEPROM_stringread(15);
Serial.println(a);
a = EEPROM_stringread(9);
Serial.println(a);
a = EEPROM_stringread(23);
Serial.println(a);
a = EEPROM_stringread(30);
Serial.println(a);
EEPROM_stringwrite(9, " ");
a = EEPROM_stringread(23);
Serial.println(a);
a = EEPROM_stringread(30);
Serial.println(a);
}
void loop() {
}