Go Down

Topic: Possible to print millis in [HH:MM:SS] format? (Read 5124 times) previous topic - next topic

.jacob

Nov 16, 2010, 12:33 am Last Edit: Nov 16, 2010, 12:33 am by .jacob Reason: 1
Hi I'm trying to insert arduino runtime into a tweet like this:

Code: [Select]

sprintf(tweet,"%d Sensor Reading S1=%e S2=%e", millis, (float)temp_f, (float)real_humidity); //Your tweet message


But millis of course is just the ms it's been running in raw form which quickly becomes a huge string.  How can I format millis into a 24 hour display, [HH:MM:SS] that resets every 24 hours, or better yet, insert an authentic time value?  I need a time stamp of some sort to get around Twitter's spam filters.

Thanks!


Korman

#1
Nov 16, 2010, 12:43 am Last Edit: Nov 16, 2010, 12:43 am by Korman Reason: 1
Look at the Time Library. It provides all the functions you want for this.

Korman

PaulS

You'll need an RTC if you don't want to have to reset the real time every time the Arduino restarts.

.jacob

Thanks!  A RTC would be nice to have but not necessary as I just need some sort of counter not to exceed 6 digits to get around Twitter filtering.

I'll experiment with the time library.


PaulS

If that's all you need, just generate a random number. Or, only use the last 6 characters of millis()'s value as a string.

.jacob

Is there no way to easily format millis as HHMMSS?  I'm digging through the Time library and it seems like everything syncs to some external time source be it my computer or RTC.  

Coding Badly

#6
Nov 16, 2010, 04:15 am Last Edit: Nov 16, 2010, 04:16 am by bcook Reason: 1
Quote
If that's all you need...

...or an unsigned incremented with each message.

Korman

Quote
I'm digging through the Time library and it seems like everything syncs to some external time source be it my computer or RTC.


Don't get mislead, the important thing with the Time library is that everything can sync with an external timer source, but doesn't need to. You also can set the time manually or decide times based on the 1.1.1970 (or the number of seconds since the the Arduino was turned on) are good enough. Just ignore the syncing part you don't care about.

Korman

.jacob

Hey can someone give me some guidance please on the time library?  Which example should I turn to?  I just want millis to print as HH:MM:SS.

Thanks much.

Coding Badly


You stated earlier this is what you need...
Quote
I just need some sort of counter not to exceed 6 digits to get around Twitter filtering

Have you changed your mind?

robtillaart

#10
Nov 20, 2010, 08:56 am Last Edit: Nov 20, 2010, 08:58 am by robtillaart Reason: 1
Quote
I just want millis to print as HH:MM:SS.


try this
Code: [Select]
// t is time in seconds = millis()/1000;
char * TimeToString(unsigned long t)
{
 static char str[12];
 long h = t / 3600;
 t = t % 3600;
 int m = t / 60;
 int s = t % 60;
 sprintf(str, "%04ld:%02d:%02d", h, m, s);
 return str;
}
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Coding Badly


@robtillaart: Won't the string exceed 6 digits?

robtillaart

#12
Nov 20, 2010, 09:57 am Last Edit: Nov 20, 2010, 09:58 am by robtillaart Reason: 1
Quote
Won't the string exceed 6 digits?

Yes, definitely!
The format jacob wanted "HH:MM:SS" is 8 characters long (I quoted that), and the code I provide will generate a string in HHHH:MM:SS format so at least 10 characters long (not counting the '\0' charcters needed). Imho the code provides enough insight to alter it to generate a string of six chars.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

.jacob

#13
Nov 22, 2010, 12:38 am Last Edit: Nov 22, 2010, 12:52 am by .jacob Reason: 1
I'm getting an error with this:

Code: [Select]

#include <Time.h>


void setup()
{
  Serial.begin(9600); // Open serial connection to report values to host
  Serial.println("Starting up");
}

void loop()
{
 // t is time in seconds = millis()/1000;
char * TimeToString(unsigned long t)

 static char str[12];
 long h = t / 3600;
 t = t % 3600;
 int m = t / 60;
 int s = t % 60;
 Serial.sprintf(str, "%04ld:%02d:%02d", h, m, s);
 return str;


 delay(10000);
}


"Expected initialized before "static."


I'm sorry if this is extremely simple for some but I just want a simple counter to display in my tweet.

PaulS

Code: [Select]
 Serial.sprintf(str, "%04ld:%02d:%02d", h, m, s);
The Serial class does not have a sprintf method.

Code: [Select]
 delay(10000);
After the return statement, this code is unreachable. Waste of space and time having it here.

Code: [Select]
void loop()
 // t is time in seconds = millis()/1000;
char * TimeToString(unsigned long t)

What happened to the body of loop()?

Go Up