Color Clock
Abouti
Nouveau projet : la Color Clock
Genèse

L’objectif de la Color Clock est d’explorer une manière différente d’indiquer l’heure : se détacher des chiffres et des aiguilles classiques pour proposer une lecture du temps uniquement à partir de couleurs et de zones lumineuses.
L’idée est donc :
- de représenter la progression des minutes par le remplissage progressif d’une surface centrale ;
- de représenter les heures par la position d’une LED allumée sur le pourtour.
- cette horloge se veut à la fois expérimentale, décorative et un support pour réfléchir à d’autres façons de « voir » le temps.
Matériel nécessaire
- un ESP32
- une horloge temps réel (RTC) sauvegardée par batterie
- 15 mètres de ruban LED adressable à 60 LED par mètre
- un capteur infrarouge avec sa télécommande
- un adaptateur secteur 5 V / 5 A
Grandes étapes
- Modélisation 3D des pièces et découverte / apprentissage d’Onshape
- Impression 3D des 12 pièces de structure
- Découpe laser de 2 pièces dans des planches de bois de 5 mm d’épaisseur
- Soudure et maillage des rubans de LED
- Programmation de l’ESP32 et intégration du capteur infrarouge
- Optimisation du code (gestion de la luminosité, animations, consommation, etc.)
Fonctionnement
La lecture de l’heure se fait en deux temps :
- Les minutes :
- La surface centrale se remplit progressivement de lumière au fil de l’heure. Plus elle est remplie, plus on se rapproche de la fin de l’heure en cours (0 % = début d’heure, 100 % = heure pile).
- Les heures :
- Une LED spécifique sur le contour indique l’heure actuelle. La position de cette LED joue le rôle d’« aiguille » simplifiée : il suffit de repérer quelle LED est allumée pour connaître l’heure.
L’ensemble donne une horloge très visuelle, qui invite davantage à ressentir le temps qu’à le lire de manière strictement numérique.
Idées pour le futur
- Ajouter une enceinte pour transformer la Color Clock en réveil lumineux et sonore
- Proposer plusieurs thèmes de couleurs (jour/nuit, ambiances chaudes/froides, mode « relax », etc.)
- Ajouter une interface web simple pour changer la palette, la luminosité ou activer/désactiver l’alarme
Code ESP32
Cliquer sur le bouton à droite pour faire apparaître/disparaître le code...
<syntaxhighlight>
// Magic Clock (ESP32) - Matrice sur GPIO33 + Contour sur GPIO27 // Version sans IR (partie IR supprimée à la demande)
- include <Adafruit_NeoPixel.h>
- include <Wire.h>
- include "RTClib.h"
- include <WiFi.h>
- include <WebServer.h>
// ===================== PINS =====================
- define LED_MATRIX_PIN 33
- define LED_CONTOUR_PIN 27
const int playbackPin = 4; // GPIO relié au PL du module son (optionnel)
// ===================== LEDS ===================== // Matrice centrale
- define MATRIX_LEDS 554
// Contour
- define CONTOUR_LEDS 78
// ===================== WIFI ===================== const char *ssid = "*****"; const char *password = "*****";
WebServer server(80);
// ===================== NEOPIXELS (2 sorties) ===================== Adafruit_NeoPixel matrixStrip(MATRIX_LEDS, LED_MATRIX_PIN, NEO_GRB + NEO_KHZ800); Adafruit_NeoPixel contourStrip(CONTOUR_LEDS, LED_CONTOUR_PIN, NEO_GRB + NEO_KHZ800);
// ===================== RTC ===================== RTC_DS3231 rtc;
// ===================== MODES ===================== enum Mode : uint8_t {
MODE_OFF = 0, MODE_CLOCK = 1, // twoColors MODE_UNUSED = 2, // (ancien chrono) MODE_NINJA = 3, MODE_UNUSED_4 = 4, MODE_RAINBOW = 5, MODE_COUNTDOWN = 6, MODE_CHANGECOLOR= 7, MODE_HYPNOTIC = 8
};
volatile Mode currentMode = MODE_CLOCK;
// ===================== LUMINOSITÉ ===================== // Luminosité logique (en %) int matrixBrightnessPercent = 4; // matrice : 4 % par défaut int contourBrightnessPercent = 22; // contour (LED heure)
// ===================== MATRICE 34 LIGNES ===================== const int nbRows = 34; const int rowLengths[34] = {
7, 9, 11, 13, 15, 15, 15, 17, 17, 17, 19, 19, 19, 21, 21, 21, 21, 21, 21, 21, 21, 19, 19, 19, 17, 17, 17, 15, 15, 15, 13, 11, 9, 7
};
// NOTE : tableau 34x34 avec lignes de tailles variables (le reste est rempli à 0 mais jamais lu car rowLengths limite la boucle) const int ledMatrix[34][34] = {
{0, 1, 2, 3, 4, 5, 6},
{7, 8, 9, 10, 11, 12, 13, 14, 15},
{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26},
{27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39},
{40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54},
{55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69},
{70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84},
{85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101},
{102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118},
{119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135},
{136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154},
{155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173},
{174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192},
{193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213},
{214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234},
{235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255},
{256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276},
{277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297},
{298, 299, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318},
{319, 320, 321, 322, 323, 324, 325, 326, 327, 328, 329, 330, 331, 332, 333, 334, 335, 336, 337, 338, 339},
{340, 341, 342, 343, 344, 345, 346, 347, 348, 349, 350, 351, 352, 353, 354, 355, 356, 357, 358, 359, 360},
{361, 362, 363, 364, 365, 366, 367, 368, 369, 370, 371, 372, 373, 374, 375, 376, 377, 378, 379},
{380, 381, 382, 383, 384, 385, 386, 387, 388, 389, 390, 391, 392, 393, 394, 395, 396, 397, 398},
{399, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417},
{418, 419, 420, 421, 422, 423, 424, 425, 426, 427, 428, 429, 430, 431, 432, 433, 434},
{435, 436, 437, 438, 439, 440, 441, 442, 443, 444, 445, 446, 447, 448, 449, 450, 451},
{452, 453, 454, 455, 456, 457, 458, 459, 460, 461, 462, 463, 464, 465, 466, 467, 468},
{469, 470, 471, 472, 473, 474, 475, 476, 477, 478, 479, 480, 481, 482, 483},
{484, 485, 486, 487, 488, 489, 490, 491, 492, 493, 494, 495, 496, 497, 498},
{499, 500, 501, 502, 503, 504, 505, 506, 507, 508, 509, 510, 511, 512, 513},
{514, 515, 516, 517, 518, 519, 520, 521, 522, 523, 524, 525, 526},
{527, 528, 529, 530, 531, 532, 533, 534, 535, 536, 537},
{538, 539, 540, 541, 542, 543, 544, 545, 546},
{547, 548, 549, 550, 551, 552, 553}
};
// ===================== COMPTE À REBOURS ===================== bool countdownActive = false; unsigned long countdownStartMillis = 0; unsigned long countdownDurationMillis = 0; // ms
bool countdownFlashing = false; unsigned long countdownFlashStartMillis = 0; const unsigned long COUNTDOWN_FLASH_DURATION = 4000; // ms
// ===================== HYPNOTIQUE ===================== bool hypnoticActive = false; unsigned long hypnoticStartMillis = 0; const unsigned long HYPNOTIC_DURATION = 30000; // ms
// ===================== COULEURS / THEMES ===================== int r1 = 0; int g1 = 77; int b1 = 153;
int r2 = 217; int g2 = 242; int b2 = 0;
int customColor1[3] = {0, 77, 153}; int customColor2[3] = {217, 242, 0};
uint8_t RGB_array[24][2][3] = {
{{217, 242, 0}, {0, 77, 153}},
{{255, 81, 208}, {217, 242, 0}},
{{0, 147, 47}, {255, 81, 208}},
{{153, 153, 153}, {0, 147, 47}},
{{255, 182, 0}, {153, 153, 153}},
{{76, 0, 153}, {255, 182, 0}},
{{242, 121, 0}, {76, 0, 153}},
{{153, 153, 153}, {242, 121, 0}},
{{0, 102, 26}, {153, 153, 153}},
{{38, 0, 153}, {0, 102, 26}},
{{178, 0, 178}, {38, 0, 153}},
{{0, 77, 153}, {178, 0, 178}},
{{248, 157, 255}, {0, 77, 153}},
{{217, 242, 0}, {248, 157, 255}},
{{0, 147, 47}, {217, 242, 0}},
{{57, 166, 229}, {0, 147, 47}},
{{255, 151, 35}, {57, 166, 229}},
{{76, 0, 153}, {255, 151, 35}},
{{242, 121, 0}, {76, 0, 153}},
{{153, 153, 153}, {242, 121, 0}},
{{0, 102, 26}, {153, 153, 153}},
{{38, 0, 153}, {0, 102, 26}},
{{86, 124, 158}, {38, 0, 153}},
{{0, 77, 153}, {86, 124, 158}}
};
int rainbowOffset = 0;
// ===================== OUTILS LUMINOSITÉ MATRICE ===================== inline float getMatrixBrightnessFactor() {
float f = (float)matrixBrightnessPercent / 100.0f; if (f < 0.02f) f = 0.02f; if (f > 1.0f) f = 1.0f; return f;
}
inline uint8_t applyMatrixBrightness(uint8_t c) {
float f = getMatrixBrightnessFactor(); int v = (int)(c * f + 0.5f); if (v < 0) v = 0; if (v > 255) v = 255; return (uint8_t)v;
}
inline uint32_t makeMatrixColor(uint8_t r, uint8_t g, uint8_t b) {
uint8_t rr = applyMatrixBrightness(r); uint8_t gg = applyMatrixBrightness(g); uint8_t bb = applyMatrixBrightness(b); return matrixStrip.Color(rr, gg, bb);
}
bool parseHexColor(const String &hex, int &r, int &g, int &b) {
if (hex.length() != 7 || hex[0] != '#') return false; r = (int)strtol(hex.substring(1, 3).c_str(), NULL, 16); g = (int)strtol(hex.substring(3, 5).c_str(), NULL, 16); b = (int)strtol(hex.substring(5, 7).c_str(), NULL, 16); return true;
}
// ===================== OUTILS CONTOUR ===================== static inline int contourHourIndexFromNow(const DateTime& now) {
int hour12 = now.hour() % 12; // 0..11 int idx = (hour12 == 0) ? 2 : (2 + hour12 * 6); if (idx >= CONTOUR_LEDS) idx = 2; return idx;
}
static inline void contourAllOff() {
contourStrip.clear();
}
static inline uint32_t scaleToContourBrightness(uint8_t r, uint8_t g, uint8_t b) {
int baseR = (int)r, baseG = (int)g, baseB = (int)b; int maxBase = max(baseR, max(baseG, baseB)); if (maxBase == 0) maxBase = 1;
float maxFactorNoClip = 255.0f / (float)maxBase;
float userFactor = (float)contourBrightnessPercent / 100.0f; if (userFactor < 0.0f) userFactor = 0.0f; if (userFactor > 1.0f) userFactor = 1.0f;
float useFactor = maxFactorNoClip * userFactor;
int cR = (int)(baseR * useFactor + 0.5f); int cG = (int)(baseG * useFactor + 0.5f); int cB = (int)(baseB * useFactor + 0.5f);
if (cR > 255) cR = 255; if (cG > 255) cG = 255; if (cB > 255) cB = 255;
return contourStrip.Color((uint8_t)cR, (uint8_t)cG, (uint8_t)cB);
}
// ===================== WHEEL ===================== uint32_t WheelMatrix(byte WheelPos) {
WheelPos = 255 - WheelPos;
uint8_t r, g, b;
if (WheelPos < 85) {
r = 255 - WheelPos * 3;
g = 0;
b = WheelPos * 3;
} else if (WheelPos < 170) {
WheelPos -= 85;
r = 0;
g = WheelPos * 3;
b = 255 - WheelPos * 3;
} else {
WheelPos -= 170;
r = WheelPos * 3;
g = 255 - WheelPos * 3;
b = 0;
}
return makeMatrixColor(r, g, b);
}
uint32_t WheelRaw(byte WheelPos) {
WheelPos = 255 - WheelPos;
if (WheelPos < 85) {
return contourStrip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if (WheelPos < 170) {
WheelPos -= 85;
return contourStrip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return contourStrip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
// ===================== HTML ===================== // (repris tel quel de ton code, sans IR) String getHtmlContent() {
String html = "<!DOCTYPE html>"; html += "<html lang=\"fr\">"; html += "<head>"; html += "<meta charset=\"UTF-8\">"; html += "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">"; html += "<link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css\">"; html += "<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js\"></script>"; html += "<script src=\"https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js\"></script>"; html += "<script src=\"https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js\"></script>"; html += "<style>"; html += "body { padding-bottom: 40px; }"; html += ".pastel-btn { background-color: #AEC6CF; border-color: #AEC6CF; }"; html += ".last-btn { background-color: #FFD1DC; border-color: #FFD1DC; }"; html += ".palette-btn { border:none; padding:10px; border-radius:12px; width:100%; margin-bottom:10px; }"; html += ".palette-colors { display:flex; height:30px; border-radius:8px; overflow:hidden; }"; html += ".palette-color-block { flex:1; }"; html += ".palette-label { font-size:0.9rem; margin-top:4px; }"; html += "#customColorsSection { display:none; margin-top:10px; }"; html += "</style>"; html += "<script>"; html += "var clickedButton = null;"; html += "function sendForm(event) {"; html += " event.preventDefault();"; html += " let form = event.target;"; html += " let formData = new FormData(form);"; html += " if (clickedButton) {"; html += " formData.append(clickedButton.name, clickedButton.value);"; html += " clickedButton = null;"; html += " }"; html += " fetch('/function', { method: 'POST', body: formData })"; html += " .then(response => response.text())"; html += " .then(data => console.log(data));"; html += "}"; html += "function selectTheme(c1, c2){"; html += " document.getElementById('color1').value = c1;"; html += " document.getElementById('color2').value = c2;"; html += " let c1Input = document.getElementById('custom_color1');"; html += " let c2Input = document.getElementById('custom_color2');"; html += " if(c1Input) c1Input.value = c1;"; html += " if(c2Input) c2Input.value = c2;"; html += " let form = document.querySelector('form');"; html += " let formData = new FormData(form);"; html += " formData.append('function', '5');"; html += " fetch('/function', { method: 'POST', body: formData })"; html += " .then(response => response.text())"; html += " .then(data => console.log('Change Color direct:', data));"; html += "}"; html += "function updateFromCustom(){"; html += " let c1 = document.getElementById('custom_color1').value;"; html += " let c2 = document.getElementById('custom_color2').value;"; html += " document.getElementById('color1').value = c1;"; html += " document.getElementById('color2').value = c2;"; html += "}"; html += "function toggleCustomColors(){"; html += " let section = document.getElementById('customColorsSection');"; html += " if(section.style.display === 'none' || section.style.display === ){"; html += " section.style.display = 'block';"; html += " } else {"; html += " section.style.display = 'none';"; html += " }"; html += "}"; html += "</script>"; html += "</head>"; html += "<body>";html += "
Magic Clock
";html += "<form method=\"POST\" onsubmit=\"sendForm(event);\">";
// ---------- MODES ----------html += "
"; html += "
Modes
"; html += " const int numButtons = 5;
int funcValues[numButtons] = {1, 2, 4, 5, 10};
String labels[numButtons] = {"Clock", "Animation", "Ninja", "Change Color", "Off"};
for (int i = 0; i < numButtons; i++) {
html += "String extraClass = (funcValues[i] == 10) ? " last-btn" : ""; html += "<button class=\"btn pastel-btn" + extraClass + " w-100\" "; html += "name=\"function\" value=\"" + String(funcValues[i]) + "\" "; html += "onclick=\"clickedButton=this;\">"; html += labels[i]; html += "</button>";html += "
}html += "
// Hidden pour les couleurs (utilisés par Change Color) html += "<input type=\"hidden\" id=\"color1\" name=\"color1\" value=\"#004D99\">"; html += "<input type=\"hidden\" id=\"color2\" name=\"color2\" value=\"#D9F200\">";
// ---------- LUMINOSITÉS ----------html += "
"; html += "
Luminosité
";// Matricehtml += "
html += " <label for=\"matrix_brightness\">Luminosité de la matrice :</label>";
html += " <input type=\"range\" class=\"custom-range\" id=\"matrix_brightness\" name=\"matrix_brightness\" ";
html += " min=\"1\" max=\"100\" value=\"" + String(matrixBrightnessPercent) + "\" ";
html += " oninput=\"document.getElementById('matrixBrightnessValue').innerText=this.value + '%';\">";
html += " Valeur : " + String(matrixBrightnessPercent) + "%";
html += "// Contourhtml += "
html += " <label for=\"contour_brightness\">Luminosité de la LED du contour (heure) :</label>";
html += " <input type=\"range\" class=\"custom-range\" id=\"contour_brightness\" name=\"contour_brightness\" ";
html += " min=\"1\" max=\"100\" value=\"" + String(contourBrightnessPercent) + "\" ";
html += " oninput=\"document.getElementById('contourBrightnessValue').innerText=this.value + '%';\">";
html += " Valeur : " + String(contourBrightnessPercent) + "%";
html += "html += "<button type=\"submit\" class=\"btn btn-secondary w-100 mb-3\" "; html += " name=\"function\" value=\"20\" onclick=\"clickedButton=this;\">"; html += "Appliquer les réglages de luminosité"; html += "</button>";
// ---------- COMPTE À REBOURS ----------html += "
"; html += "
Compte à rebours
"; html += "html += "<label for=\"cd_minutes\">Durée (minutes et secondes) :</label>";html += "
html += " <input type=\"number\" class=\"form-control\" id=\"cd_minutes\" name=\"cd_minutes\" min=\"0\" placeholder=\"Minutes\">";html += "
html += " <input type=\"number\" class=\"form-control\" id=\"cd_seconds\" name=\"cd_seconds\" min=\"0\" max=\"59\" placeholder=\"Secondes\">";html += "
html += "<button type=\"submit\" class=\"btn btn-primary w-100 mb-4\" name=\"function\" value=\"12\" onclick=\"clickedButton=this;\">"; html += "Lancer le compte à rebours"; html += "</button>";
// ---------- COULEURS PERSONNALISÉES ----------html += "
html += "<button type=\"button\" class=\"btn btn-outline-secondary btn-sm\" onclick=\"toggleCustomColors()\">"; html += "Couleurs personnalisées"; html += "</button>";html += "
html += " <label for=\"custom_color1\">Couleur 1 :</label>"; html += " <input type=\"color\" class=\"form-control\" id=\"custom_color1\" value=\"#004D99\" onchange=\"updateFromCustom()\">";html += "
html += " <label for=\"custom_color2\">Couleur 2 :</label>"; html += " <input type=\"color\" class=\"form-control\" id=\"custom_color2\" value=\"#D9F200\" onchange=\"updateFromCustom()\">";html += "
// ---------- RÉGLAGE DE L'HEURE RTC ----------html += "
"; html += "
Régler l'heure de l'horloge
"; html += "html += "<label for=\"set_hh\">Heure (HH:MM) :</label>";html += "
html += " <input type=\"number\" class=\"form-control\" id=\"set_hh\" name=\"set_hh\" min=\"0\" max=\"23\" placeholder=\"Heures\">";html += "
html += " <input type=\"number\" class=\"form-control\" id=\"set_mm\" name=\"set_mm\" min=\"0\" max=\"59\" placeholder=\"Minutes\">";html += "
html += "<button type=\"submit\" class=\"btn btn-info w-100 mb-4\" name=\"function\" value=\"30\" onclick=\"clickedButton=this;\">"; html += "Régler l'heure exacte"; html += "</button>";
html += "</form>";html += "
html += "</body>"; html += "</html>"; return html;
}
// ===================== MODES / EFFETS ===================== void turnoff() {
countdownActive = false; countdownFlashing = false; hypnoticActive = false;
matrixStrip.clear(); contourStrip.clear(); matrixStrip.show(); contourStrip.show(); currentMode = MODE_OFF;
}
void randomcolor() {
matrixStrip.clear();
contourStrip.clear();
for (int pixel = 0; pixel < MATRIX_LEDS; pixel++) {
int red = random(0, 255);
int green = random(0, 255);
int blue = random(0, 255);
matrixStrip.setPixelColor(pixel, makeMatrixColor(red, green, blue));
}
for (int i = 0; i < CONTOUR_LEDS; i++) {
contourStrip.setPixelColor(i, contourStrip.Color(random(0,255), random(0,255), random(0,255)));
}
matrixStrip.show();
contourStrip.show();
}
void increaseBrightness() {
matrixBrightnessPercent += 5; if (matrixBrightnessPercent > 100) matrixBrightnessPercent = 100;
}
void decreaseBrightness() {
matrixBrightnessPercent -= 5; if (matrixBrightnessPercent < 1) matrixBrightnessPercent = 1;
}
void rainbow() {
for (int pixel = 0; pixel < MATRIX_LEDS; pixel++) {
matrixStrip.setPixelColor(pixel, WheelMatrix(((pixel * 256 / 128)) & 255));
}
for (int i = 0; i < CONTOUR_LEDS; i++) {
contourStrip.setPixelColor(i, WheelRaw(((i * 256 / 64)) & 255));
}
matrixStrip.show();
contourStrip.show();
}
void rainbowContinuous() {
for (int i = 0; i < MATRIX_LEDS; i++) {
byte pos = (i + rainbowOffset) & 255;
matrixStrip.setPixelColor(i, WheelMatrix(pos));
}
for (int i = 0; i < CONTOUR_LEDS; i++) {
byte pos = (i + rainbowOffset) & 255;
contourStrip.setPixelColor(i, WheelRaw(pos));
}
matrixStrip.show();
contourStrip.show();
rainbowOffset = (rainbowOffset + 1) & 255;
}
// ARC-EN-CIEL PAR LIGNES VERTICALES (matrice) void verticalRainbow() {
for (int row = 0; row < nbRows; row++) {
byte WheelPosBase = (row * 256) / nbRows;
for (int col = 0; col < rowLengths[row]; col++) {
int ledIndex = ledMatrix[row][col];
if (ledIndex < 0 || ledIndex >= MATRIX_LEDS) continue;
uint32_t color = WheelMatrix(WheelPosBase);
matrixStrip.setPixelColor(ledIndex, color);
}
}
contourStrip.clear();
matrixStrip.show();
contourStrip.show();
}
// Animation hypnotique non bloquante void verticalRainbowAnimationStep() {
if (!hypnoticActive) {
hypnoticActive = true;
hypnoticStartMillis = millis();
}
unsigned long tAll = millis() - hypnoticStartMillis;
if (tAll >= HYPNOTIC_DURATION) {
hypnoticActive = false;
matrixStrip.setBrightness(255); matrixStrip.clear(); contourStrip.clear(); matrixStrip.show(); contourStrip.show();
currentMode = MODE_CLOCK; return; }
float phase = tAll * 0.004f; float breathe = (sin(tAll * 0.0007f) + 1.0f) * 0.5f; uint8_t breatheBrightness = 5 + (uint8_t)(breathe * 15.0f);
matrixStrip.setBrightness(breatheBrightness); contourStrip.clear();
for (int row = 0; row < nbRows; row++) {
uint8_t basePos = (uint8_t)(row * 8 + (int)phase);
for (int col = 0; col < rowLengths[row]; col++) {
int ledIndex = ledMatrix[row][col];
if (ledIndex < 0 || ledIndex >= MATRIX_LEDS) continue;
uint8_t pos = basePos + col * 2;
matrixStrip.setPixelColor(ledIndex, WheelRaw(pos)); // raw ok ici
}
}
matrixStrip.show(); contourStrip.show();
}
void twoColors() {
DateTime now = rtc.now(); int limite = (now.minute() * nbRows) / 60;
matrixStrip.clear(); contourStrip.clear();
int colorTest = now.hour() % 24;
int color1[3] = {
RGB_array[colorTest][0][0],
RGB_array[colorTest][0][1],
RGB_array[colorTest][0][2]
};
int color2[3] = {
RGB_array[colorTest][1][0],
RGB_array[colorTest][1][1],
RGB_array[colorTest][1][2]
};
for (int row = 0; row < nbRows; row++) {
for (int col = 0; col < rowLengths[row]; col++) {
int ledIndex = ledMatrix[row][col];
if (ledIndex < 0 || ledIndex >= MATRIX_LEDS) continue;
if (row < limite) {
matrixStrip.setPixelColor(ledIndex, makeMatrixColor(color1[0], color1[1], color1[2]));
} else {
matrixStrip.setPixelColor(ledIndex, makeMatrixColor(color2[0], color2[1], color2[2]));
}
}
}
int contourLedIndex = contourHourIndexFromNow(now); uint32_t colContour = scaleToContourBrightness( (uint8_t)color1[0], (uint8_t)color1[1], (uint8_t)color1[2] );
for (int offset = -2; offset <= 2; offset++) {
int idx = contourLedIndex + offset;
while (idx < 0) idx += CONTOUR_LEDS; while (idx >= CONTOUR_LEDS) idx -= CONTOUR_LEDS;
contourStrip.setPixelColor(idx, colContour); }
matrixStrip.show(); contourStrip.show();
}
void colorchange()
{
DateTime now = rtc.now(); int limite = (now.minute() * nbRows) / 60;
matrixStrip.clear(); contourStrip.clear();
// ---------- 1) MATRICE ----------
for (int row = 0; row < nbRows; row++) {
for (int col = 0; col < rowLengths[row]; col++) {
int ledIndex = ledMatrix[row][col];
if (ledIndex < 0 || ledIndex >= MATRIX_LEDS) continue;
if (row < limite) {
matrixStrip.setPixelColor(
ledIndex,
makeMatrixColor(
customColor1[0],
customColor1[1],
customColor1[2]
)
);
} else {
matrixStrip.setPixelColor(
ledIndex,
makeMatrixColor(
customColor2[0],
customColor2[1],
customColor2[2]
)
);
}
}
}
// ---------- 2) CONTOUR ---------- int contourLedIndex = contourHourIndexFromNow(now);
uint32_t colContour = scaleToContourBrightness( (uint8_t)customColor1[0], (uint8_t)customColor1[1], (uint8_t)customColor1[2] );
// Allumer 5 LEDs autour de l’heure (anneau)
for (int offset = -2; offset <= 2; offset++) {
int idx = contourLedIndex + offset;
// wrap circulaire while (idx < 0) idx += CONTOUR_LEDS; while (idx >= CONTOUR_LEDS) idx -= CONTOUR_LEDS;
contourStrip.setPixelColor(idx, colContour); }
matrixStrip.show(); contourStrip.show();
}
void ninjaClock() {
DateTime now = rtc.now();
int row = (now.minute() * nbRows) / 60; if (row < 0) row = 0; if (row >= nbRows) row = nbRows - 1;
matrixStrip.clear(); contourStrip.clear();
for (int col = 0; col < rowLengths[row]; col++) {
int ledIndex = ledMatrix[row][col];
if (ledIndex >= 0 && ledIndex < MATRIX_LEDS) {
matrixStrip.setPixelColor(ledIndex, makeMatrixColor(r1, g1, b1));
}
}
int contourLedIndex = contourHourIndexFromNow(now); // bleu ninja fixe, avec slider contour contourStrip.setPixelColor(contourLedIndex, scaleToContourBrightness(0, 77, 153));
matrixStrip.show(); contourStrip.show();
}
void countdownModeStep() {
if (!countdownActive || countdownDurationMillis == 0) return;
unsigned long nowMs = millis(); unsigned long elapsed = nowMs - countdownStartMillis;
float ratio = (float)elapsed / (float)countdownDurationMillis;
if (ratio >= 1.0f) {
ratio = 1.0f;
countdownActive = false;
countdownFlashing = true;
countdownFlashStartMillis = millis();
}
int rowsToFill = (int)(ratio * nbRows + 0.5f);
DateTime now = rtc.now();
int h = now.hour() % 24;
int colorFill[3] = {
RGB_array[h][0][0],
RGB_array[h][0][1],
RGB_array[h][0][2]
};
matrixStrip.clear(); contourStrip.clear();
for (int row = 0; row < nbRows; row++) {
for (int col = 0; col < rowLengths[row]; col++) {
int ledIndex = ledMatrix[row][col];
if (ledIndex < 0 || ledIndex >= MATRIX_LEDS) continue;
if (row < rowsToFill) {
matrixStrip.setPixelColor(ledIndex, makeMatrixColor(colorFill[0], colorFill[1], colorFill[2]));
} else {
matrixStrip.setPixelColor(ledIndex, matrixStrip.Color(0, 0, 0));
}
}
}
matrixStrip.show(); contourStrip.show();
}
void countdownFlashStep() {
unsigned long t = millis() - countdownFlashStartMillis;
if (t >= COUNTDOWN_FLASH_DURATION) {
countdownFlashing = false;
countdownActive = false;
matrixStrip.setBrightness(255); matrixStrip.clear(); contourStrip.clear(); matrixStrip.show(); contourStrip.show();
currentMode = MODE_CLOCK; return; }
bool orangePhase = ((t / 200) % 2 == 0); uint8_t r = orangePhase ? 255 : 255; uint8_t g = orangePhase ? 80 : 0; uint8_t b = orangePhase ? 0 : 0;
uint32_t colM = makeMatrixColor(r, g, b); uint32_t colC = contourStrip.Color(r, g, b);
for (int i = 0; i < MATRIX_LEDS; i++) matrixStrip.setPixelColor(i, colM); for (int i = 0; i < CONTOUR_LEDS; i++) contourStrip.setPixelColor(i, colC);
matrixStrip.show(); contourStrip.show();
}
// ===================== STATE MACHINE ===================== void stateChoice() {
if (currentMode != MODE_HYPNOTIC) {
matrixStrip.setBrightness(255);
contourStrip.setBrightness(255);
}
// Si countdown actif/flash -> forcer if (countdownActive || countdownFlashing) currentMode = MODE_COUNTDOWN;
switch (currentMode) {
case MODE_CLOCK: twoColors(); break;
case MODE_NINJA: ninjaClock(); break;
case MODE_RAINBOW: rainbowContinuous(); break;
case MODE_COUNTDOWN: (countdownFlashing ? countdownFlashStep() : countdownModeStep()); break;
case MODE_CHANGECOLOR: colorchange(); break;
case MODE_HYPNOTIC: verticalRainbowAnimationStep(); break;
case MODE_OFF: /* rien */ break;
default: twoColors(); break;
}
}
// ===================== WEB HANDLER ===================== void handleFunction(int function) {
switch (function) {
case 1: // Clock
countdownActive = false; countdownFlashing = false; hypnoticActive = false;
currentMode = MODE_CLOCK;
break;
case 2: // Animation hypnotique
countdownActive = false; countdownFlashing = false;
currentMode = MODE_HYPNOTIC;
hypnoticActive = false;
break;
case 4: // Ninja
countdownActive = false; countdownFlashing = false; hypnoticActive = false;
currentMode = MODE_NINJA;
break;
case 5: // Change Color
countdownActive = false; countdownFlashing = false; hypnoticActive = false;
currentMode = MODE_CHANGECOLOR;
break;
case 10: // Off
turnoff();
break;
case 12: // Countdown
currentMode = MODE_COUNTDOWN;
break;
default:
break;
}
}
void handleWebFunction() {
int function = server.arg("function").toInt();
Serial.print("Web function reçu: ");
Serial.println(function);
switch (function) {
case 12: { // Countdown durée
int cdMin = server.arg("cd_minutes").toInt();
int cdSec = server.arg("cd_seconds").toInt();
if (cdMin < 0) cdMin = 0;
if (cdSec < 0) cdSec = 0;
if (cdSec > 59) cdSec = 59;
unsigned long totalSeconds = (unsigned long)cdMin * 60UL + (unsigned long)cdSec;
if (totalSeconds == 0) totalSeconds = 1;
countdownDurationMillis = totalSeconds * 1000UL;
countdownStartMillis = millis();
countdownActive = true;
countdownFlashing = false;
handleFunction(function);
server.send(200, "text/plain", "OK: countdown started");
return;
}
case 5: { // Change Color : couleurs
String c1 = server.arg("color1");
String c2 = server.arg("color2");
int r, g, b;
if (c1.length() > 0 && parseHexColor(c1, r, g, b)) {
customColor1[0] = r; customColor1[1] = g; customColor1[2] = b;
}
if (c2.length() > 0 && parseHexColor(c2, r, g, b)) {
customColor2[0] = r; customColor2[1] = g; customColor2[2] = b;
}
handleFunction(function);
server.send(200, "text/plain", "OK: colors updated");
return;
}
case 20: { // Sliders luminosité
if (server.hasArg("matrix_brightness")) {
int m = server.arg("matrix_brightness").toInt();
if (m < 1) m = 1;
if (m > 100) m = 100;
matrixBrightnessPercent = m;
}
if (server.hasArg("contour_brightness")) {
int c = server.arg("contour_brightness").toInt();
if (c < 1) c = 1;
if (c > 100) c = 100;
contourBrightnessPercent = c;
}
// Redessine le mode courant (sans changer l’heure)
stateChoice();
// Pas obligé de repasser par handleFunction ici, mais ok si tu veux
server.send(200, "text/plain", "OK: brightness updated");
return;
}
case 30: { // Réglage RTC (IMPORTANT: sortir direct)
// Sécurité : si champs absents, on ne touche pas à la RTC
if (!server.hasArg("set_hh") || !server.hasArg("set_mm")) {
server.send(400, "text/plain", "ERR: missing set_hh / set_mm");
return;
}
int hh = server.arg("set_hh").toInt();
int mm = server.arg("set_mm").toInt();
if (hh < 0) hh = 0; if (hh > 23) hh = 23;
if (mm < 0) mm = 0; if (mm > 59) mm = 59;
DateTime nowRtc = rtc.now();
rtc.adjust(DateTime(nowRtc.year(), nowRtc.month(), nowRtc.day(), hh, mm, 0));
Serial.print("RTC ajustée via web à: ");
Serial.print(hh); Serial.print(":"); Serial.println(mm);
// On NE FAIT PAS handleFunction(30) : on évite tout effet de bord
server.send(200, "text/plain", "OK: RTC updated");
return;
}
default: {
// Modes 1,2,4,10 etc.
handleFunction(function);
server.send(200, "text/plain", "OK: mode updated");
return;
}
}
}
// ===================== SETUP / LOOP ===================== void setup() {
Serial.begin(9600);
Serial.println("Initialize System");
Wire.begin();
matrixStrip.begin(); matrixStrip.setBrightness(255); matrixStrip.clear(); matrixStrip.show();
contourStrip.begin(); contourStrip.setBrightness(255); contourStrip.clear(); contourStrip.show();
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
}
if (rtc.lostPower()) {
Serial.println("RTC a perdu l'alimentation. Régler l'heure via la page web (bouton 'Régler l'heure exacte').");
}
pinMode(playbackPin, OUTPUT); digitalWrite(playbackPin, HIGH);
WiFi.begin(ssid, password);
Serial.print("Attente de connexion ...");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println("\nConnexion etablie !");
Serial.print("Adresse IP: ");
Serial.println(WiFi.localIP());
Serial.print("Accédez à la page web ici : http://");
Serial.print(WiFi.localIP());
Serial.println("/");
server.on("/", []() { server.send(200, "text/html", getHtmlContent()); });
server.on("/function", HTTP_POST, handleWebFunction);
server.begin();
currentMode = MODE_CLOCK;
}
void loop() {
server.handleClient(); stateChoice();
}
// ===================== OPTIONNEL ===================== void triggerPlayback() {
digitalWrite(playbackPin, LOW); delay(100); digitalWrite(playbackPin, HIGH);
}
</syntaxhighlight>