DS3231 dateFormat apparently not working, though dateTime has value

I'm new to Arduino and the various sensors and tools, though I'm not new to C.

I'm trying to implement a datalogger using a real time clock module. I wired up my sample board to my mega, got a red light on the clock, and started testing some output along the lines of ...

DS3231 clock;
RTCDateTime dt;

void setup()
{
  clock.begin();
  clock.setDateTime(2018, 12, 18, 09, 01, 00);
}

void loop()
{
  dt = clock.getDateTime();
  Serial.print("Date Time:");
  Serial.println(clock.dateFormat("d-m-Y H:i:s", dt));
  delay(1000);
}

When I run this, I get nothing but ... "Date Time:" followed by no value.

Being new to Arduino, I naturally assumed the problem was in the wiring, so I checked and rechecked, taking the clock off the breadboard and eventually wiring it up directly. But I got the same results.

However, I finally thought to try something else. I added the line

Serial.println(dt.year);

And up popped the results "2018". I can get results for every other part of the datetime structure, down to the second. So it seems that the real time clock is up, running, and returning a dateTime structure in dt.

However, no matter what string I try, dateFormat returns nada. I've looked through numerous examples and don't see what I'm missing -- but I'm sure I'm missing something blindingly obvious.

And suggestions before I start just making my own format routine? Thanks.

Look at this example : you forgot to add the following lines in the header of your code

#include <Wire.h>
#include <DS3231.h>

Also make sure you use the same library as there may be several with the same name.

No, sorry, I did include the headers in my code. I just didn't show them in the text I typed above (because, of course, without the data structure definition the example would not have compiled).

The clock seems to be working fine. I can do:

dt = clock.getDateTime();
Serial.println(dt.Second);

and watch it counting away.

However, even though it's all one library, the dateFormat doesn't seem to return anything from the RTCDateTime, no matter how I format the string. I've gone through numerous examples, and get only blanks in reply -- even if asking for a single value. It doesn't generate an error. Just ... nothing.

Handing dates is always a PITB, so it would be nice if the routine were working. Though, for a simple data logger, there's really nothing that's stopping me from simply writing my own little format routine.

Devilstower:
No, sorry, I did include the headers in my code. I just didn't show them in the text I typed above (because, of course, without the data structure definition the example would not have compiled).

For most effective help, always post a complete program that actually compiles and demonstrates your problem. Otherwise, people have to guess what you left out. Not a good idea to make more work for people who are trying to help you FOR FREE.

No Serial.begin also or just didn't show it ? :slight_smile:
Just copy / paste the example I pointed you to and run it: what does the monitor display?

If I use the example you linked, I get this in the results:

12:35:09.298 → Initialize DS3231
12:35:09.298 → Long number format:
12:35:09.332 → Long format with month name:
12:35:09.365 → Short format witch 12h mode:
12:35:09.398 → Today is: days of the year.
12:35:09.465 → Actual month has: days.
12:35:09.499 → Unixtime:
12:35:09.532 →

Which was also what I was seeing from my dateFormat code.

However, if I got back into the code and add a dt.println for dt.month, dt.day, dt.hour, dt.minute, and dt.second after the Unixtime, I see reasonable values. For example …

12:46:58.311 → Initialize DS3231
12:46:58.344 → Long number format:
12:46:58.378 → Long format with month name:
12:46:58.412 → Short format witch 12h mode:
12:46:58.450 → Today is: days of the year.
12:46:58.484 → Actual month has: days.
12:46:58.520 → Unixtime:
12:46:58.557 → 12 23 12:46:53
12:46:58.590 →
12:46:59.527 → Long number format:
12:46:59.560 → Long format with month name:
12:46:59.596 → Short format witch 12h mode:
12:46:59.630 → Today is: days of the year.
12:46:59.667 → Actual month has: days.
12:46:59.703 → Unixtime:
12:46:59.736 → 12 23 12:46:54

This is what’s puzzling me. The values of dt seem to be populated, and the values if I check them individually appear reasonable. However, dateFormat returns blanks every time.

That Jarzebski library looks complicated, and it is certainly a RAM hog. And some of the code for formatting dates/times looks weird. I wonder what's with all the (const char *) I see in there.

You should probably just write your own. Use sprintf(), or if that's too much of a resource hog, just compose your string "manually" one character at a time from digits and punctuation.

Thanks. Since I'm new to Arduino, I just didn't want to miss anything obvious. But I think this is going to be a place where i just shrug and move on.

I've managed to get my oddball little Catalex SD card module writing and so I'm now cheerfully logging data. I'll add the date values to each line straight off the dt structure, skip the formatting and call it a day.

With the barometric pressure sensor, altimeter, SD card, and real time clock all wired up, I'm about halfway to the flight computer for my high altitude balloon. Now it's time to tear open the envelope on my GPS module.