// This code is written by Paul MacDougal for the December 11, 2017 meeting of TriEmbed.org // It is released to the public domain // It does what it does. // DS3231 breakout board is: // Vcc (3.3v) // SDA (A4) // SCL (A5) // NC // GND // SW to ground #define SW_PIN 4 #include #include "RTClib.h" uint8_t read_i2c_register(uint8_t addr, uint8_t reg); void write_i2c_register(uint8_t addr, uint8_t reg, uint8_t val); RTC_DS3231 rtc; void printTime() { DateTime now = rtc.now(); if (now.month() < 10) { Serial.print("0"); } Serial.print(now.month()); Serial.print('/'); if (now.day() < 10) { Serial.print("0"); } Serial.print(now.day()); Serial.print('/'); Serial.print(now.year()); Serial.print(" "); if (now.hour() < 10) { Serial.print("0"); } Serial.print(now.hour(), DEC); Serial.print(':'); if (now.minute() < 10) { Serial.print("0"); } Serial.print(now.minute(), DEC); Serial.print(':'); if (now.second() < 10) { Serial.print("0"); } Serial.print(now.second(), DEC); Serial.println(); } void setup() { Serial.begin(19200); // Serial port to PC while (!Serial); // for Leonardo Serial.println(F("Initialized ds3231 example6 v1.0")); // initialize the RTC rtc.begin(); delay(500); // allow serial data to go out pinMode(SW_PIN, INPUT_PULLUP); printTime(); } bool time_set = false; void loop() { if (!time_set && LOW == digitalRead(SW_PIN)) { time_set = true; // This line sets the RTC with an explicit date & time, for example to set // January 21, 2014 at 3am you would call: // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0)); rtc.adjust(DateTime(2018,1,1,11,19,0)); } else if (time_set) { printTime(); delay(1000); } }