#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
Serial.begin(115200);
if(!display.begin(SSD1306_PAGEADDR, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.display();
delay(2000);
display.clearDisplay();
}
void drawClock() {
display.clearDisplay();
int16_t x0 = SCREEN_WIDTH / 2;
int16_t y0 = SCREEN_HEIGHT / 2;
int16_t radius = SCREEN_HEIGHT / 2 - 2;
// Draw clock face
display.drawCircle(x0, y0, radius, WHITE);
// Draw clock ticks
for (int i = 0; i < 12; ++i) {
float angle = i * (PI / 6);
int16_t x1 = x0 + int(cos(angle) * radius * 0.8);
int16_t y1 = y0 + int(sin(angle) * radius * 0.8);
int16_t x2 = x0 + int(cos(angle) * radius);
int16_t y2 = y0 + int(sin(angle) * radius);
display.drawLine(x1, y1, x2, y2, WHITE);
}
// Get current time
int hours = 18;
int minutes = 26;
int seconds = 52;
// Draw hour hand
float hourAngle = PI / 2 - hours * (PI / 6) - minutes * (PI / 360);
int16_t hourHandX = x0 + int(cos(hourAngle) * radius * 0.5);
int16_t hourHandY = y0 - int(sin(hourAngle) * radius * 0.5);
display.drawLine(x0, y0, hourHandX, hourHandY, WHITE);
// Draw minute hand
float minuteAngle = PI / 2 - minutes * (PI / 30) - seconds * (PI / 1800);
int16_t minuteHandX = x0 + int(cos(minuteAngle) * radius * 0.8);
int16_t minuteHandY = y0 - int(sin(minuteAngle) * radius * 0.8);
display.drawLine(x0, y0, minuteHandX, minuteHandY, WHITE);
// Draw second hand
float secondAngle = PI / 2 - seconds * (PI / 30);
int16_t secondHandX = x0 + int(cos(secondAngle) * radius * 0.9);
int16_t secondHandY = y0 - int(sin(secondAngle) * radius * 0.9);
display.drawLine(x0, y0, secondHandX, secondHandY, WHITE);
// Refresh display
display.display();
}
void loop() {
drawClock();
delay(1000); // Update every second
}
Loading
wemos-s2-mini
wemos-s2-mini