Go Down

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

.jacob

So how can I test this?  I'm trying to test it in serial before inserting into my tweeting sketch.

This seems so basic to have some sort of run time clock on an arduino device.  Why is this so difficult?

Korman

#16
Nov 22, 2010, 01:09 am Last Edit: Nov 22, 2010, 01:16 am by Korman Reason: 1
Quote
This seems so basic to have some sort of run time clock on an arduino device.  Why is this so difficult?


Because you mess around. Back to your original code sample, when applying the hint you got about the Time library, you might get something like this:

Code: [Select]
sprintf(tweet,"%2.2d:%2.2d:%2.2d Sensor Reading S1=%e S2=%e",
hour(), minute(), second(),
(float)temp_f, (float)real_humidity);


And don't forget to include the Time library at the top of your sketch with this:
Code: [Select]
#include <Time.h>

Does that look like a start? If you want to have time match the real time closer, you can set it anywhere in your program with:
Code: [Select]
setTime(hr,min,sec,day,month,yr);
This leaves only the small detail of getting good values for the variables hr, min, sec, day, month and yr. This can be achieved in various ways, but that we can leave for later. For experimenting you always can put that line in your setup() function:
Code: [Select]
setTime(10,55,0,22,11,2010);

Korman


PaulS

Quote
This seems so basic to have some sort of run time clock on an arduino device.  Why is this so difficult?

You are starting from a false assumption. For 99% of embedded projects, the current time as relayed by your wall clock is completely irrelevant.

Think of a washing machine. The controller needs to turn the motor on and off at specific times, and to open and close water valves at specific times. Those times are not based on the wall clock, though, They are relative to the time you push the start button.

Does the dishwasher or microwave need to know what time it is. No. Everything they need to do is based on the time you press the go button.

You are the one trying to force a device that doesn't know about wall clocks to pretend that it does.

If you read this page:
http://arduino.cc/en/Reference/HomePage
you will see what each function does, and how they can, and can not, be combined to produce useful results.

robtillaart

#18
Nov 22, 2010, 03:17 pm Last Edit: Nov 22, 2010, 03:18 pm by robtillaart Reason: 1
Reaction on your code feew postings back :

You should declare functions outside the loop(). Try this code
Code: [Select]

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

void loop()
{
 char *s;
 s = TimeToString(millis()/1000);
 Serial.println(s);
 delay(456);
}


// 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)

PaulS

Code: [Select]
 delay(456);
456? What significance does that value have?

robtillaart

Quote
456? What significance does that value have?
none, I just didn't want to wait 10 seconds to see it work :)
Rob Tillaart

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

.jacob

#21
Nov 23, 2010, 05:56 pm Last Edit: Nov 23, 2010, 05:56 pm by .jacob Reason: 1
Quote
Because you mess around. Back to your original code sample, when applying the hint you got about the Time library, you might get something like this:


Korman, you are the man!  Thank you so much.  It's working flawlessly after 24 hours now.  Thank you so much!

www.twitter.com/myhumidor

Go Up