#if defined(ESP8266)
#include "ESP8266WiFi.h"
#elif defined(ESP32)
#include "WiFi.h"
#endif
#include <WiFiUdp.h>
#include <NTPClient.h>
#if defined(ESP8266)
#define LATCH_PIN D4
#define CLK_PIN D3
#define DATA_PIN D2
#define WINKER_PIN D1
#elif defined(ESP32)
#define LATCH_PIN 4
#define CLK_PIN 5
#define DATA_PIN 2
#define WINKER_PIN 18
#endif
#define NTP_SERVER "kr.pool.ntp.org"
#define TIME_ZONE 9 // 시간단위
#define UPDATE_INTERVAL 3600000 // 밀리초단위 1시간*3600초*1000=3600000
const byte num[] = {
B01111110, // 0
B00001100, // 1
B10110110, // 2
B10011110, // 3
B11001100, // 4
B11011010, // 5
B11111010, // 6
B00001110, // 7
B11111110, // 8
B11011110 // 9
};
const byte spinner[] = {
B10000000,
B00001000,
B00010000,
B00100000
};
// Replace with your network credentials
const char *ssid = "Wokwi-GUEST";
const char *password = "";
// Define NTP Client to get time
WiFiUDP ntpUDP;
//NTPClient(UDP& udp, const char* poolServerName, int timeOffset, int updateInterval);
//timeOffset=sec, updateInterval=millisec
//update()를 실행할 경우 updateInterval로 설정한 시간이 경과한 경우에만 update 실행
NTPClient timeClient(ntpUDP, NTP_SERVER, TIME_ZONE*3600, UPDATE_INTERVAL);
int i = 0;
void setup () {
Serial.begin(115200);
pinMode(LATCH_PIN,OUTPUT);
pinMode(CLK_PIN,OUTPUT);
pinMode(DATA_PIN,OUTPUT);
pinMode(WINKER_PIN,OUTPUT);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
digitalWrite(LATCH_PIN,LOW);
shiftOut(DATA_PIN, CLK_PIN, MSBFIRST, ~B01110010); //C
shiftOut(DATA_PIN, CLK_PIN, MSBFIRST, ~B10111000); //o
shiftOut(DATA_PIN, CLK_PIN, MSBFIRST, ~B10101000); //n
shiftOut(DATA_PIN, CLK_PIN, MSBFIRST, ~spinner[i]); //
digitalWrite(LATCH_PIN,HIGH);
++i;
if (i > 3) i=0;
delay(500);
Serial.print(".");
}
// Initialize a NTPClient to get time
timeClient.begin();
}
void loop() {
timeClient.update();
int hour = timeClient.getHours();
int minute = timeClient.getMinutes();
digitalWrite(LATCH_PIN,LOW);
shiftOut(DATA_PIN, CLK_PIN, MSBFIRST, ~num[hour/10]);
shiftOut(DATA_PIN, CLK_PIN, MSBFIRST, ~num[hour%10]);
shiftOut(DATA_PIN, CLK_PIN, MSBFIRST, ~num[minute/10]);
shiftOut(DATA_PIN, CLK_PIN, MSBFIRST, ~num[minute%10]);
digitalWrite(LATCH_PIN,HIGH);
digitalWrite(WINKER_PIN, (millis()/500%2) ? HIGH : LOW);
delay(100);
}