1

I'm working on alarm clock project using Arduino Nano (v 3.0, clone) and RTC module ZS-042 (containing DS3231 and EEPROM AT24C32).

The intention is to load the hours and minutes from EEPROM on start-up, use current date (to avoid zero-date problems) and compose DateTime object in order to utilize the TimeSpan object, making time calculations easier than using plain integer math. The issue is, the value in DateTime object seems not constructed correctly, although all the components used for its construction seem OK.

With the following code

/* Hardware definitions */
//RTC using I2C, pins 4,5
#include <Wire.h> 
#include <RTClib.h>
RTC_DS3231 rtc;

#include <extEEPROM.h>
extEEPROM e2p(kbits_32, 1, 32, 0x57);

DateTime  alarm1Time;

void setup(){
  Wire.begin(); 
  rtc.begin();
  Serial.begin(115200);
  uint8_t e2pStatus = e2p.begin(extEEPROM::twiClock400kHz);      //go fast!
  DateTime t = rtc.now(); //get current time from RTC
  Serial.print("t.year: ");  Serial.print(t.year());
  Serial.print(" t.month: ");  Serial.print(t.month());
  Serial.print(" t.day: ");  Serial.println(t.day());
  alarm1Time = (t.year(),t.month(),t.day(),e2p.read(1),e2p.read(2),0);
  Serial.print("E2P : ");
  Serial.print(e2p.read(1));
  Serial.print(":");
  Serial.println(e2p.read(2));
  char buf1[] = "YYMMDD-hh:mm:ss";
  Serial.println(alarm1Time.toString(buf1));  
}

I get on the serial monitor the following:

t.year: 2021 t.month: 1 t.day: 31
E2P : 7:15
060206-06:28:16

The date components (year, month, date) correspond to current date. As the bytes on address 1 and 2 in EEPROM read 7 and 15, I'd expect the Alarm1Time be 2021-01-31, 07:15, which isn't. The output is constant, whatever the current date and the EEPROM stored time. Later in my code the alarm1Time parses into 6 hours and 28 minutes, in accordance with last line of the serial monitor output.

What could be (or better - where I'm) wrong? Thanks.

Libraries used:

RTClib (https://github.com/adafruit/RTClib)

extEEPROM (https://github.com/JChristensen/extEEPROM/blob/master/extEEPROM.h)

0
2

Here:

alarm1Time = (t.year(),t.month(),t.day(),e2p.read(1),e2p.read(2),0);

you are repeatedly using the comma operator, which essentially means “discard all the values but the last one”. If you want to invoke the DateTime constructor that takes these arguments, you should call it explicitly:

alarm1Time = DateTime(t.year(),t.month(),t.day(),e2p.read(1),e2p.read(2),0);

or, better yet, you can declare and initialize alarm1Time at the same time:

DateTime alarm1Time(t.year(),t.month(),t.day(),e2p.read(1),e2p.read(2),0);
1
  • You are absolutely right I'm incorrectly using the constructor. Am I so stupid or so tired? Hmmm. Probably it's because I'm neebie to C++, after years of using other languages. Regarding the declaration and initialization at once - I need to declare it global, not withing setup(), otherwise I'd do it as you suggest. Thanks. – murphy Feb 1 at 21:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.