//Constants https://www.aranacorp.com/en/controlling-8-relays-with-esp32-and-shift-register/
//https://randomnerdtutorials.com/esp32-date-time-ntp-client-server-arduino/
//https://forum.arduino.cc/t/how-to-convert-time-to-integer/874333
#define number_of_74hc595s 4
#define numOfRegisterPins number_of_74hc595s * 8
#define dataPin 15 //serial
#define latchPin 2 //latch pin
#define clockPin 4 //clock
#define NTP_SERVER "pool.ntp.org"
#define UTC_OFFSET 0
#define UTC_OFFSET_DST 7200
#include <WiFi.h>
#include <Wire.h>
boolean registers [numOfRegisterPins];
const int afrda = 7024;
bool DirectionState = 0;
String A1 = "";
String A2 = "";
String asec = "";
uint32_t LEDfull = 0x0;
const int LED0[] = {0xEE, 0x22, 0x7C, 0x76, 0xB2, 0xD6, 0xDE, 0x62, 0xFE, 0xF6};
const int LED1[] = {0xEE00, 0x2200, 0x7C00, 0x7600, 0xB200, 0xD600, 0xDE00, 0x6200, 0xFE00, 0xF600};
const int LED2[] = {0xEE0000, 0x220000, 0x7C0000, 0x760000, 0xB20000, 0xD60000, 0xDE0000, 0x620000, 0xFE0000, 0xF60000};
const int LED3[] = {0x00000000, 0x22000000, 0x7C000000, 0x76000000, 0xB2000000, 0xD6000000, 0xDE000000, 0x62000000, 0xFE000000, 0xF6000000, 0xEE000000};
const int LEDS = 0x10000;
uint32_t LEDconn = 0xCC1E1A1A;
const int LED00[] = {0x77, 0x11, 0x3E, 0x3B, 0x59, 0x6B, 0x6F, 0x31, 0x7F, 0x7B};
const int LED10[] = {0x7700, 0x1100, 0x3E00, 0x3B00, 0x5900, 0x6B00, 0x6F00, 0x3100, 0x7F00, 0x7B00};
const int LED20[] = {0x770000, 0x110000, 0x3E0000, 0x3B0000, 0x590000, 0x6B0000, 0x6F0000, 0x310000, 0x7F0000, 0x7B0000};
const int LED30[] = {0x00000000, 0x11000000, 0x3E000000, 0x3B000000, 0x59000000, 0x6B000000, 0x6F000000, 0x31000000, 0x7F000000, 0x7B000000, 0x77000000};
const int LEDS0 = 0x80;
uint32_t LEDconn0 = 0x660F0D0D;
void updateShiftRegister(unsigned long leds, bool isMSBFIRST = true){
unsigned int leds16 = int(leds);
unsigned int leds32 = int(leds>>16);
byte low16LED = lowByte(leds16);
byte high16LED = highByte(leds16);
byte low32LED = lowByte(leds32);
byte high32LED = highByte(leds32);
digitalWrite(latchPin, LOW);
if (isMSBFIRST == false) {
shiftOut(dataPin, clockPin, LSBFIRST, low16LED);
shiftOut(dataPin, clockPin, LSBFIRST, high16LED);
shiftOut(dataPin, clockPin, LSBFIRST, low32LED);
shiftOut(dataPin, clockPin, LSBFIRST, high32LED);
}
else {
shiftOut(dataPin, clockPin, MSBFIRST, high32LED);
shiftOut(dataPin, clockPin, MSBFIRST, low32LED);
shiftOut(dataPin, clockPin, MSBFIRST, high16LED);
shiftOut(dataPin, clockPin, MSBFIRST, low16LED);
}
digitalWrite(latchPin, HIGH);
}
void setup(){
Serial.begin(115200);
Serial.println(F("Initialize System"));
pinMode(dataPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
delay(500);
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
uint32_t LEDconn = 0xCC1E1A1A;
uint32_t LEDconn0 = 0x660F0D0D;
// updateShiftRegister(LEDconn, DirectionState);
updateShiftRegister(LEDconn0, DirectionState);
delay(250);
}
configTime(UTC_OFFSET, UTC_OFFSET_DST, NTP_SERVER);
}
void loop(){
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
return;
}
A1 = (timeinfo.tm_hour);
const int a1 = (A1.toInt());
A2 = (timeinfo.tm_min);
const int a2 = (A2.toInt());
asec = (timeinfo.tm_sec);
const int a3 = (asec.toInt());
const int afrda2 = a1*100+a2;
if (a3 % 2 == 0){
// LEDfull=LED3[afrda2 / 1000]+LED2[afrda2 / 100 % 10]+LED1[afrda2 / 10 % 10]+LED0[afrda2 % 10]+LEDS;
LEDfull=LED30[afrda2 / 1000]+LED20[afrda2 / 100 % 10]+LED10[afrda2 / 10 % 10]+LED00[afrda2 % 10]+LEDS0;
}
else{
// LEDfull=LED3[afrda2 / 1000]+LED2[afrda2 / 100 % 10]+LED1[afrda2 / 10 % 10]+LED0[afrda2 % 10];
LEDfull=LED30[afrda2 / 1000]+LED20[afrda2 / 100 % 10]+LED10[afrda2 / 10 % 10]+LED00[afrda2 % 10];
}
updateShiftRegister(LEDfull, DirectionState);
delay(40);
}