/*Use DS3231 real-time clock to display date and time on the SH1106 OLED display
*/
#include <Wire.h>
#include <DS3231.h>
DateTime now,nowPrev;
RTClib myRTC;
#include <U8g2lib.h> //OLED display library
//Constructor for OLED: _1_=128 bytes, _2_=256 bytes, _F_=1024 bytes (full buffer)
U8G2_SH1106_128X64_NONAME_2_HW_I2C u8g2(U8G2_R0); //R0 = no rotation
void setup() {
Serial.begin(9600);
Wire.begin();
nowPrev=myRTC.now();
u8g2.begin(); //required, sends setup/init sequence to display
}
void loop() {
do now=myRTC.now(); //read time
while (now.second()==nowPrev.second()); //until the second changes
//Make texts:
char RTCdate[11];
sprintf(RTCdate,"%04d.%02d.%02d\0",now.year(),now.month(),now.day());
char RTCtime[9];
sprintf(RTCtime,"%02d:%02d:%02d\0",now.hour(),now.minute(),now.second());
//Send texts do PC:
Serial.print(RTCdate);
Serial.print(" ");
Serial.print(RTCtime);
Serial.println("");
//Display texts on OLED:
u8g2.firstPage();
do { //keep drawing pieces of the page until done
u8g2.setDrawColor(1); //1=white, 0=black
//Print date:
u8g2.setFont(u8g2_font_helvR14_tn);
u8g2.drawStr(23,18,RTCdate); //takes a char array as input
//Print time:
u8g2.setFont(u8g2_font_helvB18_tn);
u8g2.drawStr(20,60,RTCtime); //takes a char array as input
u8g2.drawFrame(0,0,128,64); //start i, start j, width to right, height to bottom
//u8g2.drawBox(0,40,5,5); //start i, start j, width to right, height to bottom
//u8g2.setDrawColor(0);
//u8g2.drawPixel(0,0);
//u8g2.drawPixel(127,0);
} while (u8g2.nextPage());
nowPrev=now;
}