Convert millis to HH:MM:SS as a string

I have looked at several examples and they do not address the issue of converting millis() in to hours, minutes and seconds, it also raises the question of why these values are not directly addressable as values automatically available like they are in JS.

eg. (assume vars have been declared, etc)

curTime = millis();
toSeconds = curTime / 1000;
toMinutes = toseconds % 60;
toSeconds -= toMinutes * 60;
toHours = toSeconds % 60;
toSeconds -= toHours * 60;

timeString = toHours + ":" + toMinutes + ":" + toseconds;

Serial.println( timeString);

this is a close approximation of an example on this Arduino forum that doesn't work, I know I tried it and it throws errors about String and char * and other issues about int when not throwing up issues of String and char *

An example error message
timemachine1_cpp.ino: In function ‘void loop()’: timemachine1_cpp.ino:48:79: error: cannot convert ‘StringSumHelper’ to ‘char*’ in initialization

Can someone point me to an actual known working example that converts millis() to their respective elements and then formats a string for output to the serial monitor please.

If (millis() - timer>=1000){
SECONDS+=1
timer=millis()
}

I suggest that you use C style strings rather than Strings

Assuming that hoursVar, minsVar and secsVar are ints and have been previously calculated

char buffer[20];
sprintf(buffer,"%d:%d:%d",hoursVar, minsVar, secsVar);
Serial.println(buffer);

Thanks for your reply, I am not looking to "count" seconds, I am looking to use millis() as part of a state machine

Thank you for your reply, I shall give that a go.

You can not generate the current time from millis directly.

You have to add the starting time, and handle the millis overflow.

Not required for the application I am working on.

For example, when button A is pressed, only a timer count from millis() to variable for timerA is counted, when button B is pressed... ... When button C is pressed ... ...

The actual time is irrelevant, I was looking for a time conversion for serial output only that wasn't in millis() but a more human readable format.

Use the functions in the DateTime library.

Or, arithmetic, using for example, the "%" modulus operator.
e.g.

long seconds = msElapsed % 1000;

Thanks, didn't know there was one.

It might be a bit of overkill for what I am attempting to do. I think I will need the available space for a library to drive a set of segmented LED's.

Libraries don't work that way. When you include a library, only the parts that you use directly, ever contribute to the machine code size. In fact, if you include it, and only use its definitions, it won't even add a single byte of additional machine code.

I showed you the other way to do it. Why not give it a try and post your solution?

Sometime in the future, its baby steps for me, last time I programmed in C was back in 1985 and then it was Delphi C, I got to get my head in to the syntax again, I have been using PHP and JavaScript for 20 years so its like having to learn a new programming language all over again.

When the prototype has been thoroughly tested I will Github it.

Okay, it sounds like you have no intention of programming at the moment. Please close the thread as "solved".

No idea how to do that, there is no button labeled up to close it.

The JeeLabs/Adafruit fork RTC library does offer an all in one text string function similar to what you asked for. Here is a line from my code that uses it:

char localTimeString[] = "DDD, MMM DD, YYYY hh:mm:ss AP";
...
console << F("local time: ") << localNow.toString(localTimeString) << endl;

Perhaps you just need to get used to the absence of many high level functions from your Delphi etc. environments. This is a small memory, small footprint, minimal or no OS environment.

The code you alread posted creates variables that you can easily convert to a text string using snprintf().

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.