#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <RTClib.h>
RTC_DS1307 rtc;
#define SCREEN_WIDTH 128 // OLED width, in pixels
#define SCREEN_HEIGHT 64 // OLED height, in pixels
// create an OLED display object connected to I2C
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup() {
Serial.begin(9600);
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("failed to start SSD1306 OLED"));
while (1);
}
delay(2000); // wait two seconds for initializing
}
void loop() {
DateTime now = rtc.now();
oled.clearDisplay(); // clear display
oled.setTextSize(1); // set text size
oled.setTextColor(WHITE); // set text color
// Display date
oled.setCursor(0, 0); // set position to display (x,y)
oled.print("Date: ");
oled.print(now.day(), DEC);
oled.print('/');
oled.print(now.month(), DEC);
oled.print('/');
oled.print(now.year(), DEC);
// Draw analog clock face
int centerX = SCREEN_WIDTH / 2;
int centerY = SCREEN_HEIGHT / 2 + 10;
int radius = min(SCREEN_WIDTH, SCREEN_HEIGHT) / 4;
oled.drawCircle(centerX, centerY, radius, WHITE); // Draw the circle face
// Hour hand
float hourAngle = (now.hour() % 12) * 30 + now.minute() * 0.5; // Each hour is 30 degrees, each minute adds 0.5 degrees
drawHand(centerX, centerY, hourAngle, radius * 0.5, WHITE);
// Minute hand
float minuteAngle = now.minute() * 6; // Each minute is 6 degrees
drawHand(centerX, centerY, minuteAngle, radius * 0.75, WHITE);
// Second hand
float secondAngle = now.second() * 6; // Each second is 6 degrees
drawHand(centerX, centerY, secondAngle, radius * 0.9, WHITE);
oled.display(); // display on OLED
delay(1000); // update once a second
}
void drawHand(int center_x, int center_y, float angle, int length, uint16_t color) {
angle = angle - 90; // Adjust for 0 degrees at top
float angle_rad = angle * (PI / 180.0);
int endX = center_x + cos(angle_rad) * length;
int endY = center_y + sin(angle_rad) * length;
oled.drawLine(center_x, center_y, endX, endY, color);
}