How to print millis in [hh:mm:ss] format?

Hi, i am trying to measure time, here is my code

unsigned long startTime = millis();

later...

unsigned long currentTime = millis();
unsigned long elapsedTime = currentTime - startTime;

How can I format millis into a 24 hour display?Should I use RTC?
THANKS!

Hi, look in the reference section for "/", "%" operator and google for sprintf () function.

The millis () function will "roll over" after a few days, so watch out for how you calculate the time interval.

If you need long term accuracy and battery backup for the time, use an rtc, but it sounds like you don't need that.

millis() returns the number of milliseconds that have passed since the Arduino was powered up or reset. It is not in any sense a clock. You could turn what millis() returns into hh:mm:ss format by dividing by the appropriate factors but it would still only be the time that has passed since the Arduino was powered up or reset, not the real time. If you were desperate you could even add a fudge factor to the millis() value to display the time now but even then millis() is not accurate over a period.

All in all an RTC is the way to go if you need the real time.

he millis () function will "roll over" after a few days

49 and a bit days, but the point about rollover to zero is still valid.

time is not accurate in itself
why do you need the time ?

Here is a clock sketch that CrossRoads wrote.
I modified it for 24-hour time.
Shouldn't be too hard for you to modify for what you have in mind.

/*  
    CrossRoads Timeclock  
    Adapted for 24hr time
    by Me!  08SEPT2012
    __h__m__s
    and leading zeros, too
*/

unsigned long previousTime = 0;
byte seconds ;
byte minutes ;
byte hours ;

void setup()
{
  //  Preset HHMMSS for 24hr rollover test
  //  Could jump to a function to set time
  //  and start
  seconds = 45;
  minutes =  59;
  hours =  23;
  Serial.begin (9600);
}

void loop ()
{
// I wonder whether "microseconds"...
  if (millis() >= (previousTime)) 
  {
     previousTime = previousTime + 1000;  // use 100000 for uS
     seconds = seconds +1;
     if (seconds == 60)
     {
        seconds = 0;
        minutes = minutes +1;
     }
     if (minutes == 60)
     {
        minutes = 0;
        hours = hours +1;
     }
     if (hours == 24)
     {
       hours = 0;
     }
     if (hours < 10)
     {
       Serial.print("0");
     }
     Serial.print (hours, DEC);
     Serial.print ("h");
     if (minutes < 10)
     {
       Serial.print("0");
     }
     Serial.print (minutes,DEC);
     Serial.print ("m");
     if (seconds < 10)
     {
       Serial.print("0");
     }
     Serial.print(seconds,DEC);
     Serial.println("s");
  } // end 1 second
} // end loop

Many current Arduino boards do not keep accurate time as they use ceramic
resonators, which are accurate typically to +/-0.3% (several minutes a day).

A quartz crystal can be substituted on some of the boards if you have the
soldering skills, giving more like +/-50ppm (a few seconds a day accuracy).

If I use RTC(DS1307), how should I modify the code?

The 1307 library comes with examples, have you checked them?

robtillaart:
The 1307 library comes with examples, have you checked them?

Yes, I have checked them.
I know how to display real time but don't know how to measure it.

MarkT:
Many current Arduino boards do not keep accurate time as they use ceramic
resonators, which are accurate typically to +/-0.3% (several minutes a day).

A quartz crystal can be substituted on some of the boards if you have the
soldering skills, giving more like +/-50ppm (a few seconds a day accuracy).

so how do you know if your Arduino has a ceramic or crystal resonator? Cheching the schematic for the uno, the resonator is just described as "XTAL". Can you tell by the physical appearance, eg does a metal can mean a crystal?

hannibal01:
I know how to display real time but don't know how to measure it.

You mean compare two times and work out the difference? You must convert each time into a number of seconds after a fixed point in time, for example midnight last night, or midnight on 1st Jan. Then you can subtract one result from the other, and finally convert that number of seconds into hh:mm:ss again.

Do you need to measure to tenths or hundredths of a second? An RTC will not do that.

for functions timing you should go for cycle
so paulRB can you tell how to know what is the resonator ?

vector0:
for functions timing you should go for cycle
so paulRB can you tell how to know what is the resonator ?

vector0, what you mean by "go for the cycle"?

I don't know how to tell which type of resonator, that is why I asked the question.

what you mean by "go for the cycle"?

A single, a double, a triple, and a home run, all in one game. I though everybody knew that. Though, what the hell vector0 is talking about is anybody's guess. That response was about as far off target as his/her other posts.

proc execution cycle what else ?

vector0:
proc execution cycle what else ?

Some punctuation. Some quoting, so we have idea what your random phrases are in response to.

maybe not using random neurons will help you to understand not quoted sentences
or you write "random" without knowing what it is ?

maybe not using random neurons will help you to understand not quoted sentences
or you write "random" without knowing what it is ?

You can quote what you are referring to, or you can leave us to guess. I don't play guessing games. I'll just ignore you.