0
Offline
Newbie
Karma: 0
Posts: 46
Arduino rocks
|
 |
« on: November 15, 2010, 06:33:24 pm » |
Hi I'm trying to insert arduino runtime into a tweet like this: 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!
|
|
|
|
« Last Edit: November 15, 2010, 06:33:38 pm by .jacob »
|
Logged
|
|
|
|
|
Central Europe
Offline
Edison Member
Karma: 5
Posts: 1220
Use the Source, Luke.
|
 |
« Reply #1 on: November 15, 2010, 06:43:12 pm » |
Look at the Time Library. It provides all the functions you want for this.
Korman
|
|
|
|
« Last Edit: November 15, 2010, 06:43:27 pm by Korman »
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 316
Posts: 35566
Seattle, WA USA
|
 |
« Reply #2 on: November 15, 2010, 06:48:24 pm » |
You'll need an RTC if you don't want to have to reset the real time every time the Arduino restarts.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 46
Arduino rocks
|
 |
« Reply #3 on: November 15, 2010, 06:59:43 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 316
Posts: 35566
Seattle, WA USA
|
 |
« Reply #4 on: November 15, 2010, 07:05:51 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 46
Arduino rocks
|
 |
« Reply #5 on: November 15, 2010, 10:07:43 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Dallas
Offline
Shannon Member
Karma: 120
Posts: 10194
|
 |
« Reply #6 on: November 15, 2010, 10:15:54 pm » |
If that's all you need... ...or an unsigned incremented with each message.
|
|
|
|
« Last Edit: November 15, 2010, 10:16:44 pm by bcook »
|
Logged
|
|
|
|
|
Central Europe
Offline
Edison Member
Karma: 5
Posts: 1220
Use the Source, Luke.
|
 |
« Reply #7 on: November 16, 2010, 01:39:20 am » |
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
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 46
Arduino rocks
|
 |
« Reply #8 on: November 19, 2010, 11:38:40 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Dallas
Offline
Shannon Member
Karma: 120
Posts: 10194
|
 |
« Reply #9 on: November 20, 2010, 01:55:16 am » |
You stated earlier this is what you need... I just need some sort of counter not to exceed 6 digits to get around Twitter filtering Have you changed your mind?
|
|
|
|
|
Logged
|
|
|
|
|
Netherlands
Online
Tesla Member
Karma: 90
Posts: 9434
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #10 on: November 20, 2010, 02:56:52 am » |
I just want millis to print as HH:MM:SS.
try this // 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; }
|
|
|
|
« Last Edit: November 20, 2010, 02:58:05 am by robtillaart »
|
Logged
|
|
|
|
|
Global Moderator
Dallas
Offline
Shannon Member
Karma: 120
Posts: 10194
|
 |
« Reply #11 on: November 20, 2010, 03:14:52 am » |
@robtillaart: Won't the string exceed 6 digits?
|
|
|
|
|
Logged
|
|
|
|
|
Netherlands
Online
Tesla Member
Karma: 90
Posts: 9434
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #12 on: November 20, 2010, 03:57:38 am » |
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.
|
|
|
|
« Last Edit: November 20, 2010, 03:58:27 am by robtillaart »
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 46
Arduino rocks
|
 |
« Reply #13 on: November 21, 2010, 06:38:18 pm » |
I'm getting an error with this: #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.
|
|
|
|
« Last Edit: November 21, 2010, 06:52:09 pm by .jacob »
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 316
Posts: 35566
Seattle, WA USA
|
 |
« Reply #14 on: November 21, 2010, 06:49:49 pm » |
Serial.sprintf(str, "%04ld:%02d:%02d", h, m, s); The Serial class does not have a sprintf method. delay(10000); After the return statement, this code is unreachable. Waste of space and time having it here. void loop() // t is time in seconds = millis()/1000; char * TimeToString(unsigned long t) What happened to the body of loop()?
|
|
|
|
|
Logged
|
|
|
|
|
|