#include <U8glib.h>
#include <Wire.h>
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0|U8G_I2C_OPT_NO_ACK|U8G_I2C_OPT_FAST);
int hours = 9; //initial starting time at 9:30:00
int minutes = 30;
int seconds = 0;
char *number[12] = {"6","5","4","3","2","1","12","11","10","9","8","7"};
const int screen_width = 128;
const int screen_height = 64;
float radius = min(screen_height, screen_width)/2-1;
const int x_center = screen_width / 2;
const int y_center = screen_height / 2;
int x1, y1, x2, y2;
double angle;
void draw(void)
{
u8g.setFont(u8g_font_4x6);
u8g.drawStr( 0, 10, "Lhai168");
//u8g.drawCircle(x_center, y_center, radius);
u8g.drawCircle(x_center, y_center, 1); // dot at center of the clock
for(int i =1; i<=60; i++)
{
//*****draw tick mark 60 seconds **********
angle = i*6; // 360/60 = 6 degree
angle = angle * 0.01745233; // π/180 = 0.0174533 pi to radian
x1 = x_center + (sin(angle) * radius);
y1 = y_center + (cos(angle) * radius);
// x2 = x_center + (sin(angle) * (radius-1));
// y2 = y_center + (cos(angle) * (radius-1));
x2 = x_center + (sin(angle) * (radius)); // x1 and x2 same position appears as dot
y2 = y_center + (cos(angle) * (radius)); // y1 and y2 same position appears as dot
u8g.drawLine(x1,y1,x2,y2);
}
for(int i =0; i<12; i++)
{
//*****draw tick mark 12 hours************
angle = i*30; // 360/12 = 30 degree
angle = angle * 0.01745233; // π/180 = 0.0174533 pi to radian
x1 = x_center + (sin(angle) * radius);
y1 = y_center + (cos(angle) * radius);
x2 = x_center + (sin(angle) * (radius - 4));
y2 = y_center + (cos(angle) * (radius - 4));
u8g.drawLine(x1,y1,x2,y2);
//*****draw 12-hour Text*******************
x2 = x_center + (sin(angle) * (radius - 8));
y2 = y_center + (cos(angle) * (radius - 8));
u8g.setFont(u8g_font_chikita);
u8g.drawStr(x2-2,y2+3, String(number[i]).c_str());
}
//******Animate moving clock hands (minute/hour)
//****** Second ********************************
angle = seconds*6;
angle = angle * 0.01745233;
x2 = x_center + (sin(angle) * (radius - 1));
y2 = y_center - (cos(angle) * (radius - 1)); // - minus for moving clockwise + for moving anti-clockwise
u8g.drawLine(x_center, y_center,x2,y2);
//***** Minute*****************************
angle = minutes*6;
angle = angle * 0.01745233;
x2 = x_center + (sin(angle) * (radius - 9));
y2 = y_center - (cos(angle) * (radius - 9));
u8g.drawLine(x_center, y_center,x2,y2);
//***** Hour ******************************
angle = hours*30 + ((minutes/12) * 6);
angle = angle * 0.01745233;
x2 = x_center + (sin(angle) * (radius /2));
y2 = y_center - (cos(angle) * (radius /2));
u8g.drawLine(x_center, y_center,x2,y2);
}
void setup()
{
}
void loop()
{
seconds +=1;
if(seconds==60)
{
seconds=seconds-60; minutes +=1;
}
if(minutes==60)
{
minutes=0; hours +=1;
}
if(hours==24)
{
hours=1;
}
u8g.firstPage();
do{
draw();
}while(u8g.nextPage());
delay(500);
}