#include <FastLED.h>
#include <WiFi.h>
#include <sntp.h>
#define LED_PIN 4
#define NUM_LEDS 60
#define LED_ORDER GRB
#define LED_TYPE WS2812B
CRGB leds[NUM_LEDS];
void setup(){
FastLED.addLeds<LED_TYPE, LED_PIN, LED_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.setBrightness(128);
Serial.begin(115200);
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
}
configTime(0,0,"pool.ntp.org");
setenv("TZ","MYT-8",1); //your timezone
tzset();
}
//function for mapping hour
long map_hour(double x, long in_min, long in_max, long out_min, long out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
//from https://www.geeksforgeeks.org/modulus-two-float-double-numbers/
double findMod(double a, double b){
double mod;
// Handling negative values
if (a < 0)
mod = -a;
else
mod = a;
if (b < 0)
b = -b;
// Finding mod by repeated subtraction
while (mod >= b)
mod = mod - b;
// Sign of result typically depends
// on sign of a.
if (a < 0)
return -mod;
return mod;
}
void loop(){
FastLED.clear();
time_t now = time(NULL);
struct tm *tm = localtime(&now);
double hour24 = ( ( ( ( tm->tm_hour ) * 3600 ) +( tm->tm_min * 60) + tm->tm_sec ) ), hour12 = ( ( ( ( tm->tm_hour-12 ) * 3600 ) +( tm->tm_min * 60) + tm->tm_sec ) );
double new_hour = 0;
if(tm->tm_hour > 12){
new_hour = findMod(hour12,86400) / 3600;
} else {
new_hour = findMod(hour24,86400) / 3600;
}
leds[map_hour(new_hour, 0, 12, 0, NUM_LEDS)] = CRGB::Blue;
leds[tm->tm_sec] = CRGB::Red;
leds[tm->tm_min] = CRGB::Green;
FastLED.show();
delay(500);
}