classicsat

WS2812 LED clock

Dec 27th, 2020
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.73 KB | None | 0 0
  1. // LED strip clock. Uses Ws2812 LEDs for segments in an 7 segment.
  2. // timetoarray simplified with only one send digit to LED routine (as a function extern,
  3. // with an array of segment positions and direct RTC to array routine.
  4. // a bunch of commented out Serial.print statemenrt were removed.
  5.  
  6. // Original project and code from: https://www.instructables.com/Big-auto-dim-room-clock-using-arduino-and-WS2811/
  7. // thanks to Reddut user /u/piensa as well.
  8.  
  9. //#include <Time.h>
  10. //#include <TimeLib.h>
  11. //#include <DS3232RTC.h>
  12. //#include <Time.h>
  13. #include <Wire.h>
  14. #include "FastLED.h"
  15. #define NUM_LEDS 60 // Number of LED controles (remember I have 3 leds / controller)
  16. #define COLOR_ORDER RGB // Define color order for your strip
  17. #define DATA_PIN 6 // Data pin for led comunication
  18.  
  19. //BID/DEC conversions
  20. #define bcd2dec(bcd_in) (bcd_in >> 4) * 10 + (bcd_in & 0x0f)
  21. #define dec2bcd(dec_in) ((dec_in / 10) << 4) + (dec_in % 10)
  22.  
  23. const byte sensorPin = 3; // light sensor pin
  24. const byte brightnessLow = 64; // Low brightness value
  25. const byte brightnessHigh = 255; // High brightness value
  26.  
  27. CRGB leds[NUM_LEDS]; // Define LEDs strip
  28. byte digits[10][7] = {{0, 1, 1, 1, 1, 1, 1}, // Digit 0
  29. {0, 1, 0, 0, 0, 0, 1}, // Digit 1
  30. {1, 1, 1, 0, 1, 1, 0}, // Digit 2
  31. {1, 1, 1, 0, 0, 1, 1}, // Digit 3
  32. {1, 1, 0, 1, 0, 0, 1}, // Digit 4
  33. {1, 0, 1, 1, 0, 1, 1}, // Digit 5
  34. {1, 0, 1, 1, 1, 1, 1}, // Digit 6
  35. {0, 1, 1, 0, 0, 0, 1}, // Digit 7
  36. {1, 1, 1, 1, 1, 1, 1}, // Digit 8
  37. {1, 1, 1, 1, 0, 1, 1}
  38. }; // Digit 9 | 2D Array for numbers on 7 segment
  39. byte cursorIndex[6] = {37, 30, 22, 15, 7, 0};// digit LED 0 positions, seconds>hours
  40. byte colons[3] = {14,29,44}; // colon LED positions.
  41.  
  42. byte dateoffset=4;// date offset. No. of RTC registers to shift, to show date.
  43. bool Dot = true; //Dot state
  44. bool DST = false; //DST state
  45. int ledColor = 0x00FF00; // Color used (in hex)
  46. int colonColor = 0x0000FF; // Color used (in hex)
  47. int ledOff = 0x000000; // LED "off" color used (in hex)
  48. byte RTC_time[0x07]; // holds the array from the DS3231 registers
  49. int counter;
  50.  
  51. void setup() {
  52. // Serial.begin(9600);
  53. Wire.begin();
  54. LEDS.addLeds<WS2812, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS); // Set LED strip type
  55. LEDS.setBrightness(64); // Set initial brightness
  56. pinMode(2, INPUT_PULLUP); // Define DST adjust button pin
  57. pinMode(4, INPUT_PULLUP); // Define Minutes adjust button pin
  58. pinMode(5, INPUT_PULLUP); // Define Hours adjust button pin
  59. }
  60.  
  61. // Check Light sensor and set brightness accordingly
  62. void BrightnessCheck() {
  63.  
  64. int sensorValue = digitalRead(sensorPin); // Read sensor
  65. if (sensorValue == 0) {
  66. LEDS.setBrightness(brightnessHigh);
  67. }
  68. else {
  69. LEDS.setBrightness(brightnessLow);
  70. }
  71. };
  72.  
  73. // Convert time to array needed for display
  74. void TimeToArray(bool showdate) {
  75.  
  76. getRTCdatetime();
  77.  
  78. Dot = RTC_time[0] % 2;
  79.  
  80. if (DST) { // if DST is true then add one hour
  81. RTC_time[2] = (RTC_time[2] + 1) % 24; // modulo, because 23 hours+DST is 0 hours, not 24 hours.
  82. }
  83.  
  84.  
  85. //light/dark colons
  86. if (Dot) {
  87. leds[colons[0]] = colonColor;
  88. leds[colons[1]] = colonColor;
  89. }
  90. else {
  91. leds[colons[0]] = ledOff;
  92. leds[colons[1]] = ledOff;
  93. }
  94. for (byte index = 0; index < 3; index++) {
  95.  
  96. byte index2 = index * 2;
  97.  
  98. // only place boolean multiplication used here
  99. // 4 added to index value if showdate true/1, 0 if false.
  100. byte digit = RTC_time[index+(dateoffset*showdate)] % 10;
  101. digit_set(cursorIndex[index2], digit);
  102.  
  103. digit = (RTC_time[index+(dateoffset*showdate)] / 10) % 10;
  104. digit_set(cursorIndex[index2 + 1], digit);
  105.  
  106. }
  107.  
  108. // added because I am using 60LED strip, so why not.
  109.  
  110. digit_set(cursorIndex[0]+15, counter%10);
  111. digit_set(cursorIndex[0]+8, (counter/10)%10);
  112. leds[colons[2]] = colonColor;
  113. }
  114.  
  115. // set LEDs for cursor point, to that digit
  116. void digit_set(byte cursor, byte digit) {
  117. for (int k = 0; k < 7; k++) {
  118. if (digits[digit][k] == 1) {
  119. leds[k + cursor] = ledColor;
  120. }
  121. else {
  122. leds[k + cursor] = ledOff;
  123. }
  124. }
  125. }
  126.  
  127. void DSTcheck() {
  128. int buttonDST = digitalRead(2);
  129. if (buttonDST == LOW) {
  130. if (DST) {
  131. DST = false;
  132. }
  133. else if (!DST) {
  134. DST = true;
  135. };
  136. delay(500);
  137. };
  138. }
  139.  
  140. void TimeAdjust() {
  141. int buttonH = digitalRead(5);
  142. int buttonM = digitalRead(4);
  143. if (buttonH == LOW || buttonM == LOW) {
  144. delay(500);
  145.  
  146. // getRTCdatetime();
  147.  
  148. if (buttonH == LOW) {
  149. if (RTC_time[2] == 23) {
  150. RTC_time[2] = 0;
  151. }
  152. else {
  153. RTC_time[2] += 1;
  154. }
  155. }
  156. else {
  157. if (RTC_time[1] == 59) {
  158. RTC_time[1] = 0;
  159. }
  160. else {
  161. RTC_time[1] += 1;
  162. };
  163. };
  164.  
  165. setRTCdatetime();
  166. }
  167. }
  168.  
  169. void loop() // Main loop
  170. {
  171. // a lot of stuff disabled, not needed for my experiment.
  172. // BrightnessCheck(); // Check brightness
  173. // DSTcheck(); // Check DST
  174. // TimeAdjust(); // Check to see if time is geting modified
  175. TimeToArray(0); // Get leds array with required configuration (RTC registers 0,1,2)
  176. // TimeToArray(1); to show date (RTC registers 4,5,6)
  177. // striptest();
  178. FastLED.show(); // Display leds array
  179. counter++; //for the even extra digits I added.
  180. delay(200);// don't need to rush this
  181. }
  182.  
  183. // I2C direct RTC to array. Got from Internet someplace. Sorry who wrote it.
  184. //Not too different from one I wrote anyways.
  185.  
  186. void getRTCdatetime() {
  187.  
  188. Wire.beginTransmission (0x68);
  189. // Set device to start read reg 0
  190. Wire.write (0x00);
  191. Wire.endTransmission ();
  192.  
  193. // request 7 bytes from the DS3231 and release the I2C bus
  194. Wire.requestFrom(0x68, 0x07, true);
  195.  
  196. byte idx = 0;
  197.  
  198. // read the first seven bytes from the DS3231 module into the array
  199. while (Wire.available()) {
  200. byte input = Wire.read();
  201. RTC_time[idx] = bcd2dec(input); // read each byte from the register in the array
  202. idx++;
  203. }
  204. }
  205.  
  206. //setting RTC from the array, except seconds are zeroed.
  207. void setRTCdatetime() {
  208.  
  209. RTC_time[0] = 0; // set seconds to zero
  210.  
  211. Wire.beginTransmission (0x68);
  212. // Set device to start read reg 0
  213. Wire.write (0x00);
  214. for (int idx = 0; idx < 7; idx++) {
  215. Wire.write (dec2bcd(RTC_time[idx]));
  216. }
  217. Wire.endTransmission ();
  218. }
  219.  
  220. //test routine, to test digit positions from cursorIndex[] array
  221. void striptest() {
  222.  
  223. for (byte i = 0; i < 6; i++) {
  224.  
  225. // clear strip
  226. for (byte sled = 0; sled < 60; sled++) {
  227. leds[sled] = 0;
  228. }
  229. digit_set(cursorIndex[i], 8);//8, because all LEDs on.
  230.  
  231. // set cursor LEDs after, so that the above doesn't overwrite them
  232. leds[colons[0]] = colonColor;
  233. leds[colons[1]] = colonColor;
  234. // show strip
  235. FastLED.show(); // Display leds array
  236. delay(750);
  237.  
  238. }// close for i loop
  239. }
Advertisement
Add Comment
Please, Sign In to add comment