55 lines
1.2 KiB
C++
55 lines
1.2 KiB
C++
// Date and time functions using a DS1302 RTC
|
|
|
|
#include <RTClib.h>
|
|
|
|
// Init rtc object
|
|
// DS1302 rtc;
|
|
// DS1302 rtc(ce_pin, sck_pin, io_pin);
|
|
//
|
|
// ce_pin (RST): default 4
|
|
// sck_pin (CLK): default 5
|
|
// io_pin (DAT): default 6
|
|
DS1302 rtc;
|
|
//DS1302 rtc(8, 6, 7);
|
|
|
|
// buffer for DateTime.tostr
|
|
char buf[20];
|
|
|
|
void setup() {
|
|
Serial.begin(9600);
|
|
rtc.begin();
|
|
|
|
if (!rtc.isrunning()) {
|
|
Serial.println("RTC is NOT running!");
|
|
// following line sets the RTC to the date & time this sketch was compiled
|
|
rtc.adjust(DateTime(__DATE__, __TIME__));
|
|
}
|
|
}
|
|
|
|
void loop() {
|
|
DateTime now = rtc.now();
|
|
|
|
Serial.println(now.tostr(buf));
|
|
|
|
Serial.print(" since midnight 1970/01/01 = ");
|
|
Serial.print(now.unixtime());
|
|
Serial.print("s = ");
|
|
Serial.print(now.unixtime() / 86400L);
|
|
Serial.println("d");
|
|
|
|
// calculate a date which is 7 days and 30 seconds into the future
|
|
DateTime future(now + (7 * 86400L + 30));
|
|
|
|
Serial.print(" now + 7d + 30s: ");
|
|
Serial.println(future.tostr(buf));
|
|
|
|
// calculate a date which is 30 days before
|
|
DateTime past(now - TimeDelta(30 * 86400L));
|
|
|
|
Serial.print(" now - 30d: ");
|
|
Serial.println(past.tostr(buf));
|
|
|
|
Serial.println();
|
|
delay(3000);
|
|
}
|