Skip to content

Instantly share code, notes, and snippets.

@Zimbelstern
Last active March 28, 2024 17:38
Show Gist options
  • Save Zimbelstern/d3db83cb935d034dd6c7690360b1c452 to your computer and use it in GitHub Desktop.
Save Zimbelstern/d3db83cb935d034dd6c7690360b1c452 to your computer and use it in GitHub Desktop.
Arduino Sketch for diy word clock: https://lutzschneider.eu/wortuhr
/*
Wortuhr.ino by Lutz Schneider, https://lutzschneider.eu
Controlling a wordclock with WS2812B LED strips and a DS3231 RTC module using the following libraries:
Time, 1.6.0, https://github.com/PaulStoffregen/Time
DS1307RTC, 1.4.1, https://github.com/PaulStoffregen/DS1307RTC
Adafruit NeoPixel, 1.8.1, https://github.com/adafruit/Adafruit_NeoPixel
*/
// Include libraries
#include "TimeLib.h"
#include "DS1307RTC.h"
#include "Adafruit_NeoPixel.h"
// Set the Arduino pin connected to the LEDs
#define LED_PIN 2
// Set the locations of words/letters
const byte esist[5+1] = {5,144,143,141,140,139};
const byte kurz[4+1] = {4,137,136,135,134};
const byte viertel[7+1] = {7,124,125,126,127,128,129,130};
const byte zwanzig[7+1] = {7,120,119,118,117,116,115,114};
const byte fuenf[4+1] = {4,112,111,110,109};
const byte zehn[4+1] = {4,97,98,99,100};
const byte minuten[7+1] = {7,102,103,104,105,106,107,108};
const byte vor[3+1] = {3,96,95,94};
const byte nach[4+1] = {4,93,92,91,90};
const byte halb[4+1] = {4,88,87,86,85};
const byte stunden[][7] = {{5,53,54,55,56,57},{3,48,47,46},{4,81,82,83,84},{4,73,74,75,76},{4,49,50,51,52},{4,57,58,59,60},{5,72,71,70,69,68},{6,45,44,43,42,41,40},{4,77,78,79,80},{4,40,39,38,37},{4,64,63,62,61},{3,67,66,65}};
const byte s[1+1] = {1,45};
const byte uhr[3+1] = {3,25,26,27};
const byte morgens[7+1] = {7,29,30,31,32,33,34,35};
const byte abends[6+1] = {6,24,23,22,21,20,19};
const byte nachts[6+1] = {6,18,17,16,15,14,13};
const byte nachm[4+1] = {4,2,3,4,5};
const byte mittags[7+1] = {7,6,7,8,9,10,11,12};
// Specify colors for Su-Mo (7 day colors followed by 7 night colors) in RGB mode
const byte colorset[14][3] = {{36,8,0},{0,24,8},{16,16,0},{24,0,0},{0,16,16},{0,32,0},{0,8,24},{9,2,0},{0,8,2},{8,8,0},{8,0,0},{0,4,4},{0,8,0},{0,4,12}};
tmElements_t tm;
// Initialise 144 NeoPixel LEDs
Adafruit_NeoPixel Leds = Adafruit_NeoPixel(144, LED_PIN, NEO_GRB + NEO_KHZ800);
// Sync with RTC and start LEDs
void setup() {
setSyncProvider(RTC.get);
Leds.begin();
}
// Update LEDs according to time and day of the week every second
void loop() {
if (RTC.read(tm)) {
if (isSummer(tm)) {
breakTime(makeTime(tm)+3600, tm);
}
Leds.clear();
byte color = getColor(tm.Hour, tm.Wday);
setMinutes(tm.Minute, color);
setHour(tm.Minute, tm.Hour, color);
Leds.show();
}
delay(1000);
}
// Return true if a given date is between the last sunday of March and the last sunday of October, 2 a.m.
bool isSummer(tmElements_t tm) {
if (tm.Month < 3 || tm.Month > 10) {
return false;
}
else if (tm.Month > 3 && tm.Month < 10) {
return true;
}
else if (tm.Month == 3) {
return tm.Day - tm.Wday >= 24 && (tm.Wday != 1 || tm.Hour >= 2);
}
else {
return tm.Day - tm.Wday < 24 || tm.Wday == 1 && tm.Hour < 2;
}
}
// Set LEDs for the minutes: es ist, kurz, viertel, zwanzig, fünf, zehn, minuten, vor, nach, halb
void setMinutes(byte m, byte c) {
turnOn(esist, c);
if ((m > 0 && m < 3) || m == 28 || m == 29 || m == 31 || m == 32 || m > 57 ){ turnOn(kurz, c); }
if ((m > 12 && m < 18) || (m > 42 && m < 48)){ turnOn(viertel, c); }
if ((m > 17 && m < 23) ||(m > 37 && m < 43)){ turnOn(zwanzig, c); }
if ((m > 2 && m < 8) ||(m > 22 && m < 28) || (m > 32 && m < 38) ||(m > 52 && m < 58)){ turnOn(fuenf, c); }
if ((m > 7 && m < 13) || (m > 47 && m < 53)){ turnOn(zehn, c); }
if ((m > 2 && m < 13) || (m > 17 && m < 28) || (m > 32 && m < 43) || (m > 47 && m < 58)){ turnOn(minuten, c); }
if ((m > 22 && m < 30) || (m > 37)){ turnOn(vor, c); }
if ((m > 0 && m < 23) || (m > 30 && m < 38)){ turnOn(nach, c); }
if (m > 22 && m < 38){ turnOn(halb, c); }
}
// Set LEDs for the hours: 1-12, uhr, ein(s), nachts, morgens, mittags, nachmittags, abends
void setHour(byte m, byte h, byte c) {
byte h2;
if (m < 23) { h2 = h; }
else { h2 = h+1; }
turnOn(stunden[h2%12], c);
if (m < 3 || m > 57){ turnOn(uhr, c); }
else if (h2%12 == 1) { turnOn(s, c); }
if (h2 < 6 || h2 > 22){ turnOn(nachts, c); }
if (h2 > 5 && h2 < 12){ turnOn(morgens, c); }
if (h2 > 11 && h2 < 18){ turnOn(mittags, c); }
if (h2 > 14 && h2 < 18){ turnOn(nachm, c); }
if (h2 > 17 && h2 < 23){ turnOn(abends, c); }
}
// Switch the LEDs on
void turnOn(byte words[], byte c) {
for(byte i = 1; i < words[0] + 1; i++){
Leds.setPixelColor(words[i]-1, Leds.Color(colorset[c][0],colorset[c][1],colorset[c][2]));
};
};
// Return the day or night color for a given time and day of the week
byte getColor(byte h, byte w) {
if (h > 8 && h < 22) {
return w-1;
}
else {
return w+6;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment