#include <Arduino.h>
#include <U8g2lib.h>
#include <Wire.h>
// ESP32-C3 Mini I2C pins: SDA=8, SCL=9
U8G2_SH1107_128X128_1_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE, /* clock=*/ 9, /* data=*/ 8);
int center_x = 80;
int center_y = 64;
int time_minutes = 10;
int time_hours = 10;
int time_seconds = 45;
unsigned long lastUpdate = 0; // track last update time
void setup(void) {
u8g2.begin();
u8g2.setContrast(255);
}
void draw_background() {
float xpos, ypos, xpos2, ypos2;
u8g2.setDrawColor(1);
u8g2.drawCircle(center_x, center_y, 40, U8G2_DRAW_ALL); // circle radius
// 60 dots
for (int i=0; i<60; i++) {
xpos = round(center_x + sin(radians(i * 6)) * 38);
ypos = round(center_y - cos(radians(i * 6)) * 38);
u8g2.drawPixel(xpos,ypos);
}
// Tickmarks
for (int i=0; i<12; i++) {
if((i % 3) != 0) {
xpos = round(center_x + sin(radians(i * 30)) * 34);
ypos = round(center_y - cos(radians(i * 30)) * 34);
xpos2 = round(center_x + sin(radians(i * 30)) * 28);
ypos2 = round(center_y - cos(radians(i * 30)) * 28);
u8g2.drawLine(xpos, ypos, xpos2, ypos2);
}
}
// Labels
u8g2.setFont(u8g2_font_8x13B_mn);
u8g2.drawStr(center_x-7, center_y-28,"12");
u8g2.drawStr(center_x+28, center_y+5,"3");
u8g2.drawStr(center_x-3, center_y+36,"6");
u8g2.drawStr(center_x-35, center_y+5,"9");
}
void draw_hand_thin (int hand_angle, int hand_lenght_long, int hand_legth_short) {
float xpos, ypos, xpos2, ypos2;
xpos = round(center_x + sin(radians(hand_angle)) * hand_lenght_long);
ypos = round(center_y - cos(radians(hand_angle)) * hand_lenght_long);
xpos2 = round(center_x + sin(radians(hand_angle + 180)) * hand_legth_short);
ypos2 = round(center_y - cos(radians(hand_angle + 180)) * hand_legth_short);
u8g2.drawLine(xpos, ypos, xpos2, ypos2);
}
void draw_hand_bold (int hand_angle, int hand_lenght_long, int hand_legth_short, int hand_dot_size) {
float xpos, ypos, xpos2, ypos2;
xpos = round(center_x + sin(radians(hand_angle)) * hand_lenght_long);
ypos = round(center_y - cos(radians(hand_angle)) * hand_lenght_long);
xpos2 = round(center_x + sin(radians(hand_angle)) * hand_legth_short);
ypos2 = round(center_y - cos(radians(hand_angle)) * hand_legth_short);
u8g2.drawLine(center_x, center_y, xpos2, ypos2);
u8g2.drawDisc(xpos, ypos, hand_dot_size);
u8g2.drawDisc(xpos2, ypos2, hand_dot_size);
}
void loop(void) {
// Update once per second
if (millis() - lastUpdate >= 1000) {
lastUpdate = millis();
time_seconds++;
if (time_seconds >= 60) {
time_seconds = 0;
time_minutes++;
if (time_minutes >= 60) {
time_minutes = 0;
time_hours++;
if (time_hours >= 12) time_hours = 0;
}
}
}
u8g2.firstPage();
do {
draw_background();
draw_hand_bold(time_minutes*6, 28, 10, 2);
draw_hand_bold(time_hours*30 + (time_minutes / 2), 20, 8, 2);
draw_hand_thin(time_seconds*6, 32, 12);
// Center circle
u8g2.setColorIndex(0);
u8g2.drawDisc(center_x, center_y, 3);
u8g2.setColorIndex(1);
u8g2.drawCircle(center_x, center_y, 3);
// Text below clock
u8g2.setFont(u8g2_font_5x8_mf); // small font
u8g2.drawStr(center_x-30, 120, "AI Centre Nandurbar");
} while ( u8g2.nextPage() );
}
https://wokwi.com/projects/466620998193116161