2

I'm trying to write two strings to an Arduino Uno's EEPROM. Here, the first code block works fine: it writes string(True/False) at 'address 0'. But the second block, which writes data at 'address 5', is not working as expected.Reading data at address 5 prints a blank line. What am I missing here? Is this the correct way to write two different strings to EEPROM?

First code block:

int state = LOW;
char first_eeprom_value;
if (state == LOW) {
 state = HIGH;
 char example_string[4] = "True";
 first_eeprom_value = EEPROM.read(0);
 Serial.println(first_eeprom_value);
 for (int i = 0 ; i < 5 ; i++) {
 EEPROM.update(i, example_string[i]);
 }
} else {
 state = LOW;
 char example_string[5] = "False";
 first_eeprom_value = EEPROM.read(0);
 Serial.println(first_eeprom_value);
 for (int i = 0 ; i < 5; i++) {
 EEPROM.update(i, example_string[i]);
 }
}
Time = millis();

Second code block:

char first_eeprom_value1;
if (state1 == LOW) {
 state1 = HIGH;
 char example_string1[4] = "True";
 first_eeprom_value1 = EEPROM.read(5); 
 Serial.println(first_eeprom_value1);
 for (int i = 5 ; i < 11 ; i++) {
 EEPROM.update(i, example_string1[i]);
 }
} else {
 state1 = LOW;
 char example_string1[5] = "False";
 first_eeprom_value1 = EEPROM.read(5); 
 Serial.println(first_eeprom_value1);
 for (int i = 5 ; i < 11 ; i++) {
 EEPROM.update(i, example_string1[i]);
 }
}
Time = millis();
//reading data at address 5
int addr = 5;
char value = EEPROM.read(addr);
Serial.println(value);// this prints blank line
asked Jan 9, 2018 at 6:48
10
  • is not working as expected - what do you expect, and what is actually happening? Commented Jan 9, 2018 at 6:52
  • Nothing is getting written when using 2nd code block. Commented Jan 9, 2018 at 6:57
  • How do you know? Can you please amend your question to include the output from the serial prints that you have in your code? Commented Jan 9, 2018 at 7:07
  • I have edited the question and 2nd code block. Commented Jan 9, 2018 at 7:13
  • remove the if ... then ... else statements ... minimize the code as much as possible ... do just a write and then a read ... do you still have the same problem? Commented Jan 9, 2018 at 7:41

1 Answer 1

2

You are cycling outside the string, thus creating undefined behavior.

Try replacing the second block with this:

char first_eeprom_value1;
if (state1 == LOW) {
 state1 = HIGH;
 char example_string1[4] = "True";
 first_eeprom_value1 = EEPROM.read(5); 
 Serial.println(first_eeprom_value1);
 for (int i = 0; i < sizeof(example_string1); i++) {
 EEPROM.update(i + 5, example_string1[i]);
 }
} else {
 state1 = LOW;
 char example_string1[5] = "False";
 first_eeprom_value1 = EEPROM.read(5); 
 Serial.println(first_eeprom_value1);
 for (int i = 0; i < sizeof(example_string1); i++) {
 EEPROM.update(i + 5, example_string1[i]);
 }
}

Please note that this is not optimal; I suggest you to use fixed-length strings, and fill the unused spaces with blanks or 0s, so you can read back all the bytes you wrote in a safe way

answered Jan 9, 2018 at 13:20

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.