#include <SPI.h>
#include <Wire.h>
#include <WiFi.h>
#include <time.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>
/* Uncomment the initialize the I2C address , uncomment only one, If you get a totally blank screen try the other*/
#define i2c_Address 0x3d //initialize with the I2C addr 0x3C Typically eBay OLED's
//#define i2c_Address 0x3d //initialize with the I2C addr 0x3D Typically Adafruit OLED's
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // QT-PY / XIAO
Adafruit_SH1106G display = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
time_t rawtime = time(nullptr);
struct tm* timeinfo = localtime(&rawtime);
int GMTOffset = -28800; //Replace with your GMT Offset in seconds
int daylightOffset = 0; //Replace with your daylight savings offset in seconds
void setup();
void screenA();
void loop() ;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
display.begin(i2c_Address, true); // Address 0x3C default
//display.setContrast (0); // dim display
delay(2000);
display.clearDisplay();
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
configTime(GMTOffset, daylightOffset, "pool.ntp.org","time.nist.gov");
screenA();
}
void screenA(){
display.clearDisplay();
int radius = 35;
display.drawCircle(display.width()/2, display.height()/2, 2, SH110X_WHITE);
//draw clock
for( int i=0; i < 360;i= i + 30 ){
float angle = i ;
angle=(angle/57.29577951) ;
int x1=(64+(sin(angle)*radius));
int y1=(32-(cos(angle)*radius));
int x2=(64+(sin(angle)*(radius-5)));
int y2=(32-(cos(angle)*(radius-5)));
display.drawLine(x1,y1,x2,y2,SH110X_WHITE);
}
//draw second hand
float angle = timeinfo->tm_sec*6 ;
angle=(angle/57.29577951) ;
int x2=(64+(sin(angle)*(radius)));
int y2=(32-(cos(angle)*(radius)));
display.drawLine(64,32,x2,y2,SH110X_WHITE);
// draw minute hand
angle = timeinfo->tm_min * 6 ;
angle=(angle/57.29577951) ;
x2=(64+(sin(angle)*(radius-3)));
y2=(32-(cos(angle)*(radius-3)));
display.drawLine(64,32,x2,y2,SH110X_WHITE);
// draw hour hand
angle = timeinfo->tm_hour * 30 + int((timeinfo->tm_min / 12) * 6 );
angle=(angle/57.29577951) ;
x2=(64+(sin(angle)*(radius-11)));
y2=(32-(cos(angle)*(radius-11)));
display.drawLine(64,32,x2,y2,SH110X_WHITE);
display.display();
delay(100);
//display.clearDisplay();
}
void loop(){
rawtime = time(nullptr);
timeinfo = localtime(&rawtime);
screenA();
}