Using RTC and displaying seconds...

Hey guys,

I jsut got my weather station to work in its beta status but I noticed something that makes it hard to evaluate the data with excel.

I save the time with this kinda code:

  logfile.print(now.hour(), DEC);
  logfile.print(":");
  logfile.print(now.minute(), DEC);
  logfile.print(":");
  logfile.print(now.second(), DEC);

The result looks like: 16:53:4 this.
The problem I have: After splitting the data into colums I set the preferences of the time colum from “Standard” to “Time” (Format: HH:MM:SS)

The problem is, I save the time like this: HH:MM:S so excel can only translate the 4s of my example if it would save like :04 and not :4

How can I change this? Hopefully you guys get what I mean :slight_smile:

regards
Bastian

Edit: Would this work?

if (now.hour(),DEC < 10) {
  logfile.print("0"); }
  logfile.print(now.hour(), DEC);
  logfile.print(":");
if (now.minute(),DEC < 10) {
  logfile.print("0"); }
  logfile.print(now.minute(), DEC);
  logfile.print(":");
if (now.second(),DEC < 10) {
  logfile.print("0"); }
  logfile.print(now.second(), DEC);

No. What if the second changes from 9 to 10 between you printing the extra zero and reading it again?

Without posting the library you are using, we can't tell if now is a variable you control of it it goes back to the RTC each time to update itself.

Thanks!

I use the RTClib.h from here.
Its from the SD Card shield guide!

Would this work?

DateTime now = rtc.now()

if (now.hour(),DEC < 10) {
  logfile.print("0"); }
  logfile.print(now.hour(), DEC);
  logfile.print(":");
if (now.minute(),DEC < 10) {
  logfile.print("0"); }
  logfile.print(now.minute(), DEC);
  logfile.print(":");
if (now.second(),DEC < 10) {
  logfile.print("0"); }
  logfile.print(now.second(), DEC);

You need to call DateTime now = rtc.now() to update the time at the start of the routine.

Without posting the library you are using, we can’t tell if now is a variable you control of it it goes back to the RTC each time to update itself.

With RTClib.h now() is referenced to a call to the RTC

man1ac: Its from the SD Card shield guide!

This information was not included in any of your earlier posts!

man1ac:
The result looks like: 16:53:4 this.
The problem I have: After splitting the data into colums I set the preferences of the time colum from “Standard” to “Time” (Format: HH:MM:SS)

The problem is, I save the time like this: HH:MM:S so excel can only translate the 4s of my example if it would save like :04 and not :4

How can I change this? Hopefully you guys get what I mean :slight_smile:

It’s a bit garbled but I understand your problem is one of leading zeros. I don’t believe this is an Arduino problem, it is an Excel problem. Once the column is properly formatted, Excel sorts out your ragged input.

I use code similar to yours

           myFile = SD.open(filename, FILE_WRITE);//<<<<<<<<<<<<< OPEN
  myFile.print(hour);
  myFile.print(":");
  myFile.print(minute);
  myFile.print(":");
  myFile.print(second);
  myFile.print(",");

  myFile.print(InTemp);
  myFile.print(",");
  myFile.print(OutTemp);
  myFile.print(",");
  myFile.print(DrainTemp);
  myFile.print(",");
  myFile.println(ShrTemp);
       myFile.close();//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>CLOSE

i.e. all the time stuff in one column, and Office 2000 supplies the zeros as required. This in the same way as, when I enter year, I enter “16” and it gets changed to “2016”.

You can see that Cattledog has a leading zero inserted on condition of single digit numbers but this is only needed for dumb stuff like the serial monitor, or a graphic display.

So can you maybe help me an tell me how to formatt the data in excel?

I would use cattledog's solution in reply #3 to get the correct data format. Next I would read the file that is written to the SD card with a text editor to verify the data. It should have the format that you specified in the code.

If that is like '04:33:05', excel will properly use it as a time (4:33:05 AM) and display 4:33:05 in the cell (on my system, Excel2013). What you see might depend on the default settings of your PC or Excel; I never care. If the removal of that leading zero in '04' is an issue, you can format the cell properly.

The best way to display the data literally is by opening excel and using data import; make sure to select 'text' for the field type and not 'general'.

if (now.minute(),DEC < 10) {

That most certainly does NOT do what you seem to think it does. 10 will never be less than 10. (If that doesn’t make sense, google the comma operator, which you are abusing.)

man1ac:
So can you maybe help me an tell me how to formatt the data in excel?

The pic below is so obvious I’m not sure it can be that enlightening. I think your answer lies in reply #8. I did not realise your code was not kosher, as I don’t use that method.