EEPROM.put() using a multi-variable struct

Short answer is you're doing the right thing.

EEPROM.put() only updates changed bytes of the structure. It still takes a bit of time to compare your struct to the contents of the EEPROM so don't do put() on every loop if you know nothing has changed, but go ahead and put the whole thing when you suspect some data may have changed.

In addition to the fields you have, I like to have a version field to be able to migrate the struct on load. If you're changing apps between devices a lot, start the version at different values (1000 for app 1, 2000 for app 2, etc) and reset the whole EEPROM content if the version isn't one of the ones expected for this app.

See this post for an example. Instead of restoring default values if the version doesn't match, you could also migrate if a previous valid version is detected: set new fields to default values and save.

3 Likes