Eeprom understanding addresses

I am trying to add eeprom to a sketch with an esp8226 board, but I am apparently not doing it right. I am having a function called for different things within the sketch
Ex. void Opened()

Within this function a motor is turned off once a limit switch is reached. The time is then recorded to a variable from a web server via:
hourOpened = (timeClient.getHours());
minuteOpened = (timeClient.getMinutes());
This lets me know when the door was opened. I am trying to save this time within eeprom so that if power is cut, I will still know what that time was when the board is powered back up. Here is the complete function:

void Opened()
  digitalWrite(RelayDown, LOW);
  digitalWrite(RelayUp, LOW);
  doorMoving = false;
  if (digitalRead(topSwitch) == LOW) {
    topSwitchDetected = "yes";
    bottomSwitchDetected = "no";
    closed = false;
    opened = true;
    EEPROM.write(10, closed);
    EEPROM.write(11, opened);
    hourOpened = (timeClient.getHours());
    minuteOpened = (timeClient.getMinutes());
    EEPROM.write(16, minuteOpened);
    if (hourOpened > 12) { //  This changes from military time to regular
      hourOpened = (hourOpened - 12);
      pm = true;
    else {
      hourOpened = hourOpened;
      pm = false;
    EEPROM.write(15, hourOpened);   

I read this information with a checkDoorState() function within setup() as soon as the board is powered up:

void checkDoorState()
  closed =;
  opened =;
  hourClosed =;
  minuteClosed =;
  hourOpened = (15);
  minuteOpened = (16);

The problem is when I disconnect and reconnect the board, I do get something from eeprom in the variables hourOpened and minuteOpened, but it is not accurate. It is usually an old time that was recorded prior to the last time the door opened.
I don't know if it has anything to do with it, but I don't understand how the address location works in eeprom.

If I assign address 1 to save data, is that single address location able to record a two digit number? Or would it need multiple address locations to record this two digit number?
If I use EEPROM.end doesn't this erase any past data that was stored in this location AND record the new data?

Edit: I can put entire code if necessary, but it is long and I figured nobody would want to look at the entire code.

Thanks for any help!

Well, at least please post the section where these variables are defined:

closed =;
  opened =;
  hourClosed =;
  minuteClosed =;
  hourOpened = (15);
  minuteOpened = (16)

because there might be a problem with the length of the data types.

 EEPROM.write(16, minuteOpened);

What type of variable is minuteOpened ?

An EEPROM address can only hold a single byte, the value of which can be between 0 and 255. So, if you have a 2 digit number such as 49 in a byte variable you can save it. If, however, the number is actually a string such as "49" then it will consist of 3 bytes and cannot be saved in a single EEPROM location. The EEPROM.put() function allows you to save multi byte data but it will save it to multiple EEPROM locations starting at the address you specify and it is up to you to ensure that multi byte data saved does not overlap other locations used

I have them saved as int..... I guess that is a problem huh? If I save them as byte this would be ok because they should never get larger than 60 right?

Yes, you need to allocate more than one byte for them.

An int needs two bytes of storage so you cannot use EEPROM.write() to save it, so a byte variable is to be preferred

Even if it works, you should align storage types. It's "do what you say, say what you do".

My favoured way to save a group of related data would be to put the data into a struct and put()/get() the struct with single commands, thus avoiding the need to know the address of the individual data items

Or perhaps 4 bytes:

Perhaps you need to use EEPROM.commit()

EDIT: no, sorry, EEPROM.end() performs EEPROM.commit() and you are using that.

Indeed. I should perhaps have said that an int requires multiple bytes

I did try .commit() previously, but it did the same thing. I don’t understand the difference in it and .end().
I will try changing variable type.

.end() performs .commit(), so you should be ok.

But I think if you use .end(), you must use .begin() again before writing any more data to EEPROM.

EEPROM.write does not write to flash immediately, instead you must call EEPROM.commit() whenever you wish to save changes to flash. EEPROM.end() will also commit, and will release the RAM copy of EEPROM contents.

Can you explain?
I don't understand how you direct eeprom to read data if you don't know what the address is?


You put() the struct to an address that you specify and the entire struct is saved to EEPROM in multiple bytes

When you want the data back you get() the struct from the previously used address then use the data in the struct rather than directly from EEPROM hence avoiding the need to know the address of the individual data items

Does this mean that it "erases" all other data within the address? before writing the new data to flash?

No. It writes all the RAM data to EEPROM, then de-allocates the RAM memory so it can be used for other purposes.

Until the commit() happens the data is held in RAM. Once the commit() occurs that RAM copy is not needed so the memory that it occupies is released to be used for other purposes

Remember that each EEPROM address can only hold a single byte of data so there is no "other data" in that address

Because EEPROM has only a relatively small number of guaranteed write cycles per address before it may fail the EEPROM functions generally only write to EEPROM addresses where the data has changed. So, if you write() a byte to an address then write a second byte with the same value to the same address it will not actually be written as there is no need.

I am lost... Sorry.

If I had a
byte hourOpen
byte hourClosed

and I get ready to put it in eeprom, I would say,
EEPROM.put() (structA, 1);

Then, when I get ready to retreive it (on power up), I would have:
EEPROM.get() (structA, 1);

and this would give me all of the saved info for all of the variables within the struct?

I am guessing that is not the correct way to declare a struct?