Here is the code below. I increment the counter to a different byte addr in the EEPROM until I reach the end, then start over at the 1st byte. I do this because there is a limit to physically writing to the same sector over and over. I hear it's around 100K per sector. So in this example I write from byte 1-127, each write equals 1 minute (the alarm is set to 60000 millis, waking the teensy->set the EEPROM->go to sleep). For simplicity I set the main function to fire off every 61 minutes but in Production this will be set to 240 minutes (send sensor data every 4 hours). Since it zeros out the prior byte as it moves along the 127 bytes there are 2 writes every iteration, repeating the write every 127 minutes. And around and around we go writing the the EEPROM every minute. At the rate I calculated the lifespan of the EEPROM on my teensy LC will be 12 years.
Code:
#include <Snooze.h>
#include <EEPROM.h>
SnoozeDigital digital;
SnoozeCompare compare;
SnoozeTimer timer;
Snoozelc5vBuffer lc5vBuffer;
SnoozeBlock config_teensyLC(digital, timer, lc5vBuffer);
// start reading from the first byte (address 0) of the EEPROM
unsigned int addr = 0;
int val;
int ttl_time,curr_addr,prior_addr = 0;
void setup()
{
// initialize serial and wait for port to open:
timer.setTimer(60000);// milliseconds
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
}
void loop()
{
Snooze.deepSleep( config_teensyLC );// return module that woke processor
Serial.println("checking time");
check_time();
}
void check_time(){
ttl_time = 0;
// read a byte from the current address of the EEPROM
for(int i=0;i < EEPROM.length();i++){
val = EEPROM.read(i);
if(val > 0){
Serial.print("here is current time: ");
Serial.println(val);
curr_addr=i;
ttl_time = val;
}
}
//61 is # of minutes
if(ttl_time > 0 && ttl_time < 61){
Serial.print("not yet, incrementing time: ");
Serial.println(ttl_time);
if(curr_addr < (EEPROM.length() - 1)){
addr = curr_addr + 1;
}
else addr = 0;
//clear the current address
EEPROM.write(curr_addr, 0);
//set the new time on the next byte
ttl_time = ttl_time + 1;
EEPROM.write(addr, ttl_time);
}
//run main function every 61 minutes (up to 255 minutes)
else if (ttl_time == 61 ){
Serial.print("time to change the world!!! ");
Serial.println(ttl_time);
delay(60000);
//start over
EEPROM.write(curr_addr, 0);
EEPROM.write(0, 1);
ttl_time = 0;
}
else if (ttl_time > 61 ){
Serial.print("SOMETHING WENT WRONG RESETING ALL NUMBERS");
for(int i=0;i < EEPROM.length() ;i++){
EEPROM.write(i, 0);
}
}
else //(ttl_time = 0)
{
EEPROM.write(0, 1);
}
}