#include "RTClib.h"
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(128, 64, &Wire, OLED_RESET);
#define C_X 63
#define C_Y 31
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
void setup () {
Serial.begin(115200);
// put your setup code here, to run once:
display.begin(SSD1306_SWITCHCAPVCC, 0x3c);
display.display();
delay(1000);
display.display();
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
}
void setcircle() {
int t, m, s;
float theta_r, x, y;
display.clearDisplay();
display.drawCircle(C_X, C_Y, 31, SSD1306_WHITE);
display.drawCircle(C_X, C_Y, 3, SSD1306_WHITE);
for (int i = 0; i < 12; i++) {
m = i * 5;
t = 120 - m * 6;
theta_r = t * (PI / 180.0);
x = 63 + 25 * cos(theta_r); // 計算指針外圍的起始位置,半徑為 28(這裡的 28 是指針的長度)
y = 31 - 25 * sin(theta_r);
display.drawLine(x, y, 63 + 31 * cos(theta_r), 31 - 31 * sin(theta_r), SSD1306_WHITE);
// 繪製指針,起點為 (x, y),終點為圓邊緣 (63 + 31 * cos(theta_r), 31 - 31 * sin(theta_r))
}
for (int i = 0; i < 60; i++) {
s = i;
t = 120 - s * 6;
theta_r = t * (PI / 180.0);
x = 63 + 28 * cos(theta_r); // 計算指針外圍的起始位置,半徑為 28(這裡的 28 是指針的長度)
y = 31 - 28 * sin(theta_r);
display.drawLine(x, y, 63 + 31 * cos(theta_r), 31 - 31 * sin(theta_r), SSD1306_WHITE);
// 繪製指針,起點為 (x, y),終點為圓邊緣 (63 + 31 * cos(theta_r), 31 - 31 * sin(theta_r))
}
display.display();
}
float a = 45;
float theta_r, x, y;
int i = 360;
int h = 0, m = 0, s = 0, t;
void loop () {
DateTime now = rtc.now();
h = now.hour();
m = now.minute();
s = now.second();
setcircle();
// put your main code here, to run repeatedly:
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(40, 39);
display.print(h);
display.print(":");
display.print(m);
display.print(":");
display.print(s);
t = 90 - h * 30 - m * 5 / 60.0;
theta_r = t * (PI / 180.0);
x = C_X + 10 * cos(theta_r);
y = C_Y - 10 * sin(theta_r);
display.drawLine(C_X, C_Y, x, y, SSD1306_WHITE);
t = 90 - m * 6;
theta_r = t * (PI / 180.0);
x = C_X + 18 * cos(theta_r);
y = C_Y - 18 * sin(theta_r);
display.drawLine(C_X, C_Y, x, y, SSD1306_WHITE);
t = 90 - s * 6;
theta_r = t * (PI / 180.0);
x = C_X + 25 * cos(theta_r);
y = C_Y - 25 * sin(theta_r);
display.drawLine(C_X, C_Y, x, y, SSD1306_WHITE);
display.display();
delay(1000);
}
Loading
ssd1306
ssd1306