#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <RTClib.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
RTC_DS1307 rtc;
void setup() {
// Initialize the OLED display
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
// Initialize the RTC module
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
// Set the time if the RTC module has lost power and is restarted
if (rtc.now().year() < 2021) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
// Set the font size and color for the display
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
}
void loop() {
DateTime now = rtc.now();
// Clear the display
display.clearDisplay();
// Print the date
display.setCursor(0, 0);
display.println("TIME");
display.setCursor(0, 10);
display.print(now.hour(), DEC);
display.print(':');
if (now.minute() < 10) {
display.print('0');
}
display.print(now.minute(), DEC);
display.print(':');
if (now.second() < 10) {
display.print('0');
}
display.print(now.second(), DEC);
// Print the time
display.setCursor(0, 30);
display.println("DATE");
display.setCursor(0, 40);
display.print(now.month(), DEC);
display.print('.');
display.print(now.day(), DEC);
display.print('.');
display.print(now.year(), DEC);
// Display the content on the OLED display
display.display();
// Delay for one second
delay(1000);
}