#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <RTClib.h>
// Define OLED display size
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Create RTC object
RTC_DS3231 rtc;
// Array of day names in Indonesian
const char* daysOfTheWeek[] = {"Minggu", "Senin", "Selasa", "Rabu", "Kamis", "Jumat", "Sabtu"};
void setup() {
// Initialize serial communication (optional)
Serial.begin(9600);
// Initialize OLED display with I2C address 0x3C
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// Clear the buffer
display.clearDisplay();
display.display();
// Initialize RTC
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, let's set the time!");
// When the RTC loses power, set the time to the compile time
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void loop() {
// Get the current date and time from the RTC
DateTime now = rtc.now();
// Clear the display
display.clearDisplay();
// Set text color to white (since OLED is monochrome)
display.setTextColor(SSD1306_WHITE);
// Display the day of the week in small size at the top
display.setTextSize(1); // Smaller text size
display.setCursor(47, 37);
display.print(daysOfTheWeek[now.dayOfTheWeek()]);
// Display the date in small size below the day of the week
display.setCursor(40, 50);
display.print(now.day());
display.print('/');
display.print(now.month());
display.print('/');
display.print(now.year());
// Display the time in large size
display.setTextSize(3); // Larger text size
display.setCursor(18, 8); // Adjust as needed to center the time
if (now.hour() < 10) display.print('0'); // Leading zero for hours
display.print(now.hour());
display.print(':');
if (now.minute() < 10) display.print('0'); // Leading zero for minutes
display.print(now.minute());
// Display the buffer
display.display();
// Delay for a second before updating the time again
delay(1000);
}