Arduino: DS1307 RTC I2C Clock Serial

From OnnoWiki
Jump to navigation Jump to search
#include "Arduino.h"
#include "uRTCLib.h"

// uRTCLib rtc;
uRTCLib rtc(0x68);

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
// char cMonth[12][12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

void setup() {
  Serial.begin(9600);
  // delay(3000); // wait for console opening 

  URTCLIB_WIRE.begin();  

  // Comment out below line once you set the date & time.
   // Following line sets the RTC with an explicit date & time
  // for example to set January 13 2022 at 12:56 you would call:
  // rtc.set(0, 56, 12, 5, 13, 1, 22);
  // rtc.set(second, minute, hour, dayOfWeek, dayOfMonth, month, year)
  // set day of week (1=Sunday, 7=Saturday)
}

void loop() {
  rtc.refresh();

  Serial.print(daysOfTheWeek[rtc.dayOfWeek()-1]);
  Serial.print(" , ");
  Serial.print(rtc.day());
  Serial.print(' ');
  Serial.print(rtc.month());
  // Serial.print(cMonth[rtc.month()-1]);
  Serial.print(' ');
  Serial.print(2000+rtc.year());

  print2digits(rtc.hour());
  Serial.print(':');
  print2digits(rtc.minute());
  Serial.print(':');
  print2digits(rtc.second());
  Serial.print('\n');
    
  delay(1000);
}

void print2digits(int number) {
  if (number >= 0 && number < 10) {
    Serial.write('0');
  }
  Serial.print(number);
}