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.
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.
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 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.
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.
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());