Writing and retrieving strings from EEprom

I am designing a bell timer that rings the bell at predetermined times input by a user. I have got everything working but am really struggling with reading and writing strings to and from the EEprom (Mega 2560 IDE 1.8.5)

The input for these preset times comes in as a 5 character string stored in an array. So for example the string 31430 would ring the bell on Wednesday (day 3, first character) at half past 2 in the afternoon (last 4 characters). I can enter these 'bell ring times' through the LCD menu system using push buttons.

I have written a 5 character conversion routine to determinate the correct time from a RTC and this scan the array so when its 5 character output matches one of the 'bell ring times' it sounds the bell.

I have already done the coding and got all the functionality and menus working and the bell rings at the correct times.

What I need to do is store the contents of the array to the EEprom so in case of power failure that 'bell ring times' can be read back from the EEprom into the string array as the five original characters. This is important because there might be up to a hundred different times in a week that the bell could ring.

I have posted a simplified bit of code below with some explanations contained within it. If anybody could point me in the right direction I would be grateful.
Thanks in advance

#include <EEPROM.h>

String rt[10];
// will have upto a hundred entries in this String array
// only first 10 shown in this example.
// String will always be 5 characters long.
// String will always contains numeric characters
// lowest string could be "00000" (five zero's)
// highest string could be "72359"

// Have to be able to read the string back without trucation so for example, rt[4] which is
// shown in this example as "00001" would need to read back from the EEprom as "00001" once it has been converted back to a string if the solution is to store it in another format.

void setup() {

// will have upto a hundred entries in this String array. Only 10 shown in this example
// String will always be 5 characters long.
// string can be changed by user input so array values and positions are not fixed.
// String will always contains numeric characters
// lowest string could be "00000" (five zero's)
// highest string could be "99999" (five nines)

rt[0] = "10730";
rt[1] = "30730";
rt[2] = "40610";
rt[3] = "20730";
rt[4] = "00001";
rt[5] = "10736";
rt[6] = "99999";
rt[7] = "51620";
rt[8] = "20755";
rt[9] = "72359";
rt[10] = "41230";

void loop() {

writeEEprom () ;

readEEprom () ;

int wait = 1;
while (wait == 1) {
// do nothing but wait here

void writeEEprom () {
// write values to EEprom
for (int i = 0 ; i <= 10; i++) {
Serial.print("Writing array value :");

  • Serial.print(" to EEprom Address :");*
  • Serial.println(i);*
    _ EEPROM.put(i, rt*.toInt());_
    void readEEprom () {
    // read values from EEprom*_
    * for (int i = 0 ; i <= 10; i++) {*
    * Serial.print("EEprom address ");*
    * Serial.println(i);*
    * Serial.print (" = ");*
    * Serial.println(EEPROM.read(i));*
    * }*

Sorry just released that the comments under setup read slightly different o the ones after the array is called earlier. Copy and paste when I should have used cut and paste!

The correct comment for the 'highest value' is ""72359"" not "99999"

Thank you

Please remember to post your code using code tags </> so that special characters are not changed in the code.

You cannot write Strings to EEPROM because the data is not all together. The string is a data header and a pointer to other memory. When you read that back you don't have the 'other' memory any more.

You should use character arrays as strings and this will work.

Hi marco_c
Thank you for your prompt reply. Is it possible you could expand on your reply with an example so I can understand it more fully

Text Strings can be represented in two ways. You can use the String data type or you can make a string out of an array of type char and null-terminate it (note one is 'S' and the other is 's').

A String is partly defined as

class Substring
		int offset;
		int length;
		String *str;

Note that the data is not stored with the data structure but in separate memory. IF you try and just save the String object, you will save the pointer to the data and not the data itself. Consequently, when you later load it back, the data will no longer there and you have thest the actual character information.

If you simply use an array of characters, then you do not have this problem as you are saving the array itself.

Also, if all you are saving is numbers why are you using characters when a long integer could do the same job and take less space (4 bytes).

Hi marco_c
Thank you again for you reply. I was using strings as the output from the RTC is incoming in string form ie currday = rtc.getDOWStr(); // get current day from RTC

So I started coding for text strings and built up a program that is working okay based on manipulating text strings. Should really have looked at the EEprom stuff earlier!

Based on you advice I will write subroutines that place the values into EEprom as integers and when read back are converted in Strings when stored in the array. I will pad out with leading zero's where required.

Thanks again for you help