#include <Arduino.h>
#include <WiFi.h>
#include <WiFiUdp.h>
#include <NTPClient.h>
#define DEMO_SETUP
#ifndef STASSID
#define STASSID "Wokwi-GUEST"
#define STAPSK ""
#endif
const char* ssid = STASSID; // your network SSID (name)
const char* pass = STAPSK; // your network password
#define PIN_LED 14
//LED_BUILTIN
// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "fr.pool.ntp.org");
const char* ntpServerName = "ntp.via.ecp.fr";
#include <Adafruit_NeoPixel.h>
#define RING_PIN 2
Adafruit_NeoPixel Ring(60, RING_PIN, NEO_GRB + NEO_KHZ800);
#define SETINTERVAL(block, period_ms) \
do { \
static uint32_t next_ms = 0; \
uint32_t now_ms = millis(); \
while ((int32_t)(now_ms - next_ms) >= 0) { \
next_ms += period_ms; \
do { \
block; \
} while (0); \
} \
} while (0); \
void blink(uint32_t high_ms, uint32_t low_ms) {
static int state = 0;
static uint32_t next_ms = 0;
uint32_t now_ms = millis();
while ((int32_t)(now_ms - next_ms) >= 0) {
state = ~state;
digitalWrite(PIN_LED, state ? LOW : HIGH); // LED is inverted
next_ms += state ? high_ms : low_ms;
}
}
void setColor(int n, uint8_t r, uint8_t g, uint8_t b) {
Ring.setPixelColor(n, Ring.Color(r, g, b));
}
void showTime(int hours, int minutes, int seconds, int ms) {
Ring.fill(0);
// 12 hour marks
for (int i = 0; i < 12; i++) {
setColor(5*i, 128, 128, 128);
}
// 4 brighter marks at noon, 3, 6, 9
for (int i = 0; i < 4; i++) {
setColor(15*i, 192, 192, 192);
}
// Draw hours hand: blue
hours %= 12;
int i_h = 5 * hours + (minutes / 12);
setColor(i_h, 0, 0, 255);
// Draw minutes hand: green
setColor(minutes, 0, 255, 0);
// Special case: change color when minutes & hours hands overlap
if (i_h == minutes) {
setColor(i_h, 0, 255, 255);
}
// Draw seconds hands: red
setColor(seconds, 255, 0, 0);
Ring.show();
}
void setup() {
Serial.begin(115200);
Serial.println();
pinMode(PIN_LED, OUTPUT);
Ring.begin();
for (int i = 0; i < 60; i++) {
Ring.setPixelColor(i, Ring.ColorHSV(map(i, 0, 59, 0, 65535), 255, 192));
}
Ring.show();
Serial.printf("Application " __FILE__ " compiled " __DATE__ " at " __TIME__ ".\n\n");
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(50);
blink(100, 100);
}
Ring.fill(0, 32, 0);
Ring.show();
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
timeClient.begin();
timeClient.setTimeOffset(3600); // Paris/heure d'hiver = UTC + 1
Ring.fill(0);
}
void loop() {
static int h, m, s;
#ifdef DEMO_SETUP
SETINTERVAL({
if (s++ == 60) {
s = 0;
if (m++ == 60) {
m = 0;
if (h++ == 12) {
h = 0;
}
}
}
showTime(h, m, s, 0);
Serial.printf("%02d:%02d:%02d\n", h, m, s);
}, 1000 / 120);
#else
SETINTERVAL({
h = timeClient.getHours();
m = timeClient.getMinutes();
s = timeClient.getSeconds();
showTime(h, m, s, 0);
Serial.printf("%02d:%02d:%02d\n", h, m, s);
}, 1000);
#endif
SETINTERVAL({
blink(100, 900);
}, 10);
SETINTERVAL({
timeClient.update();
}, 60000);
delay(1);
}