Elapsed time counter

Hi,

I am working on a egg incubator project, and i am trying to add an "elapsaed time counter", my problem is that i cant figure out how to print the elapsed time on my display, i think i have to use string; and float; i just cant figure out how to implement them.
If there is an easier way i would very much like to hear about it.

My code is posted underneath of here.

Thank you in advance.

//Morten

//DISPLAY
#define oledPin 8
#include <AXE133Y.h> //Inkludere Displayet
AXE133Y OLED = AXE133Y(oledPin);


long day = 86400000; // 86400000 milliseconds in a day
long hour = 3600000; // 3600000 milliseconds in an hour
long minute = 60000; // 60000 milliseconds in a minute
long second =  1000; // 1000 milliseconds in a second

void setup(){
Serial.begin (9600); 
}

void loop(){
 time();
 delay(1000);
}

void time(){
long timeNow = millis();
 
int days = timeNow / day ;                                //number of days
int hours = (timeNow % day) / hour;                       //the remainder from days division (in milliseconds) divided by hours, this gives the full hours
int minutes = ((timeNow % day) % hour) / minute ;         //and so on...
int seconds = (((timeNow % day) % hour) % minute) / second;

 // digital clock display of current time
 Serial.print(days,DEC);  
 printDigits(hours);  
 printDigits(minutes);
 printDigits(seconds);
 Serial.println();  
 
}

void printDigits(byte digits){
 // utility function for digital clock display: prints colon and leading 0
 Serial.print(":");
 if(digits < 10)
   Serial.print('0');
 Serial.print(digits,DEC); 


   
}

Using the StaticString library, something like this

int sec = millis()/1000;
etk::StaticString<120> ss;
int days = sec / 60 / 60 / 24;
int hours = (sec / 60 / 60) % 24;
int minutes = (sec / 60) % 60;
int seconds = sec % 60;
ss.get_rope() << days << " days, " << hours << " hours, " << minutes << " mins, " << seconds << " seconds";
Serial.println(ss.c_str());

What do you see on the serial monitor ?

You certainly don't need to use Strings.

The counter is working on the serial monitor, it is counting as it is suposed to.

//Morten

In order to print an elapsed time you need to save a start time and subtract it from the current time to get the elapsed time. Save millis() as startTime then do the maths.

note: use unsigned long for time related values

long timeNow = millis();

==>

unsigned long timeNow = millis();

this prevents negative values...