Setting Alarms: Arduino EEPROM

In my cat feeder project, I want to be able to store the feed times in such a way that when the Arduino is turned off and turned back on, I do not have to reset the alarms (I will have to reset the time, unless I have some automatic way of setting it with a connection to the internet).

I can save the feed times in an SD Card using a shield I picked up from RadioShack, but there’s a simpler solution. Every Arduino has a tiny built in hard drive in it called EEPROM. Each have different amounts depending on the microcontroller. My Arduino Uno clone has an ATmega328 microchip, which has 1024 bytes of memory. Each byte can contain a value of 0-255, or a group of bytes can store any form of data.

It should be noted that each EEPROM has a guaranteed life of 100,000 write/erase cycles, so you shouldn’t use this as a data logger that will be updating very often, else you may wear it out.

My cat feeder program will have 3 feed times (tracked by a constant, numberOfFeedTimes, for easy changes later), each with an hour (0-23) and a minute (0-59), so I will need 6 bytes total to store the information. I create an array, “feedTime”, to store my feed time values in the same order as I will read/write them to EEPROM.

i 0 1 2 3 4 5
feedTime[i] hour1 minute1 hour2 minute2 hour3 minute3

When I start the program, I want to read the EEPROM to the feedTime array. I create a function to do this, ReadFeedTimes(). Before you write any information to the bytes in EEPROM, all values are set to 255.

To record the information for a time to EEPROM, I create another function, which I can call as needed in my Loop() function to record new feed times.

To write values to EEPROM, I use Update(), rather than write(), as update will not write if the byte value is equal to what is being written. This can extend the write life of your EEPROM (though in my case, only a tiny amount).

Then, I reread the values to my feedTime array for ease of use in the program.

I hope this gives you some insight on how to incorporate some simple memory usage in your Arduino projects.

Cheers!

Leave a Message!

Your email address will not be published. Required fields are marked *