First Commit

This commit is contained in:
MindCreeper03
2025-02-27 19:31:50 +01:00
parent bcbb6aff9a
commit e490df1715
2470 changed files with 1479965 additions and 0 deletions

View File

@@ -0,0 +1,167 @@
/*
AdvanceAlarm.ino
David Sparks Sept 2022
Example of advancing an alarm by an arbitrary interval of time.
Hardware setup:
Connect DS3231 SQW pin to Arduino interrupt pin 2
Builds upon ArduinoInterrupt.ino example by
Jacob Nuernberg August 2022
Example on using recurring interrupts
of arbitrary frequency with DS3231 alarms.
Hardware setup:
Connect DS3231 SQW pin to Arduino interrupt pin 2
Tested on:
- Arduino Nano (ATmega328P)
*/
#include <DS3231.h>
#include <Wire.h>
// Interrupt frequency, in seconds
#define INT_FREQ 3UL // 3 seconds, characterized as unsigned long
// myRTC interrupt pin
#define CLINT 2
// Setup clock
DS3231 myRTC;
// Variables for use in method parameter lists
byte alarmDay;
byte alarmHour;
byte alarmMinute;
byte alarmSecond;
byte alarmBits;
bool alarmDayIsDay;
bool alarmH12;
bool alarmPM;
// Interrupt signaling byte
volatile byte tick = 1;
void setup() {
// Begin I2C communication
Wire.begin();
// Begin Serial communication
Serial.begin(9600);
while (!Serial);
// Set the DS3231 clock mode to 24-hour
myRTC.setClockMode(false); // false = not using the alternate, 12-hour mode
// Set the clock to an arbitrarily chosen time of
// 00:00:00 midnight the morning of January 1, 2020
// using a suitable Unix-style timestamp
myRTC.setEpoch(1640995200);
// Assign parameter values for Alarm 1
alarmDay = myRTC.getDate();
alarmHour = myRTC.getHour(alarmH12, alarmPM);
alarmMinute = myRTC.getMinute();
alarmSecond = INT_FREQ; // initialize to the interval length
alarmBits = 0b00001110; // Alarm 1 when seconds match
alarmDayIsDay = false; // using date of month
// Upload initial parameters of Alarm 1
myRTC.turnOffAlarm(1);
myRTC.setA1Time(
alarmDay, alarmHour, alarmMinute, alarmSecond,
alarmBits, alarmDayIsDay, alarmH12, alarmPM);
// clear Alarm 1 flag after setting the alarm time
myRTC.checkIfAlarm(1);
// now it is safe to enable interrupt output
myRTC.turnOnAlarm(1);
// When using interrupt with only one of the DS3231 alarms, as in this example,
// it may be possible to prevent the other alarm entirely,
// so it will not covertly block the outgoing interrupt signal.
// Try to prevent Alarm 2 altogether by assigning a
// nonsensical alarm minute value that cannot match the clock time,
// and an alarmBits value to activate "when minutes match".
alarmMinute = 0xFF; // a value that will never match the time
alarmBits = 0b01100000; // Alarm 2 when minutes match, i.e., never
// Upload the parameters to prevent Alarm 2 entirely
myRTC.setA2Time(
alarmDay, alarmHour, alarmMinute,
alarmBits, alarmDayIsDay, alarmH12, alarmPM);
// disable Alarm 2 interrupt
myRTC.turnOffAlarm(2);
// clear Alarm 2 flag
myRTC.checkIfAlarm(2);
// NOTE: both of the alarm flags must be clear
// to enable output of a FALLING interrupt
// attach clock interrupt
pinMode(CLINT, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(CLINT), isr_TickTock, FALLING);
// Configure the LED for blinking
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
// static variable to keep track of LED on/off state
static byte state = false;
// Do when alarm interrupt received:
if (tick) {
// right away, capture the current time in a DateTime variable
// for later processing
DateTime alarmDT = RTClib::now();
// disable Alarm 1 interrupt
myRTC.turnOffAlarm(1);
// Clear Alarm 1 flag
myRTC.checkIfAlarm(1);
tick = 0; // reset the local interrupt-received flag
state = ~state; // reverse the state of the LED
digitalWrite(LED_BUILTIN, state);
// optional serial output
Serial.print("Turning LED ");
Serial.print((state ? "ON" : "OFF"));
Serial.print(" at ");
Serial.print(alarmDT.hour());
Serial.print(":");
Serial.print(alarmDT.minute());
Serial.print(":");
Serial.println(alarmDT.second());
// extract the DateTime values as a timestamp
uint32_t nextAlarm = alarmDT.unixtime();
// add the INT_FREQ number of seconds
nextAlarm += INT_FREQ;
// update the DateTime with the new timestamp
alarmDT = DateTime(nextAlarm);
// upload the new time to Alarm 1
myRTC.setA1Time(
alarmDT.day(), alarmDT.hour(), alarmDT.minute(), alarmDT.second(),
alarmBits, alarmDayIsDay, alarmH12, alarmPM);
// enable Alarm 1 interrupts
myRTC.turnOnAlarm(1);
// clear Alarm 1 flag again after enabling interrupts
myRTC.checkIfAlarm(1);
}
}
void isr_TickTock() {
// interrupt signals to loop
tick = 1;
return;
}

View File

@@ -0,0 +1,139 @@
/*
AlarmInterrupt.ino
Jacob Nuernberg
08/22
Example on using interrupts with DS3231 alarms.
Hardware setup:
Connect DS3231 SQW pin to Arduino interrupt pin 2
Tested on:
- Arduino UNO
- Arduino nano
Added to this example:
1. Descriptively named variables to pass parameter values.
2. Modify AlarmBits for Alarm 1 to 0b00001111,
for clarity, because Alarm 1 uses only bits 3:0.
3. Add code to prevent Alarm 2 from interfering with the interrupt,
by setting A2Minute to a value that can never match the time
and setting AlarmBits to 0b01100000: alarm "when minutes match".
Also clear the A2 alarm flag.
David Sparks, September 2022
*/
#include <DS3231.h>
#include <Wire.h>
// myRTC interrupt pin
#define CLINT 2
// Setup clock
DS3231 myRTC;
// Variables for use in method parameter lists
byte alarmDay;
byte alarmHour;
byte alarmMinute;
byte alarmSecond;
byte alarmBits;
bool alarmDayIsDay;
bool alarmH12;
bool alarmPM;
// Interrupt signaling byte
volatile byte tick = 1;
void setup() {
// Begin I2C communication
Wire.begin();
// Begin Serial communication
Serial.begin(9600);
while (!Serial);
Serial.println();
Serial.println("Starting Serial");
// Assign parameter values for Alarm 1
alarmDay = 0;
alarmHour = 0;
alarmMinute = 0;
alarmSecond = 0;
alarmBits = 0b00001111; // Alarm 1 every second
alarmDayIsDay = false;
alarmH12 = false;
alarmPM = false;
// Set alarm 1 to fire at one-second intervals
myRTC.turnOffAlarm(1);
myRTC.setA1Time(
alarmDay, alarmHour, alarmMinute, alarmSecond,
alarmBits, alarmDayIsDay, alarmH12, alarmPM);
// enable Alarm 1 interrupts
myRTC.turnOnAlarm(1);
// clear Alarm 1 flag
myRTC.checkIfAlarm(1);
// When using interrupt with only one of the DS3231 alarms, as in this example,
// it may be advisable to prevent the other alarm entirely,
// so it will not covertly block the outgoing interrupt signal.
// Prevent Alarm 2 altogether by assigning a
// nonsensical alarm minute value that cannot match the clock time,
// and an alarmBits value to activate "when minutes match".
alarmMinute = 0xFF; // a value that will never match the time
alarmBits = 0b01100000; // Alarm 2 when minutes match, i.e., never
// Upload the parameters to prevent Alarm 2 entirely
myRTC.setA2Time(
alarmDay, alarmHour, alarmMinute,
alarmBits, alarmDayIsDay, alarmH12, alarmPM);
// disable Alarm 2 interrupt
myRTC.turnOffAlarm(2);
// clear Alarm 2 flag
myRTC.checkIfAlarm(2);
// NOTE: both of the alarm flags must be clear
// to enable output of a FALLING interrupt
// attach clock interrupt
pinMode(CLINT, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(CLINT), isr_TickTock, FALLING);
// Use builtin LED to blink
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
// static variable to keep track of LED on/off state
static byte state = false;
// if alarm went of, do alarm stuff
if (tick) {
tick = 0;
state = ~state;
digitalWrite(LED_BUILTIN, state);
// optional serial output
Serial.print("Turning LED ");
Serial.println((state ? "ON" : "OFF"));
// Clear Alarm 1 flag
myRTC.checkIfAlarm(1);
}
// Loop delay to emulate other running code
delay(10);
}
void isr_TickTock() {
// interrupt signals to loop
tick = 1;
return;
}

View File

@@ -0,0 +1,50 @@
/*
AlarmPolilng.ino
Jacob Nuernberg
08/22
Example on using DS3231 alarms with polling and test checkIfAlarm()
Tested on:
- Arduino nano
*/
#include <DS3231.h>
#include <Wire.h>
// Setup clock
DS3231 myRTC;
void setup() {
// Begin I2C communication
Wire.begin();
// Setup alarm one to fire every second
myRTC.turnOffAlarm(1);
myRTC.setA1Time(0, 0, 0, 0, 0b01111111, false, false, false);
myRTC.turnOnAlarm(1);
myRTC.checkIfAlarm(1);
// Use builtin LED to blink
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
}
void loop() {
// static variable to keep track of LED on/off state
static byte state = false;
// if alarm went of, do alarm stuff
// first call to checkIFAlarm does not clear alarm flag
if (myRTC.checkIfAlarm(1, false)) {
state = ~state;
digitalWrite(LED_BUILTIN, state);
// Clear alarm state
myRTC.checkIfAlarm(1, true);
}
// Loop delay to emulate other running code
delay(10);
}

View File

@@ -0,0 +1,38 @@
/*
oscillator_test.pde
Eric Ayars
4/11
Test/demo of oscillator routines for a DS3231 RTC.
Use a scope after loading this to check if things are
working as they should.
*/
#include <DS3231.h>
#include <Wire.h>
DS3231 myRTC;
byte j;
bool on = false;
void setup() {
// Start the I2C interface
Wire.begin();
// Start the serial interface
Serial.begin(57600);
}
void loop() {
for (j=0;j<4;j++) {
// invert state of 32kHz oscillator.
on = !on;
myRTC.enable32kHz(on);
// Turn on oscillator pin, frequency j
myRTC.enableOscillator(true, false, j);
delay(4000);
}
// So... The 32kHz oscillator (pin 1) will turn on or off once each 2s,
// and the oscillator out pin (pin 3) will cycle through frequencies.
}

View File

@@ -0,0 +1,113 @@
/*
DS3231_set.pde
Eric Ayars
4/11
Test of set-time routines for a DS3231 RTC
*/
#include <DS3231.h>
#include <Wire.h>
DS3231 myRTC;
byte year;
byte month;
byte date;
byte dOW;
byte hour;
byte minute;
byte second;
void getDateStuff(byte& year, byte& month, byte& date, byte& dOW,
byte& hour, byte& minute, byte& second) {
// Call this if you notice something coming in on
// the serial port. The stuff coming in should be in
// the order YYMMDDwHHMMSS, with an 'x' at the end.
boolean gotString = false;
char inChar;
byte temp1, temp2;
char inString[20];
byte j=0;
while (!gotString) {
if (Serial.available()) {
inChar = Serial.read();
inString[j] = inChar;
j += 1;
if (inChar == 'x') {
gotString = true;
}
}
}
Serial.println(inString);
// Read year first
temp1 = (byte)inString[0] -48;
temp2 = (byte)inString[1] -48;
year = temp1*10 + temp2;
// now month
temp1 = (byte)inString[2] -48;
temp2 = (byte)inString[3] -48;
month = temp1*10 + temp2;
// now date
temp1 = (byte)inString[4] -48;
temp2 = (byte)inString[5] -48;
date = temp1*10 + temp2;
// now Day of Week
dOW = (byte)inString[6] - 48;
// now hour
temp1 = (byte)inString[7] -48;
temp2 = (byte)inString[8] -48;
hour = temp1*10 + temp2;
// now minute
temp1 = (byte)inString[9] -48;
temp2 = (byte)inString[10] -48;
minute = temp1*10 + temp2;
// now second
temp1 = (byte)inString[11] -48;
temp2 = (byte)inString[12] -48;
second = temp1*10 + temp2;
}
void setup() {
// Start the serial port
Serial.begin(57600);
// Start the I2C interface
Wire.begin();
}
void loop() {
// If something is coming in on the serial line, it's
// a time correction so set the clock accordingly.
if (Serial.available()) {
getDateStuff(year, month, date, dOW, hour, minute, second);
myRTC.setClockMode(false); // set to 24h
//setClockMode(true); // set to 12h
myRTC.setYear(year);
myRTC.setMonth(month);
myRTC.setDate(date);
myRTC.setDoW(dOW);
myRTC.setHour(hour);
myRTC.setMinute(minute);
myRTC.setSecond(second);
// Test of alarm functions
// set A1 to one minute past the time we just set the clock
// on current day of week.
myRTC.setA1Time(dOW, hour, minute+1, second, 0x0, true,
false, false);
// set A2 to two minutes past, on current day of month.
myRTC.setA2Time(date, hour, minute+2, 0x0, false, false,
false);
// Turn on both alarms, with external interrupt
myRTC.turnOnAlarm(1);
myRTC.turnOnAlarm(2);
}
delay(1000);
}

View File

@@ -0,0 +1,157 @@
/*
DS3231_test.pde
Eric Ayars
4/11
Test/demo of read routines for a DS3231 RTC.
Turn on the serial monitor after loading this to check if things are
working as they should.
*/
#include <DS3231.h>
#include <Wire.h>
DS3231 myRTC;
bool century = false;
bool h12Flag;
bool pmFlag;
byte alarmDay, alarmHour, alarmMinute, alarmSecond, alarmBits;
bool alarmDy, alarmH12Flag, alarmPmFlag;
void setup() {
// Start the I2C interface
Wire.begin();
// Start the serial interface
Serial.begin(57600);
}
void loop() {
// send what's going on to the serial monitor.
// Start with the year
Serial.print("2");
if (century) { // Won't need this for 89 years.
Serial.print("1");
} else {
Serial.print("0");
}
Serial.print(myRTC.getYear(), DEC);
Serial.print(' ');
// then the month
Serial.print(myRTC.getMonth(century), DEC);
Serial.print(" ");
// then the date
Serial.print(myRTC.getDate(), DEC);
Serial.print(" ");
// and the day of the week
Serial.print(myRTC.getDoW(), DEC);
Serial.print(" ");
// Finally the hour, minute, and second
Serial.print(myRTC.getHour(h12Flag, pmFlag), DEC);
Serial.print(" ");
Serial.print(myRTC.getMinute(), DEC);
Serial.print(" ");
Serial.print(myRTC.getSecond(), DEC);
// Add AM/PM indicator
if (h12Flag) {
if (pmFlag) {
Serial.print(" PM ");
} else {
Serial.print(" AM ");
}
} else {
Serial.print(" 24h ");
}
// Display the temperature
Serial.print("T=");
Serial.print(myRTC.getTemperature(), 2);
// Tell whether the time is (likely to be) valid
if (myRTC.oscillatorCheck()) {
Serial.print(" O+");
} else {
Serial.print(" O-");
}
// Indicate whether an alarm went off
if (myRTC.checkIfAlarm(1)) {
Serial.print(" A1!");
}
if (myRTC.checkIfAlarm(2)) {
Serial.print(" A2!");
}
// New line on display
Serial.println();
// Display Alarm 1 information
Serial.print("Alarm 1: ");
myRTC.getA1Time(alarmDay, alarmHour, alarmMinute, alarmSecond, alarmBits, alarmDy, alarmH12Flag, alarmPmFlag);
Serial.print(alarmDay, DEC);
if (alarmDy) {
Serial.print(" DoW");
} else {
Serial.print(" Date");
}
Serial.print(' ');
Serial.print(alarmHour, DEC);
Serial.print(' ');
Serial.print(alarmMinute, DEC);
Serial.print(' ');
Serial.print(alarmSecond, DEC);
Serial.print(' ');
if (alarmH12Flag) {
if (alarmPmFlag) {
Serial.print("pm ");
} else {
Serial.print("am ");
}
}
if (myRTC.checkAlarmEnabled(1)) {
Serial.print("enabled");
}
Serial.println();
// Display Alarm 2 information
Serial.print("Alarm 2: ");
myRTC.getA2Time(alarmDay, alarmHour, alarmMinute, alarmBits, alarmDy, alarmH12Flag, alarmPmFlag);
Serial.print(alarmDay, DEC);
if (alarmDy) {
Serial.print(" DoW");
} else {
Serial.print(" Date");
}
Serial.print(" ");
Serial.print(alarmHour, DEC);
Serial.print(" ");
Serial.print(alarmMinute, DEC);
Serial.print(" ");
if (alarmH12Flag) {
if (alarmPmFlag) {
Serial.print("pm");
} else {
Serial.print("am");
}
}
if (myRTC.checkAlarmEnabled(2)) {
Serial.print("enabled");
}
// display alarm bits
Serial.println();
Serial.print("Alarm bits: ");
Serial.println(alarmBits, BIN);
Serial.println();
delay(1000);
}

View File

@@ -0,0 +1,51 @@
/*
Prints time stamps for 5 seconds using getXXX functions
Based on DS3231_set.pde
by Eric Ayars
4/11
Added printing back of time stamps and increased baud rate
(to better synchronize computer and RTC)
Andy Wickert
5/15/2011
*/
#include <DS3231.h>
#include <Wire.h>
DS3231 myRTC;
bool century = false;
bool h12Flag;
bool pmFlag;
void setup() {
// Start the serial port
Serial.begin(57600);
// Start the I2C interface
Wire.begin();
for (int i=0; i<5; i++){
delay(1000);
Serial.print(myRTC.getYear(), DEC);
Serial.print("-");
Serial.print(myRTC.getMonth(century), DEC);
Serial.print("-");
Serial.print(myRTC.getDate(), DEC);
Serial.print(" ");
Serial.print(myRTC.getHour(h12Flag, pmFlag), DEC); //24-hr
Serial.print(":");
Serial.print(myRTC.getMinute(), DEC);
Serial.print(":");
Serial.println(myRTC.getSecond(), DEC);
}
}
void loop() {
}

View File

@@ -0,0 +1,42 @@
// now.pde
// Prints a snapshot of the current date and time along with the UNIX time
// Modified by Andy Wickert from the JeeLabs / Ladyada RTC library examples
// 5/15/11
#include <Wire.h>
#include <DS3231.h>
RTClib myRTC;
void setup () {
Serial.begin(57600);
Wire.begin();
delay(500);
Serial.println("Nano Ready!");
}
void loop () {
delay(1000);
DateTime now = myRTC.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
Serial.print(" since midnight 1/1/1970 = ");
Serial.print(now.unixtime());
Serial.print("s = ");
Serial.print(now.unixtime() / 86400L);
Serial.println("d");
}

View File

@@ -0,0 +1,96 @@
#include <Wire.h>
#include <DS3231.h>
void showTimeFormated(time_t t) {
#if defined (__AVR__)
t -= 946684800UL;
#endif
char buffer[50];
struct tm *ptm;
ptm = gmtime (&t);
const char * timeformat {"%a %F %X - weekday %w; CW %W"};
strftime(buffer, sizeof(buffer), timeformat, ptm);
Serial.print(buffer);
Serial.print("\n");
}
constexpr time_t tstmp {1660644000UL};
RTClib myRTC;
DS3231 Clock;
void setup () {
Serial.begin(115200);
Wire.begin();
delay(500);
Serial.println("\n\n\nTest of DS3231 - setEpoch()\n\n\n");
#if defined (__AVR__)
#warning using AVR platform
Serial.println("AVR Microcontroller Ready!");
Wire.begin();
#elif defined (ESP8266)
Serial.println("ESP8266 Microcontroller Ready!");
#warning using espressif platform
// SDA = 0, SCL = 2
Wire.begin(0U, 2U);
#endif
// set the Ds3131 with a specific UnixTimestamp
// ==> Tue Aug 16 2022 10:00:00 GMT+0000 - weekday 2 (0 = Sunday); CW 33
// ==> 1660644000
Serial.println("Tue Aug 16 2022 10:00:00 GMT+0000 - weekday 2 (0 = Sunday); CW 33");
Serial.println("UnixTimestamp - 1660644000");
// feed UnixTimeStamp and don' t use localtime
Clock.setEpoch(tstmp, false);
// set to 24h
Clock.setClockMode(false);
// Just for verification of DS3231 Data
// check now the data from ESP8266 and DS3231
// get year
bool century = false;
bool h12Flag;
bool pmFlag;
DateTime now = myRTC.now();
Serial.print("\n\n");
Serial.print(" DateTime of DS3231: ");
Serial.print(Clock.getYear(), DEC);
Serial.print("-");
Serial.print(Clock.getMonth(century), DEC);
Serial.print("-");
Serial.print(Clock.getDate(), DEC);
Serial.print(" ");
Serial.print(Clock.getHour(h12Flag, pmFlag), DEC);
Serial.print(":");
Serial.print(Clock.getMinute(), DEC);
Serial.print(":");
Serial.print(Clock.getSecond(), DEC);
Serial.print(" - weekday ");
Serial.print(Clock.getDoW(), DEC);
Serial.println();
Serial.print("\n\n");
Serial.print(" DateTime of RTC: ");
Serial.print(now.year(), DEC);
Serial.print("-");
Serial.print(now.month(), DEC);
Serial.print("-");
Serial.print(now.day(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(":");
Serial.print(now.minute(), DEC);
Serial.print(":");
Serial.print(now.second(), DEC);
Serial.println();
Serial.print("\n\n Output of Struct tm: ");
showTimeFormated(tstmp);
}
void loop () {
}

View File

@@ -0,0 +1,182 @@
/*
Sets the time from input and prints back time stamps for 5 seconds
Based on DS3231_set.pde
by Eric Ayars
4/11
Added printing back of time stamps and increased baud rate
(to better synchronize computer and RTC)
Andy Wickert
5/15/2011
Clean for SAMD arch, add explanation, respect code-style and
fix interpretation of Serial input if used more than once
Olivier Staquet
4/26/2020
*/
#include <DS3231.h>
#include <Wire.h>
DS3231 myRTC;
byte year;
byte month;
byte date;
byte dow;
byte hour;
byte minute;
byte second;
bool century = false;
bool h12Flag;
bool pmFlag;
/*****************************************************************************************************
* Setup
* - Open Serial and Wire connection
* - Explain to the user how to use the program
*****************************************************************************************************/
void setup() {
// Start the serial port
Serial.begin(57600);
// Start the I2C interface
Wire.begin();
// Request the time correction on the Serial
delay(4000);
Serial.println("Format YYMMDDwhhmmssx");
Serial.println("Where YY = Year (ex. 20 for 2020)");
Serial.println(" MM = Month (ex. 04 for April)");
Serial.println(" DD = Day of month (ex. 09 for 9th)");
Serial.println(" w = Day of week from 1 to 7, 1 = Sunday (ex. 5 for Thursday)");
Serial.println(" hh = hours in 24h format (ex. 09 for 9AM or 21 for 9PM)");
Serial.println(" mm = minutes (ex. 02)");
Serial.println(" ss = seconds (ex. 42)");
Serial.println("Example for input : 2004095090242x");
Serial.println("-----------------------------------------------------------------------------");
Serial.println("Please enter the current time to set on DS3231 ended by 'x':");
}
/*****************************************************************************************************
* Loop
* - Each time we receive the time correction on the Serial
* - Set the clock of the DS3231
* - Echo the value from the DS3231 during 5 seconds
*****************************************************************************************************/
void loop() {
// If something is coming in on the serial line, it's
// a time correction so set the clock accordingly.
if (Serial.available()) {
inputDateFromSerial();
myRTC.setClockMode(false); // set to 24h
myRTC.setYear(year);
myRTC.setMonth(month);
myRTC.setDate(date);
myRTC.setDoW(dow);
myRTC.setHour(hour);
myRTC.setMinute(minute);
myRTC.setSecond(second);
// Give time at next five seconds
for (uint8_t i = 0; i < 5; i++){
delay(1000);
Serial.print(myRTC.getYear(), DEC);
Serial.print("-");
Serial.print(myRTC.getMonth(century), DEC);
Serial.print("-");
Serial.print(myRTC.getDate(), DEC);
Serial.print(" ");
Serial.print(myRTC.getHour(h12Flag, pmFlag), DEC); //24-hr
Serial.print(":");
Serial.print(myRTC.getMinute(), DEC);
Serial.print(":");
Serial.println(myRTC.getSecond(), DEC);
}
// Notify that we are ready for the next input
Serial.println("Please enter the current time to set on DS3231 ended by 'x':");
}
delay(1000);
}
/*****************************************************************************************************
* inputDateFromSerial
* - Read and interpret the data from the Serial input
* - Store the data in global variables
*****************************************************************************************************/
void inputDateFromSerial() {
// Call this if you notice something coming in on
// the serial port. The stuff coming in should be in
// the order YYMMDDwHHMMSS, with an 'x' at the end.
boolean isStrComplete = false;
char inputChar;
byte temp1, temp2;
char inputStr[20];
uint8_t currentPos = 0;
while (!isStrComplete) {
if (Serial.available()) {
inputChar = Serial.read();
inputStr[currentPos] = inputChar;
currentPos += 1;
// Check if string complete (end with "x")
if (inputChar == 'x') {
isStrComplete = true;
}
}
}
Serial.println(inputStr);
// Find the end of char "x"
int posX = -1;
for(uint8_t i = 0; i < 20; i++) {
if(inputStr[i] == 'x') {
posX = i;
break;
}
}
// Consider 0 character in ASCII
uint8_t zeroAscii = '0';
// Read Year first
temp1 = (byte)inputStr[posX - 13] - zeroAscii;
temp2 = (byte)inputStr[posX - 12] - zeroAscii;
year = temp1 * 10 + temp2;
// now month
temp1 = (byte)inputStr[posX - 11] - zeroAscii;
temp2 = (byte)inputStr[posX - 10] - zeroAscii;
month = temp1 * 10 + temp2;
// now date
temp1 = (byte)inputStr[posX - 9] - zeroAscii;
temp2 = (byte)inputStr[posX - 8] - zeroAscii;
date = temp1 * 10 + temp2;
// now Day of Week
dow = (byte)inputStr[posX - 7] - zeroAscii;
// now Hour
temp1 = (byte)inputStr[posX - 6] - zeroAscii;
temp2 = (byte)inputStr[posX - 5] - zeroAscii;
hour = temp1 * 10 + temp2;
// now Minute
temp1 = (byte)inputStr[posX - 4] - zeroAscii;
temp2 = (byte)inputStr[posX - 3] - zeroAscii;
minute = temp1 * 10 + temp2;
// now Second
temp1 = (byte)inputStr[posX - 2] - zeroAscii;
temp2 = (byte)inputStr[posX - 1] - zeroAscii;
second = temp1 * 10 + temp2;
}