#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
#include <RTClib.h>
#include <WiFi.h>
#include <WiFiUdp.h>
#include <NTPClient.h>
#include <Timelib.h>
#include <Adafruit_NeoPixel.h>
#include "Font7Seg.h"
const char *ssid = "Wokwi-GUEST";
const char *password = "";
#define HARDWARE_TYPE MD_MAX72XX::PAROLA_HW
#define MAX_DEVICES 4
#define CLK_PIN 18
#define DATA_PIN 23
#define CS_PIN 5
#define PIN 19
#define NUMPIXELS 60
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
MD_Parola p = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
RTC_DS3231 rtc;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
char mesg[20] = "";
char wkt[20] = "";
char namaHari[7][12] = {"Minggu", "Senin", "Selasa", "Rabu", "Kamis", "Jumat", "Sabtu"};
char namaBulan[12][12]={"Jan","Feb","Mar","Apr","mei","Jun","Jul","Agu","Sep","Okt","Nov","Des"};
unsigned long previousMillis = 0;
unsigned long aktif = 0;
bool flasher = false;
bool start = true;
uint8_t display = 0;
int led = 0;
void getDate(char *psz)
{
DateTime now = rtc.now();
sprintf(psz, "%s %02d %s %04d", namaHari[now.dayOfTheWeek()], now.day(), namaBulan[now.month()-1] , now.year());
}
void getTime(char *psz, bool f = true)
{
DateTime now = rtc.now();
sprintf(psz, "%02d%c%02d", now.hour(), (f ? ':' : ' '), now.minute());
if(f==true){
//Serial.println(led);
if (now.second()==0)pixels.clear();
pixels.setPixelColor(now.second(), pixels.Color(0, 0, 255));
pixels.show();
}
if(start==true){
for (int i=0;i<now.second();i++) {
pixels.setPixelColor(i, pixels.Color(0, 0, 255));
}
pixels.show();
start=false;
}
if(now.hour()==18 && now.minute()==35 && now.second()==0){
timeClient.update();
unsigned long rawTime = timeClient.getEpochTime() + 25200;
time_t t = rawTime;
rtc.adjust(DateTime(year(t),month(t),day(t),hour(t),minute(t),second(t)));
}
}
void setup() {
Serial.begin(9600);
p.begin();
p.setIntensity(100);
p.displayClear();
p.addChar('$', degC);
dht.begin();
if (! rtc.begin()) {
// Serial.println("Couldn't find RTC");
// Serial.flush();
abort();
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, let's set the time!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
WiFi.begin(ssid, password);
while ( WiFi.status() != WL_CONNECTED ) {
delay ( 500 );
Serial.print ( "." );
}
timeClient.begin();
pixels.begin();
pixels.clear();
pixels.setBrightness(100);
// DateTime now = rtc.now();
// Serial.println(now.second());
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= 500) {
previousMillis = currentMillis;
if (currentMillis - aktif >= 60000) {
aktif = currentMillis;
display = 0;
}
//Serial.println(flasher);
getTime(wkt, flasher);
flasher = !flasher;
}
if (p.displayAnimate()) {
switch (display)
{
case 0: // Clock
P.setFont(0, numeric7Seg);
P.setTextEffect(0, PA_PRINT, PA_NO_EFFECT);
P.setPause(0, 0);
if ((millis() - lastTime) >= 1000)
{
lastTime = millis();
getTime(szMesg, flasher);
flasher = !flasher;
}
if ((seconds == 00) && (seconds <= 30)) {
display++;
P.setTextEffect(0, PA_PRINT, PA_WIPE_CURSOR);
}
break;
case 1: // Day of week
P.setFont(0, nullptr);
P.setTextEffect(0, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
display++;
dow2str(wday, szMesg, MAX_MESG);
break;
default: // Calendar
P.setTextEffect(0, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
display = 0;
getDate(szMesg);
break;
}
p.displayReset();
}
}