Advertisement
Guest User

Word Clock

a guest
Apr 2nd, 2025
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. #include "FastLED.h"
  2.  
  3. #define BTN_PIN 3
  4. #define LED_PIN A3
  5. #define LED_TYPE WS2812B
  6. #define LED_COLOR 0x7aff0b
  7. #define TOTAL_LEDS 105
  8. #define BRIGHTNESS 40 /* 0 to 255 */
  9. #define LOWEST_BRIGHTNESS 5 /* 0 to 255 */
  10.  
  11. #define CHUNK_SIZE_MS 300000 /* 5 minute chunks (1 sentence per chunk) */
  12. #define INTERVAL_MS 250
  13. #define HOUR_MAX_LEN 11 /* Maximum amount of letters an hour can have */
  14. #define SENTENCE_MAX_LEN 26 /* Maximum amount of letters a sentence can have */
  15. #define POWERUP_SAFETY_DELAY 3000 /* Avoid to fry the LEDs on startup */
  16. #define DEBUG_MODE false /* Same as holding down the push button */
  17.  
  18. CRGB leds[TOTAL_LEDS];
  19.  
  20. /* Clock starts at 9am (o'clock) */
  21. int HOUR_LETTERS[12][HOUR_MAX_LEN] = {
  22. {94,87,80,73,66}, /* N I N E */
  23. {31,24,17,10}, /* T E N */
  24. {95,86,81,72,67,58}, /* E L E V E N */
  25. {45,38,31,24,17,10}, /* T W E L V E */
  26. {90,77,76}, /* O N E */
  27. {62,49,48}, /* T W O */
  28. {35,34,21,20,7}, /* T H R E E */
  29. {92,89,78,75,64,61,50,47,36}, /* F O U R */
  30. {102,93,88,79,74}, /* F I V E */
  31. {19,8,5}, /* S I X */
  32. {51,46,37,32}, /* S E V E N */
  33. {23,18,9,4} /* E I G H T */
  34. };
  35.  
  36. /* Replaceable numbers is the fastest solution I found for variable hours */
  37. /* e.g.: 888 = {hours}, 999 = {next hours} */
  38. int SENTENCE_LETTERS[12][SENTENCE_MAX_LEN] = {
  39. {109, 888}, /* IT IS {hs} O'CLOCK */
  40. {53,44,39, 28,27,14,13,0, 888}, /* IT IS FIVE PAST {hs} */
  41. {53,44,39, 43,40,29,26, 888}, /* IT IS TEN PAST {hs} */
  42. {53,44,39, 96,85,82,71,68,57,54, 888}, /* IT IS QUARTER PAST {hs} */
  43. {53,44,39, 70,69,56,55,42,41, 888}, /* IT IS TWENTY PAST {hs} */
  44. {53,44,39, 70,69,56,55,42,41, 28,27,14,13,0, 888}, /* IT IS TWENTYFIVE PAST {hs} */
  45. {53,44,39, 98,97,84,83, 888}, /* IT IS half PAST {hs} */
  46. {30,25,16,11, 70,69,56,55,42,41, 28,27,14,13,0, 999}, /* IT IS TWENTYFIVE TO {next-hs} */
  47. {30,25,16,11, 70,69,56,55,42,41, 999}, /* IT IS TWENTY TO {next-hs} */
  48. {30,25,16,11, 96,85,82,71,68,57,54, 999}, /* IT IS QUARTER TO {next-hs} */
  49. {30,25,16,11, 43,40,29,26, 999}, /* IT IS TEN TO {next-hs} */
  50. {30,25,16,11, 28,27,14,13,0, 999} /* IT IS FIVE TO {next-hs} */
  51. };
  52.  
  53. int manuallyEnteredChunk = 4; /* Updated with the button */
  54. int currentSentenceOrder = 42; /* Answer to the ultimate question of life, the universe, and everything */
  55. int prevHourOrder = 0;
  56.  
  57. void setup() {
  58. delay(POWERUP_SAFETY_DELAY);
  59. FastLED.addLeds<LED_TYPE, LED_PIN>(leds, TOTAL_LEDS);
  60. FastLED.setBrightness(BRIGHTNESS);
  61. /* Initialize button pin as a pull-up input. */
  62. pinMode(BTN_PIN, INPUT_PULLUP);
  63. }
  64.  
  65. void loop() {
  66. int buttonState = digitalRead(BTN_PIN);
  67. if (buttonState == LOW || DEBUG_MODE) {
  68. manuallyEnteredChunk = manuallyEnteredChunk + 1;
  69. }
  70.  
  71. int chunk = floor(millis() / CHUNK_SIZE_MS) + manuallyEnteredChunk;
  72. int sentenceOrder = chunk % 12;
  73.  
  74. /* Avoid to change anything if there was no change in the sentence */
  75. if (sentenceOrder != currentSentenceOrder) {
  76. currentSentenceOrder = sentenceOrder;
  77.  
  78. int hourChunk = floor(chunk / 12);
  79. int hourOrder24 = hourChunk % 24; /* 0 to 24 on HOUR_LETTERS */
  80.  
  81. cleanDisplay();
  82.  
  83. /* Only paint LEDs between 9am and 11pm */
  84. if (hourOrder24 <= 13) {
  85. /* Lower the brightness between 9pm and 9am */
  86. FastLED.setBrightness(hourOrder24 >= 12 ? LOWEST_BRIGHTNESS : BRIGHTNESS);
  87.  
  88. for (int i = 0; i < SENTENCE_MAX_LEN; i++) {
  89. int letter = SENTENCE_LETTERS[sentenceOrder][i];
  90. if (letter < TOTAL_LEDS) {
  91.  
  92. //paintLED(letter); *********************************************************
  93. } else if (letter == 888) {
  94. int hourOrder = hourChunk % 12; /* 0 to 12 */
  95. for (int j = 0; j < HOUR_MAX_LEN; j++) {
  96. paintLED(HOUR_LETTERS[hourOrder][j]);
  97. }
  98. } else if (letter == 999) {
  99. int nextHourChunk = hourChunk + 1;
  100. int nextHourOrder = nextHourChunk % 12; /* 0 to 12 */
  101. for (int j = 0; j < HOUR_MAX_LEN; j++) {
  102. paintLED(HOUR_LETTERS[nextHourOrder][j]);
  103.  
  104. }
  105. }
  106. }
  107. }
  108.  
  109. FastLED.show();
  110. }
  111.  
  112. delay(INTERVAL_MS);
  113. }
  114.  
  115. void cleanDisplay() {
  116. for (int i = 0; i < TOTAL_LEDS; i++) {
  117. leds[i] = CRGB::Black;
  118. }
  119. }
  120.  
  121. void paintLED(int i) {
  122. leds[i].setColorCode(LED_COLOR);
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement