Pierre_Swanepoel_94:
It should store the run time every 5 minutes on the EEPROM.
If I remember correctly, each EEPROM cell is only good for 100000 writes.
So, if you always write the time to the same EEPROM location, that is 100000 times 5 minutes, or about 8333 hours. After that, the EEPROM might continue to work... or it might not.
You will need to learn about wear leveling. I'm not sure about how to do it properly myself, but it sounds like what you need.
As for the actual timekeeping itself (not the EEPROM stuff), this code should work.
It shows the total runtime on a 16x2 LCD display. Or, at least, it should if I haven't made a mistake somewhere. I have not actually tested this code, so it might have bugs in it. (I modified it from something else I once had, which I know worked properly. But I do not know if it will still work properly after these modifications.)
#include <LiquidCrystal.h>
// declare and initialize variables
byte totalMyriads = 0; // myriads of hours (1 myriad = 10000)
byte totalHundreds = 0; // hundreds of hours
byte totalHours = 0; // hours, from 0 to 99
byte totalMinutes = 0; // "minutes" part of total
byte totalSeconds = 0; // "seconds" part of total
unsigned long microsAtLastSecond = 0UL; // value of micros() at most recent 1/10 second
// a pretty important constant
const unsigned long MICROS_PER_SECOND = 1000000UL; // number of microseconds per second
// here we specify what pins our LCD is on
// RS EN D4 D5 D6 D7
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
// start the LCD going
lcd.begin(16, 2);
// display the time (which at this point will be all zeros)
updateTimeDisplay();
}
void loop() {
// check if it is time for the clock to advance
if ((micros() - microsAtLastSecond) >= MICROS_PER_SECOND) {
// make the clock advance 1 second
totalSeconds++;
// make sure that the next advance happens exactly when it is due
// (they should happen exactly at 1-second intervals)
microsAtLastTenth += MICROS_PER_TENTH;
// our clock needs to do more than count seconds
// it also needs to count minutes, hours, hundreds, and myriads
// so let's go ahead and do that
// too many seconds?
if (totalSeconds >= 60) {
// trade 60 seconds for 1 minute
totalSeconds -= 60;
totalMinutes++;
}
// too many minutes?
if (totalMinutes >= 60) {
// trade 60 minutes for 1 hour
totalMinutes -= 60;
totalHours++;
}
// too many hours?
if (totalHours >= 100) {
// trade 100 hours for 1 hundred
totalHours -= 100;
totalHundreds++;
}
// too many hundreds?
if (totalHundreds >= 100) {
// trade 100 hundreds for 1 myriad
totalHundreds -= 100;
totalMyriads++;
}
// update the display so we can see the new time
updateTimeDisplay();
}
}
void updateTimeDisplay () {
// function to update the display to show the current elapsed time
//
// we want the display to look like this
// Position: 01234567890123456
// Line 0: h m s
// Line 1: 00000:00:00
// declare a buffer for storing a string (we'll need it later)
// we expect to need only 16 characters, but to be safe, let's make room for 20
// so we allow 20 characters worth of room, plus 1 extra for the null terminator
// (Always allow 1 character extra worth of room for the null terminator!)
char buf[21];
// move to the top line of the display
lcd.setCursor(0,0);
// show the units
lcd.print(" h m s ");
// convert the elapsed time to a string
// for myriads, allow 3 digits worth of room
// for hundreds, hours, minutes, and seconds, allow 2 digits for each, and use leading zeros
sprintf(buf,"%3d%02d%02d:%02d:%02d ",totalMyriads,totalHundreds,totalHours,totalMinutes,totalSeconds);
// move to the bottom line of the display
lcd.setCursor(0,1);
// show the string for the elapsed time
lcd.print(buf);
}