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)