Ds3231 RTC coding

Hi all, I use this coding to get the date and time from the ds3231 RTC. I can print out the hour and minute separately. I would like to be able to print out the current time using one line of code only. Is this possible? Getting the time in hours, minutes, seconds just as on a watch. Is there coding for this on just the one line? Time stamp prints date and time.

 DateTime now = rtc.now();
    Serial.println(now.timestamp());
    Serial.println(rtc.month());
    Serial.println(rtc.day());
    Serial.println((rtc.hour) ());
     Serial.println((rtc.minute) ());

===>

 DateTime now = rtc.now();
    Serial.print(now.timestamp()); Serial.print(' ');   //space
    Serial.print(rtc.month());     Serial.print(' '); 
    Serial.print(rtc.day());       Serial.print(' ');
    Serial.print((rtc.hour)());    Serial.print(' ');
     Serial.println((rtc.minute) ());

Study from here the difference etween Serial.print(); and Serial.println(); commands.

    char buffer[20];
    sprintf(buffer, "%d %d %d %d:%d", now.timestamp(), now.month(), now.day(), now.hour(), now.minute());
    Serial.println(buffer);  //one line of code

Which Arduino board are you using ?

@petercl14

You have been posting about the DS3231 numerous times using different libraries every time. How are we supposed to know which one you are currently using?

It would also be appreciated (at least by me) if you would post a small example that can be compiled.


Some libraries do have a method to display a formatted date and time (although possibly not in the format that you need). But we do not know which library your using.

What do you expect from a one-liner of code? A shorter loop() function? If so, you can always write a function to print a correctly formatted data and/or time and call that function when you need to print it.

It just depends on what library you are using, some RTC libraries have an option to print the formatted time. go through the cpp files for your RTC library and check if it has anywhere that says about the formatted time.

FYI: reading library code and documentation is a good way to learn more about the library you're using, and can make your life a lot easier at times

I guess I wasn't specific enough. I don't mean one line of code for the whole lot. Only one line of code for the time is what I meant. The library is ds323x. This is just part of a much larger sketch. In the extended sketch I have to put in the current time as on my watch.

Whatever it is that you want to print, why is it so imperative that it is done in a single line of code ?

What does that mean ?

Sorry about that . The library is ds323x. The code can be compiled. There is no error in it. You may be assuming that it could not be compiled. The only one line of code I am looking for is the time (hour, minute, second). It may print out in a format like this 'hour:minute:second'. In another sketch I entered time into the serial monitor like this for 4.15pm. It would not accept that. I had to use a decimal 16.25 input into the serial monitor to get 4.15pm.

I've used something like this in my sketches before

char timeBuff[20];
sprintf("%02d:%02d:02d", now.hour(), now.minute(), now.second());
Serial.println(timeBuff);

or if you are looking for even less:


Serial.printf("%02d:%02d:%02d", now.hour(), now.minute(), now.second());

02d just means it prints a 0 in front of the number if it is a single digit

Serial.printf("%02d:%02d:%02d", now.hour(), now.minute(), now.second());

Only on some boards, which is why I asked

But got no answer

1 Like

I am using the nano board. This sketch will be part of a larger sketch. The larger sketch asks for the local time. In a larger sketch I entered the time in the serial monitor like this for 4.15pm; 16.25 using a decimal here. .15 is a 1/4 of the way to the next hour. You only get the correct print out by doing this.
The library is ds323x. So it would be convenient to get the time back like that (16.25). I may be able to manipulate the hour and minute in the code to get a single input like 16.25.

No, I did not.

In which case you did something wrong. You will have to convert it to a 24 hour clock before setting it using rtc.minute() and rtc.hour().

That statement is still not clear to me.

Please provide your full sketch. I'm not quite sure what you are doing here

If you want to use 12 hour time, you can use something like this:

 switch(hour) {

    case 0:
      formattedHour = hour + 12;
    break;

    case 1 ... 11:
      formattedHour = hour;
    break;

    case 12:
      formattedHour = hour;
      break;

      case 13 ... 23:
        formattedHour = hour - 12;
      break;
  }

hour is just a variable that is equal to now.hour() and when printing the time, use the formattedHour variable. I just took that out a part of one of my own sketches. You can also add the meridiem in the switch case statement so you can print 'am' or 'pm' following the hour.

I am very confused as to what you are trying to do

What format is the larger sketch expecting the time to be entered in and what format do you want to display it in ?

What does manipulate mean? If you need a text, you have been given the example with sprintf in an earlier post.

What does input mean? Entering it in e.g. serial monitor?

That might do it. Your one line of code. UK bob wanted to know the board I was using as that code might not work on all boards. I am using the nano board.
Would the print read like this; 08:09:05? I am using the ds323x library.

arduino board is nano. library is ds323x. chrisd79 says I only need one line of code. This is it. Similar to your middle line but not quite the same. May make all the difference between using 1 line or 3 lines.
Serial.printf("%02d:%02d:%02d", now.hour(), now.minute(), now.second());

Serial.printf() is not available on the classic Nano, hence my suggestion of using sprintf() to a buffer, then printing the buffer

I still have no idea why you are so obsessed with using a single line of code

1 Like

It's oh so easy to test. Put that line of code in your sketch and compile. For a classic Nano, it will fail to compile.

size_t printTime()
{
    char buffer[20];
    sprintf(buffer, "%d %d %d %d:%d", now.timestamp(), now.month(), now.day(), now.hour(), now.minute());
    return Serial.println(buffer);
}

You can fine tune the function to print what is needed. The function returns the number of bytes that are printed (in case you seed it).

And call it when needed

void loop()
{
  printTime();  // one line of code
  delay(1000);
}

You entered what?
Please be specific - do you need to OUTPUT or INPUT time?