As suggested, use a wear leveling scheme for your eeprom writes, and place the data in different register locations as it is written. By moving the data around, you can greatly extend the life of the eeprom.
You want your count data as a long integer(32 bits), and consequently you will need to write the value to 4 bytes in the eeprom using EEPROM.put(). With each write, you will move this storage chunk progressively around in the eeprom to 256 possible locations. You can write 100K times 256 = 25,600,000.
A write every 6 seconds is 14400/day. 25,600,000/14400 = 1778 days or nearly 5 years. If you increment the count by 2 every 12 seconds you are looking at 9.75 years of eeprom life.
There will be a data value in eeprom bytes 0:3, then the next at bytes 4:7, then the next in 8:12 etc. When you get to the end of the eeprom space you start around again. When the power is on and the program is running the management of the next write location is straight forward.
When power is cycled, the program needs to find where the last written chunk was located, and to start writing again from that location.
Because your count data is monotonically increasing, the data becomes its own index to the last written location, and the wear leveling routine is pretty straightforward.
The way to find the last value written is to start reading the eeprom (using EEPROM.get()) from address 0 in 4 byte chunks and test each value against the next. When the value at an address is not bigger than the value in the previous address, you know that is where to write the next value.
You will need to start off with the eeprom at all 0's so that the comparison of each stored value to the next will work on the first pass. Additionally, if on a restart, you happen to find that all 512 locations contain increasing data values, then your next write will be at address 0.
To clear the eeprom to begin, set the boolean variable freshStart to true. Run the program. Then set freshStart = false, upload again, and your counting begins.
#include <EEPROM.h>
unsigned long count;
int index;
#define lastIndex EEPROM.length() - sizeof(count) //1020
boolean freshStart = true;
//boolean freshStart = false;
void setup() {
Serial.begin(115200);
if (freshStart)
clearEeprom();
//find last count and index in Eeprom with checkEeprom()function
Serial.print("Checking for last stored count = ");
index = checkEeprom();
Serial.println(EEPROM.get(index, count));
Serial.print("Located at EEPROM index = ");
Serial.println(index);
}
void loop() {
//emulate machine cycle incrementing count and index
static boolean machineCycleComplete = false;
static unsigned long lastMachineComplete = 0;
if (millis() - lastMachineComplete >= 2000)
{
machineCycleComplete = true;
lastMachineComplete = millis();
}
//values to incremented by machine cycle
if (machineCycleComplete == true)
{
machineCycleComplete = false;
count += 2;
index += sizeof(count);//increment address by 4 with long
if (index > lastIndex)
index = 0;
EEPROM.put(index, count);
Serial.print("index = ");
Serial.print(index);
Serial.print(" count = ");
Serial.println(count);
machineCycleComplete = false;
}
}
void clearEeprom()
{
Serial.println("Clear Eeprom Cells");
for (int i = 0; i < EEPROM.length(); i++)
{
EEPROM.write(i, 0);
}
Serial.println("All eeprom cells set to zero.");
Serial.println("Initialize freshStart to false and reload program");
while (1);//stop program
}
int checkEeprom()
{
static unsigned long lastValue = 0;
static unsigned long value = 0;
for (int i = 0; i <= lastIndex; i += sizeof(count))
{
lastValue = value;
EEPROM.get(i, value);
if (value < lastValue)//next value is not larger than last
{
return (i - sizeof(count));//last valid index
}
if (i == lastIndex)//no smaller value found or values all equal as zeros
{
return i;
}
}
}