// Written by: Widyo Rio
// Subscribe our YT channel: SkwaDrone Salatiga
// Follow our IG n TikTok: @skwadronesalatiga
#include <WiFi.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define SCREEN_ADDRESS 0x3C
// LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
#define NTP_SERVER "pool.ntp.org"
// #define NTP_SERVER "ntp.bmkg.go.id"
#define UTC_OFFSET 25200
#define UTC_OFFSET_DST 7
const int NUM_POINTS = 60;
const int RADIUS = 26;
int pointsX[NUM_POINTS];
int pointsY[NUM_POINTS];
int detik, menit,jam;
void BacaWaktu()
{
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
display.print("Connection Err");
display.display();
return;
}
detik = timeinfo.tm_sec;
menit = timeinfo.tm_min;
jam = timeinfo.tm_hour;
if (jam>12) jam=jam-12;
Serial.println("Jam : "+String(jam));
Serial.println("Menit: "+String(menit));
Serial.println("Detik: "+String(detik));
}
bool isKelipatan5(int angka) {
// Fungsi ini mengembalikan nilai true jika angka merupakan kelipatan 5, dan false sebaliknya
return (angka % 5 == 0);
}
void TampilanAnalog(){
float secAngle = map(detik, 0, 60, 0, 360);
float minAngle = map(menit, 0, 60, 0, 360);
// float hrAngle = map(jam, 1, 12, 30, 360);
float hrAngle = map(jam*60+menit, 0, 720, 0, 360);
//calculate the positions of the hands
int hrX = 64 + (RADIUS - 14) * cos((hrAngle - 90) * PI / 180);
int hrY = 31 + (RADIUS - 14) * sin((hrAngle - 90) * PI / 180);
int minX = 64 + (RADIUS - 7) * cos((minAngle - 90) * PI / 180);
int minY = 31 + (RADIUS - 7) * sin((minAngle - 90) * PI / 180);
int secX = 64 + (RADIUS+2)*cos((secAngle - 90) * PI / 180);
int secY = 31 + (RADIUS+2)*sin((secAngle - 90) * PI / 180);
display.clearDisplay(); //clear the display buffer
//draw the clock face
display.drawCircle(64, 31, 31, WHITE); //Draw a Center Point
display.fillCircle(64, 31, 2, WHITE);
//Draw 12 Clock points
for (int i = 0; i < NUM_POINTS; i += 5) {
if ((i==0)||(i==15)||(i==30)||(i==45)){
// do nothing
} else display.fillCircle(pointsX[i], pointsY[i], 1, WHITE);
// display.drawPixel(pointsX[i], pointsY[i], WHITE);
}
//Display Numbers 12-3-6-9
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
for (int i = 12; i > 1; i -= 3) {
float angle = map(i, 1, 12, 30, 360);
int xPos = 64 + (RADIUS - 1) * cos((angle - 90) * PI / 180) - 3;
int yPos = 31 + (RADIUS - 1) * sin((angle - 90) * PI / 180) - 3;
display.setCursor(xPos, yPos);
display.print(i);
}
//draw the hour hand
display.drawLine(64, 31, hrX, hrY, WHITE);
//draw the minute hand
display.drawLine(64, 31, minX, minY, WHITE);
//draw the Second hand
//display.drawLine(64, 31, secX, secY, WHITE);
display.drawCircle(secX, secY, 2, WHITE);
//update the display
display.display();
}
void UpdateWaktu(){
detik = detik +1;
if (detik==60)
{
detik = 0; menit = menit+1;
if (menit==60)
{
menit = 0; jam = jam+1;
BacaWaktu();
}
if (isKelipatan5(menit)) BacaWaktu();
}
TampilanAnalog();
}
void setup() {
Serial.begin(115200);
display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
display.print("."); display.display();
}
display.clearDisplay();
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
display.setCursor(0, 0);
display.println("Updating time...");
configTime(UTC_OFFSET, UTC_OFFSET_DST, NTP_SERVER);
BacaWaktu();
}
void loop() {
UpdateWaktu();
delay(1000);
}
Loading
ssd1306
ssd1306