first commit
This commit is contained in:
59
libraries/RTCLib_by_NeiroN/examples/datecalc/datecalc.ino
Normal file
59
libraries/RTCLib_by_NeiroN/examples/datecalc/datecalc.ino
Normal file
@@ -0,0 +1,59 @@
|
||||
// Simple date conversions and calculations
|
||||
|
||||
// #include <Wire.h>
|
||||
#include <RTClib.h>
|
||||
|
||||
void showDate(const char* txt, const DateTime& dt) {
|
||||
// buffer for DateTime.tostr
|
||||
static char buf[20];
|
||||
|
||||
Serial.print(txt);
|
||||
Serial.print(' ');
|
||||
Serial.print(dt.tostr(buf));
|
||||
|
||||
Serial.print(" = ");
|
||||
Serial.print(dt.unixtime());
|
||||
Serial.print("s / ");
|
||||
Serial.print(dt.unixtime() / 86400L);
|
||||
Serial.print("d since 1970");
|
||||
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
|
||||
DateTime dt0(0, 1, 1, 0, 0, 0);
|
||||
showDate("dt0", dt0);
|
||||
|
||||
DateTime dt1(1, 1, 1, 0, 0, 0);
|
||||
showDate("dt1", dt1);
|
||||
|
||||
DateTime dt2(2009, 1, 1, 0, 0, 0);
|
||||
showDate("dt2", dt2);
|
||||
|
||||
DateTime dt3(2009, 1, 2, 0, 0, 0);
|
||||
showDate("dt3", dt3);
|
||||
|
||||
DateTime dt4(2009, 1, 27, 0, 0, 0);
|
||||
showDate("dt4", dt4);
|
||||
|
||||
DateTime dt5(2009, 2, 27, 0, 0, 0);
|
||||
showDate("dt5", dt5);
|
||||
|
||||
DateTime dt6(2009, 12, 27, 0, 0, 0);
|
||||
showDate("dt6", dt6);
|
||||
|
||||
DateTime dt7(dt6.unixtime() + 3600); // one hour later
|
||||
showDate("dt7", dt7);
|
||||
|
||||
DateTime dt8(dt6.unixtime() + 86400L); // one day later
|
||||
showDate("dt8", dt8);
|
||||
|
||||
DateTime dt9(dt6 + TimeDelta(7 * 86400L)); // one week later
|
||||
showDate("dt9", dt9);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// do nothing
|
||||
}
|
||||
54
libraries/RTCLib_by_NeiroN/examples/ds1302/ds1302.ino
Normal file
54
libraries/RTCLib_by_NeiroN/examples/ds1302/ds1302.ino
Normal file
@@ -0,0 +1,54 @@
|
||||
// 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);
|
||||
}
|
||||
51
libraries/RTCLib_by_NeiroN/examples/ds1307/ds1307.ino
Normal file
51
libraries/RTCLib_by_NeiroN/examples/ds1307/ds1307.ino
Normal file
@@ -0,0 +1,51 @@
|
||||
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
|
||||
|
||||
#include <Wire.h>
|
||||
#include <RTClib.h>
|
||||
|
||||
DS1307 rtc;
|
||||
|
||||
// buffer for DateTime.tostr
|
||||
char buf[20];
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
#ifdef AVR
|
||||
Wire.begin();
|
||||
#else
|
||||
Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
|
||||
#endif
|
||||
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 1/1/1970 = ");
|
||||
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);
|
||||
}
|
||||
72
libraries/RTCLib_by_NeiroN/examples/ds13_ram/ds13_ram.ino
Normal file
72
libraries/RTCLib_by_NeiroN/examples/ds13_ram/ds13_ram.ino
Normal file
@@ -0,0 +1,72 @@
|
||||
// Example for using internal RAM on DS series chips
|
||||
|
||||
#include <Wire.h>
|
||||
#include <RTClib.h>
|
||||
|
||||
// For DS1302 pin configuration, please check ds1302 example
|
||||
DS1302 rtc;
|
||||
//DS1307 rtc;
|
||||
//DS3231 rtc;
|
||||
|
||||
#define BUFSIZE DS1302_RAMSIZE
|
||||
//#define BUFSIZE DS1307_RAMSIZE
|
||||
|
||||
// buffer
|
||||
char buf[BUFSIZE];
|
||||
|
||||
void printArray(const byte* ptr, int len) {
|
||||
Serial.print("[");
|
||||
for (int i = 0; i < len - 1; i++) {
|
||||
Serial.print(*(ptr + i), DEC);
|
||||
Serial.print(", ");
|
||||
}
|
||||
Serial.print(*(ptr + len - 1), DEC);
|
||||
Serial.println("]");
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
Wire.begin(); // for DS1307
|
||||
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__));
|
||||
}
|
||||
|
||||
byte b = 63;
|
||||
Serial.print("Set first byte of RAM to: ");
|
||||
rtc.writeram(0, b);
|
||||
Serial.println(b);
|
||||
|
||||
Serial.print("Get first byte of RAM: ");
|
||||
b = rtc.readram(0);
|
||||
Serial.println(b);
|
||||
Serial.println();
|
||||
|
||||
// test data
|
||||
for (int i = 0; i < BUFSIZE; i++) buf[i] = i;
|
||||
|
||||
rtc.putram(buf, BUFSIZE);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
static int counter = 0;
|
||||
|
||||
DateTime now = rtc.now();
|
||||
|
||||
Serial.println("Previous data in RAM:");
|
||||
rtc.getram(buf, BUFSIZE);
|
||||
printArray(buf, BUFSIZE);
|
||||
|
||||
Serial.println("Setting new data to RAM:");
|
||||
snprintf(buf, BUFSIZE, "%s count %d", now.tostr(buf), counter);
|
||||
Serial.println(buf);
|
||||
rtc.putram(buf, BUFSIZE);
|
||||
|
||||
counter++;
|
||||
|
||||
Serial.println();
|
||||
delay(3000);
|
||||
}
|
||||
51
libraries/RTCLib_by_NeiroN/examples/ds3231/ds3231.ino
Normal file
51
libraries/RTCLib_by_NeiroN/examples/ds3231/ds3231.ino
Normal file
@@ -0,0 +1,51 @@
|
||||
// Example for DS3231 module connected via I2C interface
|
||||
|
||||
#include <RTClib.h>
|
||||
|
||||
DS3231 rtc;
|
||||
|
||||
// buffer for DateTime.tostr
|
||||
char buf[20];
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
#ifdef AVR
|
||||
Wire.begin();
|
||||
#else
|
||||
Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
|
||||
#endif
|
||||
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/1/1 = ");
|
||||
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 - 30 * 86400L);
|
||||
|
||||
Serial.print(" now - 30d: ");
|
||||
Serial.println(past.tostr(buf));
|
||||
|
||||
Serial.println();
|
||||
delay(3000);
|
||||
}
|
||||
53
libraries/RTCLib_by_NeiroN/examples/pcf_alarm/pcf_alarm.ino
Normal file
53
libraries/RTCLib_by_NeiroN/examples/pcf_alarm/pcf_alarm.ino
Normal file
@@ -0,0 +1,53 @@
|
||||
#include <Wire.h>
|
||||
#include <RTClib.h>
|
||||
|
||||
PCF8563 rtc;
|
||||
DateTime alarm_l;
|
||||
const byte rtc_int = 2;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
Serial.println("start");
|
||||
pinMode(rtc_int, INPUT);
|
||||
attachInterrupt(digitalPinToInterrupt(rtc_int), interrupt, FALLING);
|
||||
Wire.begin();
|
||||
rtc.begin();
|
||||
|
||||
if (!rtc.isrunning()) {
|
||||
Serial.println("RTC is NOT running!");
|
||||
rtc.adjust(DateTime(__DATE__, __TIME__));
|
||||
}
|
||||
else {
|
||||
Serial.println("RTC OK");
|
||||
DateTime now = DateTime(2020, 4, 1, 12, 00, 0);
|
||||
rtc.adjust(now);
|
||||
|
||||
setAlarm(1);
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
DateTime now = rtc.now();
|
||||
char buf[100];
|
||||
strncpy(buf, "DD.MM.YYYY hh:mm:ss", 100);
|
||||
Serial.println(now.format(buf));
|
||||
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
void interrupt() {
|
||||
Serial.println("WakeUp atmega");
|
||||
}
|
||||
|
||||
void setAlarm(int value) {
|
||||
char buf[100];
|
||||
strncpy(buf, "DD.MM.YYYY hh:mm:ss", 100);
|
||||
|
||||
DateTime alarm = rtc.now();
|
||||
alarm.setminute(alarm.minute() + value);
|
||||
Serial.print("Setting alarm: ");
|
||||
strncpy(buf, "DD hh:mm MM", 100);
|
||||
Serial.println(alarm.format(buf));
|
||||
rtc.set_alarm(alarm, {1, 0, 0, 0});
|
||||
rtc.on_alarm();
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
// DateTime format utility example
|
||||
|
||||
#include <Wire.h>
|
||||
#include <RTClib.h>
|
||||
|
||||
DS1307 rtc;
|
||||
//DS1302 rtc; // see ds1302 example for pin configuration
|
||||
//DS3231 rtc;
|
||||
//PCF8563 rtc;
|
||||
//PCF8583 rtc;
|
||||
//RTC_Millis rtc;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
Wire.begin();
|
||||
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();
|
||||
char buf[100];
|
||||
strncpy(buf, "YYYY.MM.DD hh:mm:ss", 100);
|
||||
Serial.println(now.format(buf));
|
||||
delay(1000);
|
||||
}
|
||||
31
libraries/RTCLib_by_NeiroN/examples/softrtc/softrtc.ino
Normal file
31
libraries/RTCLib_by_NeiroN/examples/softrtc/softrtc.ino
Normal file
@@ -0,0 +1,31 @@
|
||||
// Date and time functions using just software, based on millis() & timer
|
||||
|
||||
#include <RTClib.h>
|
||||
|
||||
RTC_Millis rtc;
|
||||
|
||||
// buffer for DateTime.tostr
|
||||
char buf[20];
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
// following line sets the RTC to the date & time this sketch was compiled
|
||||
rtc.begin(DateTime(__DATE__, __TIME__));
|
||||
}
|
||||
|
||||
void loop() {
|
||||
DateTime now = rtc.now();
|
||||
|
||||
Serial.println(now.tostr(buf));
|
||||
|
||||
Serial.print(" seconds since 1970: ");
|
||||
Serial.println(now.unixtime());
|
||||
|
||||
// calculate a date which is 7 days and 30 seconds into the future
|
||||
DateTime future(now + (7 * 86400L + 30));
|
||||
|
||||
Serial.println(future.tostr(buf));
|
||||
|
||||
Serial.println();
|
||||
delay(3000);
|
||||
}
|
||||
Reference in New Issue
Block a user