Tr33x0rs

Sketch

Feb 27th, 2025
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 96.62 KB | None | 0 0
  1. /* -[ClockSketch v7.3]----------------------------------------------------------------------------------------
  2. https://www.instructables.com/ClockSketch-V7-Part-I/
  3.  
  4. pre-configured for:
  5. Lazy 7 - Quick Build Edition
  6. https://www.instructables.com/Lazy-7-Quick-Build-Edition/
  7.  
  8. Arduino UNO/Nano/Pro Mini (AtMega328, 5V, 16 MHz), DS3231 RTC
  9.  
  10. February 2023 - Daniel Cikic
  11.  
  12. Serial Baud Rates:
  13. Arduino: 57600
  14. nodeMCU: 74880
  15. -------------------------------------------------------------------------------------------------------------- */
  16.  
  17.  
  18. // comment below to disable serial in-/output and free some RAM
  19. #define DEBUG
  20.  
  21. // nodeMCU - uncomment to compile this sketch for nodeMCU 1.0 / ESP8266, make sure to select the proper board
  22. // type inside the IDE! This mode is NOT supported and only experimental!
  23. // #define NODEMCU
  24.  
  25. // useWiFi - enable WiFi support, WPS setup only! If no WPS support is available on a router check settings
  26. // further down, set useWPS to fals e and enter ssid/password there
  27. // #define USEWIFI
  28.  
  29. // useNTP - enable NTPClient, requires NODEMCU and USEWIFI. This will also enforce AUTODST.
  30. // Configure a ntp server further down below!
  31. // #define USENTP
  32.  
  33. // RTC selection - uncomment the one you're using, comment all others and make sure pin assignemts for
  34. // DS1302 are correct in the parameters section further down!
  35. // #define RTC_DS1302
  36. #define RTC_DS1307
  37. //#define RTC_DS3231
  38.  
  39. // autoDST - uncomment to enable automatic DST switching, check Time Change Rules below!
  40. // #define AUTODST
  41.  
  42. // FADING - uncomment to enable fading effects for dots/digits, other parameters further down below
  43. #define FADING
  44.  
  45. // autoBrightness - uncomment to enable automatic brightness adjustments by using a photoresistor/LDR
  46. #define AUTOBRIGHTNESS
  47.  
  48. // customDisplay - uncomment this to enable displayMyStuff(). It's an example of how to display values
  49. // at specified times, like temperature readouts
  50. // #define CUSTOMDISPLAY
  51.  
  52. // FastForward will speed up things and advance time, this is only for testing purposes!
  53. // Disables AUTODST, USENTP and USERTC.
  54. // #define FASTFORWARD
  55.  
  56. // customHelper will start some kind of assistant when adapting this sketch to other led layouts, this
  57. // tests all the steps neccessary to run it on almost any led strip configuration.
  58. // #define CUSTOMHELPER
  59.  
  60. /* ----------------------------------------------------------------------------------------------------- */
  61.  
  62.  
  63. #include <TimeLib.h> // "Time" by Michael Margolis, used in all configs
  64. #include <EEPROM.h> // required for reading/saving settings to eeprom
  65.  
  66.  
  67. /* Start RTC config/parameters--------------------------------------------------------------------------
  68. Check pin assignments for DS1302 (SPI), others are I2C (A4/A5 on Arduino by default)
  69. Currently all types are using the "Rtc by Makuna" library */
  70. #ifdef RTC_DS1302
  71. #include <ThreeWire.h>
  72. #include <RtcDS1302.h>
  73. ThreeWire myWire(7, 6, 8); // IO/DAT, SCLK, CE/RST
  74. RtcDS1302<ThreeWire> Rtc(myWire);
  75. #define RTCTYPE "DS1302"
  76. #define USERTC
  77. #endif
  78.  
  79. #ifdef RTC_DS1307
  80. #include <Wire.h>
  81. #include <RtcDS1307.h>
  82. RtcDS1307<TwoWire> Rtc(Wire);
  83. #define RTCTYPE "DS1307"
  84. #define USERTC
  85. #endif
  86.  
  87. #ifdef RTC_DS3231
  88. #include <Wire.h>
  89. #include <RtcDS3231.h>
  90. RtcDS3231<TwoWire> Rtc(Wire);
  91. #define RTCTYPE "DS3231"
  92. #define USERTC
  93. #endif
  94.  
  95. #if !defined ( USERTC )
  96. #pragma message "No RTC selected, check definitions on top of the sketch!"
  97. #endif
  98. /* End RTC config/parameters---------------------------------------------------------------------------- */
  99.  
  100.  
  101. /* Start WiFi config/parameters------------------------------------------------------------------------- */
  102. #ifdef USEWIFI
  103. const bool useWPS = true; // set to false to disable WPS and use credentials below
  104. const char* wifiSSID = "Tr33x0rs";
  105. const char* wifiPWD = "7205196219";
  106. #endif
  107. /* End WiFi config/parameters--------------------------------------------------------------------------- */
  108.  
  109.  
  110. /* Start NTP config/parameters--------------------------------------------------------------------------
  111. Using NTP will enforce autoDST, so check autoDST/time zone settings below! */
  112. #ifdef USENTP
  113. /* I recommend using a local ntp service (many routers offer them), don't spam public ones with dozens
  114. of requests a day, get a rtc! ^^ */
  115. //#define NTPHOST "europe.pool.ntp.org"
  116. #define NTPHOST "128.138.141.177"
  117. #ifndef AUTODST
  118. #define AUTODST
  119. #endif
  120. #endif
  121. /* End NTP config/parameters---------------------------------------------------------------------------- */
  122.  
  123.  
  124. /* Start autoDST config/parameters ----------------------------------------------------------------------
  125. Comment/uncomment/add TimeChangeRules as needed, only use 2 (tcr1, tcr2), comment out unused ones!
  126. Enabling/disabling autoDST will require to set time again, clock will be running in UTC time if autoDST
  127. is enabled, only display times are adjusted (check serial monitor with DEBUG defined!)
  128. This will also add options for setting the date (Year/Month/Day) when setting time on the clock! */
  129. #ifdef AUTODST
  130. #include <Timezone.h> // "Timezone" by Jack Christensen
  131. TimeChangeRule *tcr;
  132. //-----------------------------------------------
  133. /* US */
  134. // TimeChangeRule tcr1 = {"tcr1", First, Sun, Nov, 2, -360}; // utc -6h, valid from first sunday of november at 2am
  135. // TimeChangeRule tcr2 = {"tcr2", Second, Sun, Mar, 2, -300}; // utc -5h, valid from second sunday of march at 2am
  136. //-----------------------------------------------
  137. /* Europe */
  138. TimeChangeRule tcr1 = {"tcr1", Last, Sun, Oct, 3, 60}; // standard/winter time, valid from last sunday of october at 3am, UTC + 1 hour (+60 minutes) (negative value like -300 for utc -5h)
  139. TimeChangeRule tcr2 = {"tcr2", Last, Sun, Mar, 2, 120}; // daylight/summer time, valid from last sunday of march at 2am, UTC + 2 hours (+120 minutes)
  140. //-----------------------------------------------
  141. Timezone myTimeZone(tcr1, tcr2);
  142. #endif
  143. /* End autoDST config/parameters ----------------------------------------------------------------------- */
  144.  
  145.  
  146. /* Start autoBrightness config/parameters -------------------------------------------------------------- */
  147. uint8_t upperLimitLDR = 180; // everything above this value will cause max brightness (according to current level) to be used (if it's higher than this)
  148. uint8_t lowerLimitLDR = 50; // everything below this value will cause minBrightness to be used
  149. uint8_t minBrightness = 30; // anything below this avgLDR value will be ignored
  150. const bool nightMode = false; // nightmode true -> if minBrightness is used, colorizeOutput() will use a single color for everything, using HSV
  151. const uint8_t nightColor[2] = { 0, 70 }; // hue 0 = red, fixed brightness of 70, https://github.com/FastLED/FastLED/wiki/FastLED-HSV-Colors
  152. float factorLDR = 0.5; // try 0.5 - 2.0, compensation value for avgLDR. Set dbgLDR true & define DEBUG and watch the serial monitor. Looking...
  153. const bool dbgLDR = true; // ...for values roughly in the range of 120-160 (medium room light), 40-80 (low light) and 0 - 20 in the dark
  154. #ifdef NODEMCU
  155. uint8_t pinLDR = 0; // LDR connected to A0 (nodeMCU only offers this one)
  156. #else
  157. uint8_t pinLDR = 1; // LDR connected to A1 (in case somebody flashes this sketch on arduino and already has an ldr connected to A1)
  158. #endif
  159. uint8_t intervalLDR = 75; // read value from LDR every 75ms (most LDRs have a minimum of about 30ms - 50ms)
  160. uint16_t avgLDR = 0; // we will average this value somehow somewhere in readLDR();
  161. uint16_t lastAvgLDR = 0; // last average LDR value we got
  162. /* End autoBrightness config/parameters ---------------------------------------------------------------- */
  163.  
  164.  
  165. #define SKETCHNAME "ClockSketch v7.3"
  166. #define CLOCKNAME "Lazy 7 - Quick Build Edition"
  167.  
  168.  
  169. /* Start button config/pins----------------------------------------------------------------------------- */
  170. #ifdef NODEMCU
  171. const uint8_t buttonA = 13; // momentary push button, 1 pin to gnd, 1 pin to d7 / GPIO_13
  172. const uint8_t buttonB = 14; // momentary push button, 1 pin to gnd, 1 pin to d5 / GPIO_14
  173. #else
  174. const uint8_t buttonA = 3; // momentary push button, 1 pin to gnd, 1 pin to d3
  175. const uint8_t buttonB = 4; // momentary push button, 1 pin to gnd, 1 pin to d4
  176. #endif
  177. /* End button config/pins------------------------------------------------------------------------------- */
  178.  
  179.  
  180. /* Start basic appearance config------------------------------------------------------------------------ */
  181. const bool dotsBlinking = true; // true = only light up dots on even seconds, false = always on
  182. const bool leadingZero = true; // true = enable a leading zero, 9:00 -> 09:00, 1:30 -> 01:30...
  183. uint8_t displayMode = 1; // 0 = 24h mode, 1 = 12h mode ("1" will also override setting that might be written to EEPROM!)
  184. uint8_t colorMode = 0; // different color modes, setting this to anything else than zero will overwrite values written to eeprom, as above
  185. uint16_t colorSpeed = 100; // controls how fast colors change, smaller = faster (interval in ms at which color moves inside colorizeOutput();)
  186. const bool colorPreview = true; // true = preview selected palette/colorMode using "8" on all positions for 3 seconds
  187. const uint8_t colorPreviewDuration = 2; // duration in seconds for previewing palettes/colorModes if colorPreview is enabled/true
  188. const bool reverseColorCycling = true; // true = reverse color movements
  189. const uint8_t brightnessLevels[5] {0, 50, 125, 180, 255}; // 0 - 255, brightness Levels (min, med, max) - index (0-2) will be saved to eeprom
  190. uint8_t brightness = brightnessLevels[0]; // default brightness if none saved to eeprom yet / first run
  191. #ifdef FADING
  192. uint8_t fadeDigits = 2; // fade digit segments, 0 = disabled, 1 = only fade out segments turned off, 2 = fade old out and fade new in
  193. uint8_t fadeDots = 2; // fade dots, 0 = disabled, 1 = turn dots off without fading in/out after specidfied time, 2 = fade in and out
  194. uint8_t fadeDelay = 15; // milliseconds between each fading step, 5-25 should work okay-ish
  195. #endif
  196. /* End basic appearance config-------------------------------------------------------------------------- */
  197.  
  198.  
  199. /* End of basic config/parameters section */
  200.  
  201.  
  202. /* End of feature/parameter section, unless changing advanced things/modifying the sketch there's absolutely nothing to do further down! */
  203.  
  204.  
  205. /* library, wifi and ntp stuff depending on above config/parameters */
  206. #ifdef NODEMCU
  207. #if defined ( USENTP ) && !defined ( USEWIFI ) // enforce USEWIFI when USENTP is defined
  208. #define USEWIFI
  209. #pragma warning "USENTP without USEWIFI, enabling WiFi"
  210. #endif
  211. #ifdef USEWIFI
  212. #include <ESP8266WiFi.h>
  213. #include <WiFiUdp.h>
  214. #endif
  215. #endif
  216.  
  217. #ifdef USENTP
  218. #include <NTPClient.h>
  219. WiFiUDP ntpUDP;
  220. NTPClient timeClient(ntpUDP, NTPHOST, 0, 60000);
  221. #endif
  222. /* end library stuff */
  223.  
  224.  
  225. /* setting feature combinations/options */
  226. #if defined ( FASTFORWARD ) || defined ( CUSTOMHELPER )
  227. bool firstLoop = true;
  228. #ifdef USERTC
  229. #undef USERTC
  230. #endif
  231. #ifdef USEWIFI
  232. #undef USEWIFI
  233. #endif
  234. #ifdef USENTP
  235. #undef USENTP
  236. #endif
  237. #ifdef AUTODST
  238. #undef AUTODST
  239. #endif
  240. #endif
  241. /* setting feature combinations/options */
  242.  
  243.  
  244. /* Start of FastLED/clock stuff */
  245. #define LEDSTUFF
  246. #ifdef LEDSTUFF
  247. #ifdef NODEMCU
  248. #define FASTLED_ESP8266_RAW_PIN_ORDER // this means we'll be using the raw esp8266 pin order -> GPIO_12, which is d6 on nodeMCU
  249. #define LED_PIN 12 // led data in connected to GPIO_12 (d6/nodeMCU)
  250. #else
  251. #define FASTLED_ALLOW_INTERRUPTS 0 // AVR + WS2812 + IRQ = https://github.com/FastLED/FastLED/wiki/Interrupt-problems
  252. #define LED_PIN 6 // led data in connected to d6 (arduino)
  253. #endif
  254.  
  255. #define LED_PWR_LIMIT 500 // 500mA - Power limit in mA (voltage is set in setup() to 5v)
  256. #define LED_DIGITS 4 // 4 or 6 digits, HH:MM or HH:MM:SS
  257. #define LED_COUNT 60 // Total number of leds, 60 on Lazy 7 - QBE, resulting in leds[0] - leds[59]
  258.  
  259. #include <FastLED.h>
  260.  
  261. uint8_t markerHSV[3] = { 0, 127, 20 }; // this color will be used to "flag" leds for coloring later on while updating the leds
  262. CRGB leds[LED_COUNT];
  263. CRGBPalette16 currentPalette;
  264. #endif
  265.  
  266.  
  267. // start clock specific config/parameters
  268. /* Segment order, seen from the front:
  269.  
  270. < A >
  271. /\ /\
  272. F B
  273. \/ \/
  274. < G >
  275. /\ /\
  276. E C
  277. \/ \/
  278. < D >
  279.  
  280. digit positions, seen from the front:
  281. _ _ _ _ _ _
  282. |_| |_| |_| |_| |_| |_|
  283. |_| |_| |_| |_| |_| |_|
  284.  
  285. 5 4 3 2 1 0
  286.  
  287. Note: Digit positions for showSegments() depends on the order in which the segments
  288. are defined in segGroups[] below. Most of my things/clocks published so far start
  289. from the right side when seen from the front, as above. But some have different
  290. orders, like Lazy 7 - QBE, which is using a single strip and has an order of
  291. 3, 0, 2, 1 for top left, top right, bottom left, bottom right.
  292.  
  293. */
  294.  
  295.  
  296. /* Below is the configuration for led <> segment assignments.
  297. LED_ACCESS_MODE 0 will use the two values inside each segment (led a, led b)
  298. as they are - 2 leds per segment.
  299. LED_ACCESS_MODE 1 will use the two values inside each segment (led a, led b)
  300. as start and end value to get 2+ leds/segment.
  301.  
  302. Example:
  303. leds 0, 3 -> MODE 0 -> led 0 and 3 inside the segment -> 2 leds
  304. leds 0, 3 -> MODE 1 -> led 0 - 3 inside the segment -> 4 leds
  305.  
  306. Simply add all the leds into their corresponding segments inside the array.
  307. The order of digits/strip routing doesn't really matter there, positions of
  308. HH:MM:SS are assigned using digitPositions.
  309.  
  310. digitsLAM -> LED_ACCESS_MODE per digit
  311.  
  312. */
  313.  
  314. // defining access modes for each digit individually
  315. uint8_t digitsLAM[6] = { 0, 0, 0, 0, 0, 0 };
  316.  
  317. #if ( LED_DIGITS == 4 )
  318. const uint8_t digitPositions[4] = { 3, 0, 2, 1 }; // positions of HH:MM (3, 0, 2, 1) on L7-QBE
  319. const uint16_t segGroups[28][2] PROGMEM = {
  320. #endif
  321.  
  322. /* segments 0-27, 4 digits x 7 segments */
  323. /* digit position 0 */
  324. { 6, 7 }, // top, a
  325. { 8, 9 }, // top right, b
  326. { 11, 12 }, // bottom right, c
  327. { 0, 13 }, // bottom, d
  328. { 1, 2 }, // bottom left, e
  329. { 4, 5 }, // top left, f
  330. { 3, 10 }, // center, g
  331. /* digit position 1 */
  332. { 15, 28 }, // top, a
  333. { 16, 17 }, // top right, b
  334. { 19, 20 }, // bottom right, c
  335. { 21, 22 }, // bottom, d
  336. { 23, 24 }, // bottom left, e
  337. { 26, 27 }, // top left, f
  338. { 18, 25 }, // center, g
  339. /* digit position 2 */
  340. { 31, 44 }, // top, a
  341. { 32, 33 }, // top right, b
  342. { 35, 36 }, // bottom right, c
  343. { 37, 38 }, // bottom, d
  344. { 39, 40 }, // bottom left, e
  345. { 42, 43 }, // top left, f
  346. { 34, 41 }, // center, g
  347. /* digit position 3 */
  348. { 52, 53 }, // top, a
  349. { 54, 55 }, // top right, b
  350. { 57, 58 }, // bottom right, c
  351. { 46, 59 }, // bottom, d
  352. { 47, 48 }, // bottom left, e
  353. { 50, 51 }, // top left, f
  354. { 49, 56 } // center, g
  355. };
  356.  
  357. #if ( LED_DIGITS == 4 )
  358. const uint16_t upperDots[2] PROGMEM = { 14, 29 }; // leds inside the upper dots (right on L7-QBE)
  359. const uint16_t lowerDots[2] PROGMEM = { 30, 45 }; // leds inside the lower dots (left on L7-QBE)
  360. #endif
  361.  
  362. // Using above arrays it's very easy to "talk" to the segments. Simply use 0-6 for the first 7 segments, add 7 (7-13) for the second one, 14-20 for third....
  363. const uint8_t digits[21][7] PROGMEM = {
  364. /* Lets define 10 numbers (0-9) with 7 segments each, also adding some letters
  365. 1 = segment is on, 0 = segment is off */
  366. { 1, 1, 1, 1, 1, 1, 0 }, // 0 -> Show segments a - f, don't show g (center one)
  367. { 0, 1, 1, 0, 0, 0, 0 }, // 1 -> Show segments b + c (top right and bottom right), nothing else
  368. { 1, 1, 0, 1, 1, 0, 1 }, // 2 -> and so on...
  369. { 1, 1, 1, 1, 0, 0, 1 }, // 3
  370. { 0, 1, 1, 0, 0, 1, 1 }, // 4
  371. { 1, 0, 1, 1, 0, 1, 1 }, // 5
  372. { 1, 0, 1, 1, 1, 1, 1 }, // 6
  373. { 1, 1, 1, 0, 0, 0, 0 }, // 7
  374. { 1, 1, 1, 1, 1, 1, 1 }, // 8
  375. { 1, 1, 1, 1, 0, 1, 1 }, // 9
  376. { 0, 0, 0, 1, 1, 1, 1 }, // t -> some letters/symbols from here on (index 10-20, so this won't...
  377. { 0, 0, 0, 0, 1, 0, 1 }, // r -> ...interfere with using digits 0-9 by using index 0-9
  378. { 0, 1, 1, 1, 0, 1, 1 }, // y
  379. { 0, 1, 1, 1, 1, 0, 1 }, // d
  380. { 1, 0, 0, 1, 1, 1, 0 }, // C
  381. { 1, 0, 0, 0, 1, 1, 1 }, // F
  382. { 1, 1, 0, 0, 1, 1, 0 }, // some kind of "half letter M" (left half), displayed using two digits
  383. { 1, 1, 1, 0, 0, 1, 0 }, // some kind of "half letter M" (right half), displayed using two digits
  384. { 1, 1, 0, 0, 0, 1, 1 }, // °
  385. { 0, 1, 1, 0, 1, 1, 1 }, // H
  386. { 0, 0, 0, 0, 0, 0, 0 } // "blank"
  387. };
  388.  
  389. uint8_t clockStatus = 1; // Used for various things, don't mess around with it! 1 = startup
  390. // 0 = regular mode, 1 = startup, 9x = setup modes (90, 91, 92, 93...)
  391.  
  392. /* these values will be saved to EEPROM:
  393. 0 = index for selected palette
  394. 1 = index for selected brightness level
  395. 2 = displayMode, 12h/24h mode
  396. 3 = colorMode */
  397.  
  398. /* End of FastLED/clock stuff */
  399. // End clock specific configs/parameters
  400.  
  401. /* other variables */
  402. uint8_t btnRepeatCounter = 0; // keeps track of how often a button press has been repeated
  403. /* */
  404.  
  405.  
  406. /* -- this is where the fun parts start -------------------------------------------------------------------------------------------------------- */
  407.  
  408.  
  409. void setup() {
  410. #ifdef DEBUG
  411. while ( millis() < 300 ) { // safety delay for serial output
  412. #ifdef NODEMCU
  413. yield();
  414. #endif
  415. }
  416. #ifdef NODEMCU
  417. Serial.begin(74880); Serial.println(F("  "));
  418. #else
  419. Serial.begin(57600); Serial.println(F("  "));
  420. #endif
  421. #ifdef SKETCHNAME
  422. Serial.print(SKETCHNAME); Serial.println(F(" starting up..."));
  423. #endif
  424. #ifdef CLOCKNAME
  425. Serial.print("Clock Type: "); Serial.println(CLOCKNAME);
  426. #endif
  427. #ifdef RTCTYPE
  428. Serial.print(F("Configured RTC: ")); Serial.println(RTCTYPE);
  429. #endif
  430. #ifdef LEDSTUFF
  431. Serial.print(F("LED power limit: ")); Serial.print(LED_PWR_LIMIT); Serial.println(F(" mA"));
  432. Serial.print(F("Total LED count: ")); Serial.println(LED_COUNT);
  433. Serial.print(F("LED digits: ")); Serial.println(LED_DIGITS);
  434. #endif
  435. #ifdef AUTODST
  436. Serial.println(F("autoDST enabled"));
  437. #endif
  438. #ifdef NODEMCU
  439. Serial.println(F("Configured for nodeMCU"));
  440. #ifdef USEWIFI
  441. Serial.println(F("WiFi enabled"));
  442. #endif
  443. #ifdef USENTP
  444. Serial.print(F("NTP enabled, NTPHOST: ")); Serial.println(NTPHOST);
  445. #endif
  446. #else
  447. Serial.println(F("Configured for Arduino"));
  448. #endif
  449. #ifdef FASTFORWARD
  450. Serial.println(F("!! FASTFORWARD defined !!"));
  451. #endif
  452. while ( millis() < 600 ) { // safety delay for serial output
  453. #ifdef NODEMCU
  454. yield();
  455. #endif
  456. }
  457. #endif
  458.  
  459. #ifdef AUTOBRIGHTNESS
  460. #ifdef DEBUG
  461. Serial.print(F("autoBrightness enabled, LDR using pin: ")); Serial.println(pinLDR);
  462. #endif
  463. pinMode(pinLDR, INPUT);
  464. #endif
  465.  
  466. pinMode(buttonA, INPUT_PULLUP);
  467. pinMode(buttonB, INPUT_PULLUP);
  468.  
  469. #ifdef DEBUG
  470. if ( digitalRead(buttonA) == LOW || digitalRead(buttonB) == LOW ) {
  471. if ( digitalRead(buttonA) == LOW ) {
  472. Serial.println(F("buttonA is LOW / pressed - check wiring!"));
  473. }
  474. if ( digitalRead(buttonB) == LOW ) {
  475. Serial.println(F("buttonB is LOW / pressed - check wiring!"));
  476. }
  477. }
  478. #endif
  479.  
  480. #ifdef LEDSTUFF
  481. FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, LED_COUNT).setCorrection(TypicalSMD5050).setTemperature(DirectSunlight).setDither(1);
  482. FastLED.setMaxPowerInVoltsAndMilliamps(5, LED_PWR_LIMIT);
  483. FastLED.clear();
  484. FastLED.show();
  485. #ifdef CUSTOMHELPER // customHelper() will run in a loop if defined!
  486. while ( 1 > 0 ) {
  487. customHelper();
  488. }
  489. #endif
  490. #ifdef DEBUG
  491. Serial.println(F("setup(): Lighting up some leds..."));
  492. #endif
  493. for ( uint8_t i = 0; i < LED_DIGITS; i++ ) {
  494. showSegment(6, i);
  495. }
  496. FastLED.show();
  497. #endif
  498.  
  499. #ifdef NODEMCU // if building for nodeMCU...
  500. #ifdef USEWIFI // ...and if using WiFi.....
  501. #ifdef DEBUG
  502. Serial.println(F("Starting up WiFi..."));
  503. #endif
  504. WiFi.mode(WIFI_STA); // set WiFi mode to STA...
  505. if ( useWPS ) {
  506. WiFi.begin(WiFi.SSID().c_str(),WiFi.psk().c_str()); // ...and start connecting using saved credentials...
  507. #ifdef DEBUG
  508. Serial.println(F("Using WPS setup / saved credentials"));
  509. #endif
  510. } else {
  511. WiFi.begin(wifiSSID, wifiPWD); // ...or credentials defined in the USEWIFI config section
  512. #ifdef DEBUG
  513. Serial.println(F("Using credentials from sketch"));
  514. #endif
  515. }
  516. unsigned long startTimer = millis();
  517. uint8_t wlStatus = 0;
  518. uint8_t counter = 6;
  519. #ifdef DEBUG
  520. Serial.print(F("Waiting for WiFi connection... "));
  521. #endif
  522. while ( wlStatus == 0 ) {
  523. if ( WiFi.status() != WL_CONNECTED ) wlStatus = 0; else wlStatus = 1;
  524. #ifdef LEDSTUFF
  525. if ( millis() - startTimer >= 1000 ) {
  526. FastLED.clear();
  527. showDigit(counter, digitPositions[3]);
  528. FastLED.show();
  529. if ( counter > 0 ) counter--; else wlStatus = 2;
  530. startTimer = millis();
  531. #ifdef DEBUG
  532. Serial.print(F("."));
  533. #endif
  534. }
  535. #endif
  536. #ifdef NODEMCU
  537. yield();
  538. #endif
  539. }
  540. if ( WiFi.status() == WL_CONNECTED ) { // if status is connected...
  541. #ifdef USENTP // ...and USENTP defined...
  542. timeClient.begin(); // ...start timeClient
  543. #endif
  544. }
  545. #ifdef DEBUG
  546. Serial.println();
  547. if ( WiFi.status() != 0 ) {
  548. Serial.print(F("setup(): Connected to SSID: ")); Serial.println(WiFi.SSID());
  549. } else Serial.println(F("setup(): WiFi connection failed."));
  550. #endif
  551. #endif
  552. EEPROM.begin(512);
  553. #endif
  554.  
  555. #ifdef USERTC
  556. Rtc.Begin();
  557. if ( Rtc.GetIsRunning() == false ) {
  558. #ifdef DEBUG
  559. Serial.println(F("setup(): RTC not running, trying to start..."));
  560. #endif
  561. Rtc.SetIsRunning(true);
  562. }
  563. #ifdef DEBUG
  564. Serial.println(F("setup(): RTC.begin(), 2 second safety delay before"));
  565. Serial.println(F(" doing any read/write actions!"));
  566. #endif
  567. unsigned long tmp_time = millis();
  568. while ( millis() - tmp_time < 2000 ) {
  569. #ifdef NODEMCU
  570. yield();
  571. #endif
  572. }
  573. #ifdef DEBUG
  574. Serial.println(F("setup(): RTC initialized"));
  575. #endif
  576. #else
  577. #ifdef DEBUG
  578. Serial.println(F("setup(): No RTC defined!"));
  579. setTime(12, 0, 0, 1, 1, 2000);
  580. #endif
  581. #endif
  582.  
  583. #ifdef LEDSTUFF
  584. FastLED.clear();
  585. FastLED.show();
  586. /* eeprom settings */
  587. #ifdef nodeMCU
  588. EEPROM.begin(512);
  589. #endif
  590. paletteSwitcher();
  591. brightnessSwitcher();
  592. colorModeSwitcher();
  593. displayModeSwitcher();
  594. #endif
  595.  
  596. #ifdef FASTFORWARD
  597. setTime(21, 59, 50, 31, 1, 2023); // h, m, s, d, m, y to set the clock to when using FASTFORWARD
  598. #endif
  599.  
  600. #ifdef USENTP
  601. syncHelper();
  602. #endif
  603.  
  604. clockStatus = 0; // change from 1 (startup) to 0 (running mode)
  605.  
  606. #ifdef DEBUG
  607. printTime();
  608. Serial.println(F("setup() done"));
  609. Serial.println(F("------------------------------------------------------"));
  610. #endif
  611. }
  612.  
  613.  
  614. /* MAIN LOOP */
  615.  
  616.  
  617. void loop() {
  618. static uint8_t lastInput = 0; // != 0 if any button press has been detected
  619. static uint8_t lastSecondDisplayed = 0; // This keeps track of the last second when the display was updated (HH:MM and HH:MM:SS)
  620. static unsigned long lastCheckRTC = millis(); // This will be used to read system time in case no RTC is defined (not supported!)
  621. static bool doUpdate = false; // Update led content whenever something sets this to true. Coloring will always happen at fixed intervals!
  622. #ifdef USERTC
  623. static RtcDateTime rtcTime = Rtc.GetDateTime(); // Get time from rtc
  624. #else
  625. static time_t sysTime = now(); // if no rtc is defined, get local system time
  626. #endif
  627. #ifdef LEDSTUFF
  628. static uint8_t refreshDelay = 5; // refresh leds every 5ms
  629. static long lastRefresh = millis(); // Keeps track of the last led update/FastLED.show() inside the loop
  630. #ifdef AUTOBRIGHTNESS
  631. static long lastReadLDR = millis();
  632. #endif
  633. #endif
  634. #ifdef FASTFORWARD
  635. static unsigned long lastFFStep = millis(); // Keeps track of last time increment if FASTFORWARD is defined
  636. #endif
  637.  
  638. if ( lastInput != 0 ) { // If any button press is detected...
  639. if ( btnRepeatCounter < 1 ) { // execute short/single press function(s)
  640. #ifdef DEBUG
  641. Serial.print(F("loop(): ")); Serial.print(lastInput); Serial.println(F(" (short press)"));
  642. #endif
  643. if ( lastInput == 1 ) { // short press button A
  644. #ifdef LEDSTUFF
  645. brightnessSwitcher();
  646. #endif
  647. }
  648. if ( lastInput == 2 ) { // short press button B
  649. #ifdef LEDSTUFF
  650. paletteSwitcher();
  651. #endif
  652. }
  653. if ( lastInput == 3 ) { // short press button A + button B
  654. }
  655. } else if ( btnRepeatCounter > 8 ) { // execute long press function(s)...
  656. btnRepeatCounter = 1; // ..reset btnRepeatCounter to stop this from repeating
  657. #ifdef DEBUG
  658. Serial.print(F("loop(): ")); Serial.print(lastInput); Serial.println(F(" (long press)"));
  659. #endif
  660. if ( lastInput == 1 ) { // long press button A
  661. #ifdef LEDSTUFF
  662. colorModeSwitcher();
  663. #endif
  664. }
  665. if ( lastInput == 2 ) { // long press button B
  666. #ifdef LEDSTUFF
  667. displayModeSwitcher();
  668. #endif
  669. }
  670. if ( lastInput == 3) { // long press button A + button B
  671. #ifdef USEWIFI // if USEWIFI is defined and...
  672. if ( useWPS ) { // ...if useWPS is true...
  673. connectWPS(); // connect WiFi using WPS
  674. }
  675. #else // if USEWIFI is not defined...
  676. #ifdef LEDSTUFF
  677. FastLED.clear();
  678. FastLED.show();
  679. setupClock(); // start date/time setup
  680. #endif
  681. #endif
  682. }
  683. while ( digitalRead(buttonA) == LOW || digitalRead(buttonB) == LOW ) { // wait until buttons are released again
  684. #ifdef LEDSTUFF
  685. if ( millis() % 50 == 0 ) { // Refresh leds every 50ms to give optical feedback
  686. colorizeOutput(colorMode);
  687. FastLED.show();
  688. }
  689. #endif
  690. #ifdef NODEMCU
  691. yield();
  692. #endif
  693. }
  694. }
  695. }
  696.  
  697. #ifdef FASTFORWARD // if FASTFORWARD is defined...
  698. if ( millis() - lastFFStep >= 250 ) { // ...and 250ms have passed...
  699. adjustTime(5); // ...add 5 seconds to current time
  700. lastFFStep = millis();
  701. }
  702. #endif
  703.  
  704. if ( millis() - lastCheckRTC >= 50 ) { // check rtc/system time every 50ms
  705. #ifdef USERTC
  706. rtcTime = Rtc.GetDateTime();
  707. if ( lastSecondDisplayed != rtcTime.Second() ) doUpdate = true;
  708. #else
  709. sysTime = now();
  710. if ( lastSecondDisplayed != second(sysTime) ) doUpdate = true;
  711. #endif
  712. lastCheckRTC = millis();
  713. }
  714.  
  715. if ( doUpdate ) { // this will update the led array if doUpdate is true because of a new second from the rtc
  716. #ifdef USERTC
  717. setTime(rtcTime.Hour(), rtcTime.Minute(), rtcTime.Second(),
  718. rtcTime.Day(), rtcTime.Month(), rtcTime.Year() ); // sync system time to rtc every second
  719. #ifdef LEDSTUFF
  720. FastLED.clear(); // 1A - clear all leds...
  721. displayTime(now()); // 2A - output rtcTime to the led array..
  722. #endif
  723. lastSecondDisplayed = rtcTime.Second();
  724. #else
  725. #ifdef LEDSTUFF
  726. FastLED.clear(); // 1B - clear all leds...
  727. displayTime(now()); // 2B - output sysTime to the led array...
  728. #endif
  729. lastSecondDisplayed = second(sysTime);
  730. #endif
  731. #ifdef CUSTOMDISPLAY
  732. displayMyStuff(); // 3AB - if customDisplay is defined this will clear the led array again to display custom values...
  733. #endif
  734. doUpdate = false;
  735. #ifdef DEBUG
  736. if ( second() % 20 == 0 ) {
  737. printTime();
  738. }
  739. #endif
  740. #ifdef USENTP // if NTP is enabled, resync to ntp server at 3:01:00 am
  741. time_t t = myTimeZone.toLocal(now()); // convert current system time to local time zone according to rules on top of the sketch
  742. if ( hour(t) == 3 && minute(t) == 1 and second(t) == 0 ) {
  743. syncHelper();
  744. }
  745. #endif
  746. }
  747.  
  748. #ifdef LEDSTUFF
  749. colorizeOutput(colorMode); // 1C, 2C, 3C...colorize the data inside the led array right now...
  750. #ifdef AUTOBRIGHTNESS
  751. if ( millis() - lastReadLDR >= intervalLDR ) { // if LDR is enabled and sample interval has been reached...
  752. readLDR(); // ...call readLDR();
  753. if ( abs(avgLDR - lastAvgLDR) >= 5 ) { // if avgLDR has changed for more than +/- 5 update lastAvgLDR
  754. lastAvgLDR = avgLDR;
  755. FastLED.setBrightness(avgLDR);
  756. }
  757. lastReadLDR = millis();
  758. }
  759. #endif
  760. #ifdef FADING
  761. digitsFader();
  762. dotsFader();
  763. #endif
  764. if ( millis() - lastRefresh >= refreshDelay ) {
  765. FastLED.show();
  766. lastRefresh = millis();
  767. }
  768. #endif
  769.  
  770. lastInput = inputButtons();
  771. }
  772.  
  773.  
  774. /* */
  775.  
  776.  
  777. #ifdef LEDSTUFF
  778.  
  779. #ifdef CUSTOMDISPLAY
  780. void displayMyStuff() {
  781. /* One way to display custom sensor data/other things. displayMyStuff() is then called inside the doUpdate if statement inside
  782. void loop() - after updating the leds but before calling colorizeOutput() and FastLED.show() */
  783. if ( second() >= 30 && second() < 40 ) { // only do something if current second is 30-39
  784. #ifdef RTC_DS3231 // if DS3231 is used we can read the temperature from that for demo purposes here
  785. float rtcTemp = Rtc.GetTemperature().AsFloatDegC(); // get temperature in °C as float (25.75°C)....
  786. uint8_t tmp = round(rtcTemp); // ...and round (26°C)
  787. #else
  788. uint8_t tmp = 99; // get whatever value from whatever sensor into tmp
  789. #endif
  790. FastLED.clear();
  791. if ( LED_DIGITS == 4 ) { // if 4 digits, display following content:
  792. showDigit(tmp / 10, digitPositions[0]); // tmp (26°C) / 10 = 2 on position 1 of HH
  793. showDigit(tmp % 10, digitPositions[1]); // tmp (26°C) % 10 = 6 on position 2 of HH
  794. showDigit(18, digitPositions[2]); // ° symbol from array digits[][] on position 1 of MM
  795. showDigit(14, digitPositions[3]); // C from array digits[][] on position 2 of MM
  796. }
  797. if ( LED_DIGITS == 6 ) { // if 6 digits....
  798. showDigit(tmp / 10, digitPositions[2]); // ...do the above using MM:SS positions instead of HH:MM
  799. showDigit(tmp % 10, digitPositions[3]);
  800. showDigit(18, digitPositions[4]);
  801. showDigit(14, digitPositions[5]);
  802. }
  803. }
  804. }
  805. #endif
  806.  
  807.  
  808. #ifdef FADING
  809. void fadeSegment(uint8_t pos, uint8_t segment, uint8_t amount, uint8_t fadeType) {
  810. /* this will check if the first led of a given segment is lit and if it is, will fade by
  811. amount using fadeType. fadeType is important because when fading things in that where
  812. off previously we must avoid setting them black at first - hence fadeLightBy instead
  813. of fadeToBlack. */
  814. uint8_t ledAM = digitsLAM[pos]; // led access mode according to the position
  815. if ( leds[pgm_read_word_near(&segGroups[segment + pos * 7][0])] ) {
  816. if ( ledAM == 0 ) {
  817. for ( uint8_t i = 0; i < 2; i++ ) {
  818. if ( fadeType == 0 ) {
  819. leds[pgm_read_word_near(&segGroups[segment + pos * 7][i])].fadeToBlackBy(amount);
  820. } else {
  821. leds[pgm_read_word_near(&segGroups[segment + pos * 7][i])].fadeLightBy(amount);
  822. }
  823. }
  824. }
  825. if ( ledAM == 1 ) {
  826. uint16_t startLed = pgm_read_word_near(&segGroups[segment + pos * 7][0]);
  827. uint16_t endLed = pgm_read_word_near(&segGroups[segment + pos * 7][1]);
  828. for ( uint16_t i = startLed; i <= endLed; i++ ) {
  829. if ( fadeType == 0 ) {
  830. leds[i].fadeToBlackBy(amount);
  831. } else {
  832. leds[i].fadeLightBy(amount);
  833. }
  834. }
  835. }
  836. }
  837. }
  838.  
  839.  
  840. void digitsFader() {
  841. if ( fadeDigits == 0 ) return;
  842. static unsigned long firstRun = 0; // time when a change has been detected and fading starts
  843. static unsigned long lastRun = 0; // used to store time when this function was executed the last time
  844. static boolean active = false; // will be used as a flag when to do something / fade segments
  845. static uint8_t previousSegments[LED_DIGITS][7] = { 0 }; // all the segments lit after the last run
  846. static uint8_t currentSegments[LED_DIGITS][7] = { 0 }; // all the segments lit right now
  847. static uint8_t changedSegments[LED_DIGITS][7] = { 0 }; // used to store the differences -> 1 = led has been turned off, fade out, 2 = was off, fade in
  848. static uint8_t fadeSteps = 15; // steps used to fade dots in or out
  849. lastRun = millis();
  850. if ( !active ) { // this will check if....
  851. firstRun = millis();
  852. for ( uint8_t digitPos = 0; digitPos < LED_DIGITS; digitPos++ ) { // ...any of the segments are on....
  853. for ( uint8_t segmentPos = 0; segmentPos < 7; segmentPos++ ) {
  854. if ( leds[pgm_read_word_near(&segGroups[segmentPos + digitPos * 7][0])] ) {
  855. currentSegments[digitPos][segmentPos] = 1;
  856. } else {
  857. currentSegments[digitPos][segmentPos] = 0;
  858. }
  859. if ( currentSegments[digitPos][segmentPos] != previousSegments[digitPos][segmentPos] ) { // ...and compare them to the previous displayed segments.
  860. active = true; // if a change has been detected, set active = true so fading gets executed
  861. #ifdef DEBUG
  862. Serial.print(F("digitPos: ")); Serial.print(digitPos);
  863. Serial.print(F(" - segmentPos: ")); Serial.print(segmentPos);
  864. Serial.print(F(" was "));
  865. #endif
  866. if ( currentSegments[digitPos][segmentPos] == 0 ) {
  867. changedSegments[digitPos][segmentPos] = 1;
  868. #ifdef DEBUG
  869. Serial.println(F("ON, is now OFF"));
  870. #endif
  871. } else {
  872. changedSegments[digitPos][segmentPos] = 2;
  873. #ifdef DEBUG
  874. Serial.println(F("OFF, is now ON"));
  875. #endif
  876. }
  877. }
  878. }
  879. }
  880. }
  881. if ( active ) { // this part is executed once a change has been detected....
  882. static uint8_t counter = 1;
  883. static unsigned long lastFadeStep = millis();
  884. for ( uint8_t digitPos = 0; digitPos < LED_DIGITS; digitPos++ ) { // redraw segments that have turned off, so we can fade them out...
  885. for ( uint8_t segmentPos = 0; segmentPos < 7; segmentPos++ ) {
  886. if ( changedSegments[digitPos][segmentPos] == 1 ) {
  887. showSegment(segmentPos, digitPos);
  888. }
  889. }
  890. }
  891. colorizeOutput(colorMode); // colorize again after redraw, so colors keep consistent
  892. for ( uint8_t digitPos = 0; digitPos < LED_DIGITS; digitPos++ ) {
  893. for ( uint8_t segmentPos = 0; segmentPos < 7; segmentPos++ ) {
  894. if ( changedSegments[digitPos][segmentPos] == 1 ) { // 1 - segment has turned on, this one has to be faded in
  895. fadeSegment(digitPos, segmentPos, counter * ( 255.0 / fadeSteps ), 0); // fadeToBlackBy, segments supposed to be off/fading out
  896. }
  897. if ( changedSegments[digitPos][segmentPos] == 2 ) { // 2 - segment has turned off, this one has to be faded out
  898. if ( fadeDigits == 2 ) {
  899. fadeSegment(digitPos, segmentPos, 255 - counter * ( 255.0 / fadeSteps ), 1 ); // fadeLightBy, segments supposed to be on/fading in
  900. }
  901. }
  902. }
  903. }
  904. if ( millis() - lastFadeStep >= fadeDelay ) {
  905. counter++;
  906. lastFadeStep = millis();
  907. }
  908. if ( counter > fadeSteps ) { // done with fading, reset variables...
  909. counter = 1;
  910. active = false;
  911. for ( uint8_t digitPos = 0; digitPos < LED_DIGITS; digitPos++ ) { // and save current segments to previousSegments
  912. for ( uint8_t segmentPos = 0; segmentPos < 7; segmentPos++ ) {
  913. if ( leds[pgm_read_word_near(&segGroups[segmentPos + digitPos * 7][0])] ) {
  914. previousSegments[digitPos][segmentPos] = 1;
  915. } else {
  916. previousSegments[digitPos][segmentPos] = 0;
  917. }
  918. changedSegments[digitPos][segmentPos] = 0;
  919. }
  920. }
  921. #ifdef DEBUG
  922. Serial.print(F("digit fading sequence took ")); // for debugging/checking duration - fading should never take longer than 1000ms!
  923. Serial.print(millis() - firstRun);
  924. Serial.println(F(" ms"));
  925. #endif
  926. }
  927. }
  928. }
  929.  
  930.  
  931. void dotsFader() {
  932. if ( fadeDots == 0 ) return;
  933. static unsigned long firstRun = 0;
  934. static unsigned long lastRun = 0;
  935. static boolean active = false;
  936. static uint8_t fadeSteps = 15;
  937. lastRun = millis();
  938. if ( !active ) {
  939. if ( leds[pgm_read_word_near(&upperDots[0])] ) {
  940. active = true;
  941. firstRun = millis();
  942. }
  943. }
  944. if ( fadeDots == 1 && active ) { // action = 1, simply turn off specidifc leds after 500ms
  945. if ( lastRun - firstRun >= 500 ) {
  946. for ( uint8_t i = 0; i < ( sizeof(upperDots) / sizeof(upperDots[0]) ); i++ ) {
  947. leds[pgm_read_word_near(&upperDots[i])].setHSV(0, 0, 0);
  948. }
  949. for ( uint8_t i = 0; i < ( sizeof(lowerDots) / sizeof(lowerDots[0]) ); i++ ) {
  950. leds[pgm_read_word_near(&lowerDots[i])].setHSV(0, 0, 0);
  951. }
  952. active = false;
  953. }
  954. }
  955. if ( fadeDots == 2 && active ) { // fade in/out dots
  956. static uint8_t counter = 1;
  957. static unsigned long lastFadeStep = millis();
  958. static boolean fadeInDone = true;
  959. if ( !fadeInDone ) {
  960. for ( uint8_t i = 0; i < ( sizeof(upperDots) / sizeof(upperDots[0]) ); i++ ) {
  961. leds[pgm_read_word_near(&upperDots[i])].fadeToBlackBy( 255 - counter * ( 255.0 / fadeSteps ) );
  962. }
  963. for ( uint8_t i = 0; i < ( sizeof(lowerDots) / sizeof(lowerDots[0]) ); i++ ) {
  964. leds[pgm_read_word_near(&lowerDots[i])].fadeToBlackBy( 255 - counter * ( 255.0 / fadeSteps ) );
  965. }
  966. if ( millis() - lastFadeStep >= fadeDelay ) {
  967. counter++;
  968. lastFadeStep = millis();
  969. }
  970. if ( counter > fadeSteps ) {
  971. counter = 1;
  972. fadeInDone = true;
  973. #ifdef DEBUG
  974. Serial.print(F("dot fade-in sequence took ")); // for debugging/checking
  975. Serial.print(millis() - firstRun);
  976. Serial.println(F(" ms"));
  977. #endif
  978. }
  979. }
  980. if ( lastRun - firstRun >= 950 - fadeDelay * fadeSteps ) {
  981. for ( uint8_t i = 0; i < ( sizeof(upperDots) / sizeof(upperDots[0]) ); i++ ) {
  982. leds[pgm_read_word_near(&upperDots[i])].fadeToBlackBy( counter * ( 255.0 / fadeSteps ) );
  983. }
  984. for ( uint8_t i = 0; i < ( sizeof(lowerDots) / sizeof(lowerDots[0]) ); i++ ) {
  985. leds[pgm_read_word_near(&lowerDots[i])].fadeToBlackBy( counter * ( 255.0 / fadeSteps ) );
  986. }
  987. if ( millis() - lastFadeStep >= fadeDelay ) {
  988. counter++;
  989. lastFadeStep = millis();
  990. }
  991. if ( counter > fadeSteps ) {
  992. counter = 1;
  993. active = false;
  994. fadeInDone = false;
  995. #ifdef DEBUG
  996. Serial.print(F("dot fading sequence took ")); // for debugging/checking
  997. Serial.print(millis() - firstRun);
  998. Serial.println(F(" ms"));
  999. #endif
  1000. }
  1001. }
  1002. }
  1003. }
  1004. #endif
  1005.  
  1006.  
  1007. #ifdef AUTOBRIGHTNESS
  1008. void readLDR() { // read LDR value 5 times and write average to avgLDR
  1009. static uint8_t runCounter = 1;
  1010. static uint16_t tmp = 0;
  1011. uint8_t readOut = map(analogRead(pinLDR), 0, 1023, 0, 250);
  1012. tmp += readOut;
  1013. if (runCounter == 5) {
  1014. avgLDR = ( tmp / 5 ) * factorLDR;
  1015. tmp = 0; runCounter = 0;
  1016. #ifdef DEBUG
  1017. if ( dbgLDR ) {
  1018. Serial.print(F("readLDR(): avgLDR value: "));
  1019. Serial.print(avgLDR);
  1020. }
  1021. #endif
  1022. if ( avgLDR < minBrightness ) avgLDR = minBrightness;
  1023. if ( avgLDR > brightness ) avgLDR = brightness;
  1024. if ( avgLDR >= upperLimitLDR && avgLDR < brightness ) avgLDR = brightness; // if avgLDR is above upperLimitLDR switch to max current brightness
  1025. if ( avgLDR <= lowerLimitLDR ) avgLDR = minBrightness; // if avgLDR is below lowerLimitLDR switch to minBrightness
  1026. #ifdef DEBUG
  1027. if ( dbgLDR ) {
  1028. Serial.print(F(" - adjusted to: "));
  1029. Serial.println(avgLDR);
  1030. }
  1031. #endif
  1032. }
  1033. runCounter++;
  1034. }
  1035. #endif
  1036.  
  1037. void setupClock() {
  1038. /* This sets time and date (if AUTODST is defined) on the clock/rtc */
  1039. clockStatus = 90; // clockStatus 9x = setup, relevant for other functions/coloring
  1040. while ( digitalRead(buttonA) == LOW || digitalRead(buttonB) == LOW ) { // do nothing until both buttons are released to avoid accidental inputs right away
  1041. #ifdef NODEMCU
  1042. yield();
  1043. #endif
  1044. }
  1045. tmElements_t setupTime; // Create a time element which will be used. Using the current time would
  1046. setupTime.Hour = 12; // give some problems (like time still running while setting hours/minutes)
  1047. setupTime.Minute = 0; // Setup starts at 12 (12 pm) (utc 12 if AUTODST is defined)
  1048. setupTime.Second = 0; //
  1049. setupTime.Day = 1; // date settings only used when AUTODST is defined, but will set them anyways
  1050. setupTime.Month = 2; // see above
  1051. setupTime.Year = 23; // current year - 2000 (2023 - 2000 = 23)
  1052. #ifdef USERTC
  1053. RtcDateTime writeTime;
  1054. #endif
  1055. #ifdef AUTODST
  1056. clockStatus = 91; // 91 = y/m/d setup
  1057. uint8_t y, m, d;
  1058. y = getUserInput(12, 20, 23, 99); // show Y + blank, get value from 21 - 99 into y
  1059. setupTime.Year = y;
  1060. m = getUserInput(16, 17, 1, 12); // show M, get value from 1 - 12 into m
  1061. setupTime.Month = m;
  1062. if ( m == 2 ) {
  1063. if ( leapYear(y + 2000) ) { // check for leap year...
  1064. #ifdef DEBUG // ...and get according day input ranges for each month
  1065. Serial.println(F("setupClock(): Leap year detected"));
  1066. #endif
  1067. d = getUserInput(13, 20, 1, 29);
  1068. } else {
  1069. d = getUserInput(13, 20, 1, 28);
  1070. }
  1071. }
  1072. if ( m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12 ) {
  1073. d = getUserInput(13, 20, 1, 31);
  1074. }
  1075. if ( m == 4 || m == 6 || m == 9 || m == 11 ) {
  1076. d = getUserInput(13, 20, 1, 30);
  1077. }
  1078. setupTime.Day = d;
  1079. #ifdef USERTC
  1080. writeTime = { 2000 + y, setupTime.Month, setupTime.Day,
  1081. setupTime.Hour, setupTime.Minute, setupTime.Second };
  1082. Rtc.SetDateTime(writeTime);
  1083. setTime(writeTime.Hour(), writeTime.Minute(), writeTime.Second(), writeTime.Day(), writeTime.Month(), writeTime.Year());
  1084. #else
  1085. setTime(setupTime.Hour, setupTime.Minute, setupTime.Second, setupTime.Day, setupTime.Month, setupTime.Year + 30);
  1086. #endif
  1087. #ifdef DEBUG
  1088. Serial.print(F("setupClock(): now is ")); Serial.println(now());
  1089. #endif
  1090. #endif
  1091. uint8_t lastInput = 0;
  1092. // hours
  1093. while ( lastInput != 2 ) {
  1094. clockStatus = 92; // 92 = HH setup
  1095. if ( lastInput == 1 ) {
  1096. if ( setupTime.Hour < 23 ) {
  1097. setupTime.Hour++;
  1098. } else {
  1099. setupTime.Hour = 0;
  1100. }
  1101. }
  1102. displayTime(makeTime(setupTime));
  1103. lastInput = inputButtons();
  1104. }
  1105. lastInput = 0;
  1106. // minutes
  1107. while ( lastInput != 2 ) {
  1108. clockStatus = 93; // 93 = MM setup
  1109. if ( lastInput == 1 ) {
  1110. if ( setupTime.Minute < 59 ) {
  1111. setupTime.Minute++;
  1112. } else {
  1113. setupTime.Minute = 0;
  1114. }
  1115. }
  1116. displayTime(makeTime(setupTime));
  1117. lastInput = inputButtons();
  1118. }
  1119. lastInput = 0;
  1120. // seconds
  1121. if ( LED_DIGITS == 6 ) {
  1122. while ( lastInput != 2 ) {
  1123. clockStatus = 94; // 94 = SS setup
  1124. if ( lastInput == 1 ) {
  1125. if ( setupTime.Second < 59 ) {
  1126. setupTime.Second++;
  1127. } else {
  1128. setupTime.Second = 0;
  1129. }
  1130. }
  1131. displayTime(makeTime(setupTime));
  1132. lastInput = inputButtons();
  1133. }
  1134. lastInput = 0;
  1135. }
  1136. #ifdef DEBUG
  1137. #ifdef AUTODST
  1138. Serial.print(F("setupClock(): "));
  1139. Serial.print(F("Y/M/D -> "));
  1140. Serial.print(2000 + setupTime.Year); Serial.print(F("/"));
  1141. Serial.print(setupTime.Month); Serial.print(F("/"));
  1142. Serial.println(setupTime.Day);
  1143. #endif
  1144. Serial.print(F("setupClock(): "));
  1145. Serial.print(F("HH:MM:SS -> "));
  1146. #ifdef AUTODST
  1147. Serial.print(F("AUTODST enabled, setting LOCAL time -> "));
  1148. #endif
  1149. if ( setupTime.Hour < 10 ) Serial.print(F("0"));
  1150. Serial.print(setupTime.Hour); Serial.print(F(":"));
  1151. if ( setupTime.Minute < 10 ) Serial.print(F("0"));
  1152. Serial.print(setupTime.Minute); Serial.print(F(":"));
  1153. if ( setupTime.Second < 10 ) Serial.print(F("0"));
  1154. Serial.println(setupTime.Second);
  1155. #endif
  1156. #ifdef USERTC
  1157. writeTime = { 2000 + setupTime.Year, setupTime.Month, setupTime.Day,
  1158. setupTime.Hour, setupTime.Minute, setupTime.Second };
  1159. #ifdef AUTODST
  1160. setupTime.Year += 30;
  1161. time_t t = myTimeZone.toUTC(makeTime(setupTime)); // get UTC time from entered time
  1162. writeTime = { year(t), month(t), day(t), hour(t), minute(t), second(t) };
  1163. #endif
  1164. Rtc.SetDateTime(writeTime);
  1165. setTime(writeTime.Hour(), writeTime.Minute(), writeTime.Second(), writeTime.Day(), writeTime.Month(), writeTime.Year());
  1166. #ifdef DEBUG
  1167. Serial.println(F("setupClock(): RTC time set"));
  1168. printTime();
  1169. #endif
  1170. #else
  1171. #ifdef AUTODST
  1172. setupTime.Year += 30;
  1173. time_t t = myTimeZone.toUTC(makeTime(setupTime)); // get UTC time from entered time
  1174. setTime(t);
  1175. #else
  1176. setupTime.Year += 30;
  1177. setTime(makeTime(setupTime));
  1178. #endif
  1179. #endif
  1180. clockStatus = 0;
  1181. #ifdef DEBUG
  1182. Serial.println(F("setupClock() done"));
  1183. #endif
  1184. }
  1185.  
  1186.  
  1187. uint16_t getUserInput(uint8_t sym1, uint8_t sym2, uint8_t startVal, uint8_t endVal) {
  1188. /* This will show two symbols on HH and allow to enter a 2 digit value using the buttons
  1189. and display the value on MM. */
  1190. static uint8_t lastInput = 0;
  1191. static uint8_t currentVal = startVal;
  1192. static bool newInput = true;
  1193. if ( newInput ) {
  1194. currentVal = startVal;
  1195. newInput = false;
  1196. }
  1197. while ( lastInput != 2 ) {
  1198. if ( lastInput == 1 ) {
  1199. if ( currentVal < endVal ) {
  1200. currentVal++;
  1201. } else {
  1202. currentVal = startVal;
  1203. }
  1204. }
  1205. FastLED.clear();
  1206. showDigit(sym1, digitPositions[0]);
  1207. showDigit(sym2, digitPositions[1]);
  1208. showDigit(currentVal / 10, digitPositions[2]);
  1209. showDigit(currentVal % 10, digitPositions[3]);
  1210. if ( millis() % 30 == 0 ) {
  1211. colorizeOutput(colorMode);
  1212. FastLED.show();
  1213. }
  1214. lastInput = inputButtons();
  1215. }
  1216. #ifdef DEBUG
  1217. Serial.print(F("getUserInput(): returned ")); Serial.println(currentVal);
  1218. #endif
  1219. lastInput = 0;
  1220. newInput = true;
  1221. return currentVal;
  1222. #ifdef DEBUG
  1223. Serial.print(F("getUserInput(): returned ")); Serial.println(currentVal);
  1224. #endif
  1225. }
  1226.  
  1227.  
  1228. void colorizeOutput(uint8_t mode) {
  1229. /* So far showDigit()/showSegment() only set some leds inside the array to values from "markerHSV" but we haven't updated
  1230. the leds yet using FastLED.show(). This function does the coloring of the right now single colored but "insivible"
  1231. output. This way color updates/cycles aren't tied to updating display contents */
  1232. static unsigned long lastColorChange = 0;
  1233. static uint8_t startColor = 0;
  1234. static uint8_t colorOffset = 0; // different offsets result in quite different results, depending on the amount of leds inside each segment...
  1235. // ...so it's set inside each color mode if required
  1236. /* mode 0 = check every segment if it's lit and assign a color based on position -> different color per digit
  1237. Checking the leds like this will not include the dots - they'll be colored later on */
  1238. if ( mode == 0 ) {
  1239. colorOffset = 256 / LED_DIGITS;
  1240. for ( uint8_t pos = 0; pos < LED_DIGITS; pos++ ) {
  1241. for ( uint8_t segment = 0; segment < 7; segment++ ) {
  1242. colorizeSegment(segment, pos, startColor + colorOffset * pos);
  1243. }
  1244. }
  1245. }
  1246. /* mode 1 = simply assign different colors with an offset of "colorOffset" to each led that's not black/off -
  1247. This will include the dots if they're supposed to be on - but they will be overwritten later for all modes */
  1248. if ( mode == 1 ) {
  1249. colorOffset = 32;
  1250. for ( uint16_t i = 0; i < LED_COUNT; i++ ) {
  1251. if ( leds[i] ) {
  1252. leds[i] = ColorFromPalette(currentPalette, startColor + i * colorOffset, brightness, LINEARBLEND);
  1253. }
  1254. }
  1255. }
  1256. /* mode 2 = check every segment if it's lit and assign a color based on segment -> different color per segment,
  1257. same across digits. Checking the leds like this will not include the dots - they'll be colored later on */
  1258. if ( mode == 2 ) {
  1259. colorOffset = 24;
  1260. for ( uint8_t pos = 0; pos < LED_DIGITS; pos++ ) {
  1261. for ( uint8_t segment = 0; segment < 7; segment++ ) {
  1262. colorizeSegment(segment, digitPositions[pos], startColor + 1 * colorOffset * segment);
  1263. }
  1264. }
  1265. }
  1266. /* mode 3 = same as above - but will assign colorOffsets depending on segment in a specific order (top/down effect) */
  1267. if ( mode == 3 ) {
  1268. uint8_t colorOffsets[7] = { 0, 24, 72, 96, 72, 24, 48 }; // colorOffsets for segments a-g
  1269. for ( uint8_t pos = 0; pos < LED_DIGITS; pos++ ) {
  1270. for ( uint8_t segment = 0; segment < 7; segment++ ) {
  1271. colorizeSegment(segment, digitPositions[pos], startColor + 1 * colorOffsets[segment]);
  1272. }
  1273. }
  1274. }
  1275. /* clockStatus >= 90 is used for coloring output while in setup mode */
  1276. if ( clockStatus >= 90 ) {
  1277. static boolean blinkFlag = true;
  1278. static unsigned long lastBlink = millis();
  1279. static uint8_t b = brightnessLevels[0];
  1280. if ( millis() - lastBlink > 333 ) { // blink switch frequency, 3 times a second
  1281. if ( blinkFlag ) {
  1282. blinkFlag = false;
  1283. b = brightnessLevels[1];
  1284. } else {
  1285. blinkFlag = true;
  1286. b = brightnessLevels[0];
  1287. }
  1288. lastBlink = millis();
  1289. } // unset values = red, set value = green, current value = yellow and blinkinkg
  1290. for ( uint8_t pos = 0; pos < LED_DIGITS; pos++ ) {
  1291. if ( clockStatus == 91 ) { // Y/M/D setup
  1292. colorHelper(digitPositions[0], 0, 255, brightness);
  1293. colorHelper(digitPositions[1], 0, 255, brightness);
  1294. colorHelper(digitPositions[2], 64, 255, b);
  1295. colorHelper(digitPositions[3], 64, 255, b);
  1296. }
  1297. if ( clockStatus == 92 ) { // hours
  1298. colorHelper(digitPositions[0], 64, 255, b);
  1299. colorHelper(digitPositions[1], 64, 255, b);
  1300. colorHelper(digitPositions[2], 0, 255, brightness);
  1301. colorHelper(digitPositions[3], 0, 255, brightness);
  1302. if ( LED_DIGITS == 6 ) {
  1303. colorHelper(digitPositions[4], 0, 255, brightness);
  1304. colorHelper(digitPositions[5], 0, 255, brightness);
  1305. }
  1306. }
  1307. if ( clockStatus == 93 ) { // minutes
  1308. colorHelper(digitPositions[0], 96, 255, brightness);
  1309. colorHelper(digitPositions[1], 96, 255, brightness);
  1310. colorHelper(digitPositions[2], 64, 255, b);
  1311. colorHelper(digitPositions[3], 64, 255, b);
  1312. if ( LED_DIGITS == 6 ) {
  1313. colorHelper(digitPositions[4], 0, 255, brightness);
  1314. colorHelper(digitPositions[5], 0, 255, brightness);
  1315. }
  1316. }
  1317. if ( clockStatus == 94 ) { // seconds
  1318. colorHelper(digitPositions[0], 96, 255, brightness);
  1319. colorHelper(digitPositions[1], 96, 255, brightness);
  1320. colorHelper(digitPositions[2], 96, 255, brightness);
  1321. colorHelper(digitPositions[3], 96, 255, brightness);
  1322. if ( LED_DIGITS == 6 ) {
  1323. colorHelper(digitPositions[4], 64, 255, b);
  1324. colorHelper(digitPositions[5], 64, 255, b);
  1325. }
  1326. }
  1327. }
  1328. }
  1329. /* The dots will always be colored in the same way, just using colors from the current palette. Depending on setup/parameters
  1330. this can otherwise lead to the dots looking quite different from the digits, so as before they're cycling through the
  1331. color palette once per minute */
  1332. if ( leds[pgm_read_word_near(&upperDots[0])] ) { // if the first led inside the array upperDot is lit...
  1333. for ( uint8_t i = 0; i < ( sizeof(upperDots) / sizeof(upperDots[0]) ); i++ ) { // ...start applying colors to all leds inside the array
  1334. if ( clockStatus == 0 ) {
  1335. leds[pgm_read_word_near(&upperDots[i])] = ColorFromPalette(currentPalette, second() * 4.25, brightness, LINEARBLEND);
  1336. } else {
  1337. leds[pgm_read_word_near(&upperDots[i])].setHSV(64, 255, brightness);
  1338. }
  1339. }
  1340. }
  1341. if ( leds[pgm_read_word_near(&lowerDots[0])] ) { // same as before for the lower dots...
  1342. for ( uint8_t i = ( sizeof(lowerDots) / sizeof(lowerDots[0]) ); i > 0; i-- ) {
  1343. if ( clockStatus == 0 ) {
  1344. leds[pgm_read_word_near(&lowerDots[i - 1])] = ColorFromPalette(currentPalette, second() * 4.25, brightness, LINEARBLEND);
  1345. } else {
  1346. leds[pgm_read_word_near(&lowerDots[i - 1])].setHSV(64, 255, brightness);
  1347. }
  1348. }
  1349. }
  1350. #ifdef FASTFORWARD
  1351. if ( millis() - lastColorChange > 15 ) {
  1352. #else
  1353. if ( millis() - lastColorChange > colorSpeed ) {
  1354. #endif
  1355. if ( reverseColorCycling ) {
  1356. startColor--;
  1357. } else {
  1358. startColor++;
  1359. }
  1360. lastColorChange = millis();
  1361. }
  1362. #ifdef AUTOBRIGHTNESS
  1363. if ( nightMode && clockStatus == 0 ) { // nightmode will overwrite everything that has happened so far...
  1364. for ( uint16_t i = 0; i < LED_COUNT; i++ ) {
  1365. if ( leds[i] ) {
  1366. if ( avgLDR == minBrightness ) {
  1367. leds[i].setHSV(nightColor[0], 255, nightColor[1] ); // and assign nightColor to all lit leds. Default is a very dark red.
  1368. FastLED.setDither(0);
  1369. } else {
  1370. FastLED.setDither(1);
  1371. }
  1372. }
  1373. }
  1374. }
  1375. #endif
  1376.  
  1377. /* // example for time based coloring
  1378. // for coloring based on current times the following will get local display time into
  1379. // checkTime if autoDST is defined as the clock is running in utc time then
  1380. #ifdef AUTODST
  1381. time_t checkTime = myTimeZone.toLocal(now());
  1382. #else
  1383. time_t checkTime = now();
  1384. #endif
  1385.  
  1386. // below if-loop simply checks for a given time and colors everything in green/blue accordingly
  1387. if ( hour(checkTime) > 6 && hour(checkTime) <= 22 ) { // if hour > 6 AND hour <= 22 ---> 07:00 - 22:59
  1388. for ( uint16_t i = 0; i < LED_COUNT; i++ ) { // for each position...
  1389. if ( leds[i] ) { // ...check led and if it's lit...
  1390. leds[i].setHSV(96, 255, brightness); // ...redraw with HSV color 96 -> green
  1391. }
  1392. }
  1393. } else { // ---> 23:00 - 06:59
  1394. for ( uint16_t i = 0; i < LED_COUNT; i++ ) { // for each position...
  1395. if ( leds[i] ) { // ...check led and if it's lit...
  1396. leds[i].setHSV(160, 255, brightness); // ...redraw with HSV color 160 -> blue
  1397. }
  1398. }
  1399. }
  1400. */
  1401. }
  1402.  
  1403.  
  1404. void colorizeSegment(uint8_t segment, uint8_t pos, uint8_t color) {
  1405. /* Checks if segment at position is on - and if it is, assigns color from current palette */
  1406. uint8_t ledAM = digitsLAM[pos]; // led access mode according to the position
  1407. if ( leds[pgm_read_word_near(&segGroups[segment + digitPositions[pos] * 7][0])] ) {
  1408. if ( ledAM == 0 ) {
  1409. for ( uint8_t i = 0; i < 2; i++ ) {
  1410. leds[pgm_read_word_near(&segGroups[segment + digitPositions[pos] * 7][i])] = ColorFromPalette(currentPalette, color, brightness, LINEARBLEND);
  1411. }
  1412. }
  1413. if ( ledAM == 1 ) {
  1414. uint16_t startLed = pgm_read_word_near(&segGroups[segment + digitPositions[pos] * 7][0]);
  1415. uint16_t endLed = pgm_read_word_near(&segGroups[segment + digitPositions[pos] * 7][1]);
  1416. for ( uint16_t i = startLed; i <= endLed; i++ ) {
  1417. leds[i] = ColorFromPalette(currentPalette, color, brightness, LINEARBLEND);
  1418. }
  1419. }
  1420. }
  1421. }
  1422.  
  1423.  
  1424. void colorHelper(uint8_t pos, uint8_t hue, uint8_t sat, uint8_t bri) {
  1425. /* Used for coloring digits inside setup routines/steps
  1426. It will simply set the digit at the given position to the given hsv values */
  1427. uint8_t ledAM = digitsLAM[pos]; // led access mode according to the position
  1428. for ( uint8_t segment = 0; segment < 7; segment++ ) {
  1429. if ( leds[pgm_read_word_near(&segGroups[segment + pos * 7][0])] ) { // if first led inside segment is lit...
  1430. if ( ledAM == 0 ) {
  1431. for ( uint8_t i = 0; i < 2; i++ ) { // assign hue to led 0 + 1 inside segment
  1432. leds[pgm_read_word_near(&segGroups[segment + pos * 7][i])].setHSV(hue, sat, bri);
  1433. }
  1434. }
  1435. if ( ledAM == 1 ) {
  1436. uint16_t startLed = pgm_read_word_near(&segGroups[segment + pos * 7][0]);
  1437. uint16_t endLed = pgm_read_word_near(&segGroups[segment + pos * 7][1]);
  1438. for ( uint16_t i = startLed; i <= endLed; i++ ) { // assign hue to led 0 - 1 inside segment
  1439. leds[i].setHSV(hue, sat, bri);
  1440. }
  1441. }
  1442. }
  1443. }
  1444. }
  1445.  
  1446.  
  1447. void displayTime(time_t t) {
  1448. #ifdef AUTODST
  1449. if ( clockStatus < 90 ) { // display adjusted times only while NOT in setup
  1450. t = myTimeZone.toLocal(t); // convert display time to local time zone according to rules on top of the sketch
  1451. }
  1452. #endif
  1453. if ( clockStatus >= 90 ) {
  1454. FastLED.clear();
  1455. }
  1456. /* hours */
  1457. if ( displayMode == 0 ) {
  1458. if ( hour(t) < 10 ) {
  1459. if ( leadingZero ) {
  1460. showDigit(0, digitPositions[0]);
  1461. }
  1462. } else {
  1463. showDigit(hour(t) / 10, digitPositions[0]);
  1464. }
  1465. showDigit(hour(t) % 10, digitPositions[1]);
  1466. } else if ( displayMode == 1 ) {
  1467. if ( hourFormat12(t) < 10 ) {
  1468. if ( leadingZero ) {
  1469. showDigit(0, digitPositions[0]);
  1470. }
  1471. } else {
  1472. showDigit(hourFormat12(t) / 10, digitPositions[0]);
  1473. }
  1474. showDigit(hourFormat12(t) % 10, digitPositions[1]);
  1475. }
  1476. /* minutes */
  1477. showDigit(minute(t) / 10, digitPositions[2]);
  1478. showDigit(minute(t) % 10, digitPositions[3]);
  1479. if ( LED_DIGITS == 6 ) {
  1480. /* seconds */
  1481. showDigit(second(t) / 10, digitPositions[4]);
  1482. showDigit(second(t) % 10, digitPositions[5]);
  1483. }
  1484. if ( clockStatus >= 90 ) { // in setup modes displayTime will also use colorizeOutput/FastLED.show!
  1485. static unsigned long lastRefresh = millis();
  1486. if ( isAM(t) && displayMode == 1 ) { // in 12h mode and if it's AM only light up the upper dots (while setting time)
  1487. showDots(1);
  1488. } else {
  1489. showDots(2);
  1490. }
  1491. if ( millis() - lastRefresh >= 25 ) {
  1492. colorizeOutput(colorMode);
  1493. FastLED.show();
  1494. lastRefresh = millis();
  1495. }
  1496. return;
  1497. }
  1498. /* dots */
  1499. if ( dotsBlinking ) {
  1500. if ( second(t) % 2 == 0 ) {
  1501. showDots(2);
  1502. }
  1503. } else {
  1504. showDots(2);
  1505. }
  1506. }
  1507.  
  1508.  
  1509. void showSegment(uint8_t segment, uint8_t segDisplay) {
  1510. // This shows the segments from top of the sketch on a given position (segDisplay). Order of positions/segDisplay is the order
  1511. // of definitions on the top, first one defined is segDisplay 0, second one is segDisplay 1 and so on...
  1512. // "firstLoop" is used to display all information only once per test if customHelper is defined
  1513. uint8_t ledAM = digitsLAM[segDisplay]; // led access mode according to the position
  1514. #ifdef DEBUG
  1515. #ifdef CUSTOMHELPER
  1516. if ( firstLoop ) {
  1517. Serial.print(F("LED_ACCESS_MODE for segment ")); Serial.print(segment);
  1518. Serial.print(F(" at position ")); Serial.print(segDisplay);
  1519. Serial.print(F(" is ")); Serial.print(ledAM);
  1520. }
  1521. #endif
  1522. #endif
  1523. if ( ledAM == 0 ) { // using both values inside the array to light up two leds
  1524. #ifdef DEBUG
  1525. #ifdef CUSTOMHELPER
  1526. if ( firstLoop ) {
  1527. Serial.print(F(". Leds "));
  1528. }
  1529. #endif
  1530. #endif
  1531. segment += segDisplay * 7;
  1532. for (uint8_t i = 0; i < 2; i++) {
  1533. leds[pgm_read_word_near(&segGroups[segment][i])].setHSV(markerHSV[0], markerHSV[1], markerHSV[2]);
  1534. #ifdef DEBUG
  1535. #ifdef CUSTOMHELPER
  1536. if ( firstLoop ) {
  1537. if ( i == 0 ) {
  1538. Serial.print(pgm_read_word_near(&segGroups[segment][i])); Serial.print(F(" and "));
  1539. }
  1540. if ( i == 1 ) {
  1541. Serial.println(pgm_read_word_near(&segGroups[segment][i]));
  1542. }
  1543. }
  1544. #endif
  1545. #endif
  1546. }
  1547. }
  1548. if ( ledAM == 1 ) { // using both values inside the array as start and end to light up multiple leds
  1549. segment += segDisplay * 7;
  1550. uint16_t startLed = pgm_read_word_near(&segGroups[segment][0]);
  1551. uint16_t endLed = pgm_read_word_near(&segGroups[segment][1]);
  1552. #ifdef DEBUG
  1553. #ifdef CUSTOMHELPER
  1554. if ( firstLoop ) {
  1555. Serial.print(F(". Leds ")); Serial.print(startLed);
  1556. Serial.print(F(" - ")); Serial.println(endLed);
  1557. }
  1558. #endif
  1559. #endif
  1560. for ( uint16_t i = startLed; i <= endLed; i++ ) {
  1561. leds[i].setHSV(markerHSV[0], markerHSV[1], markerHSV[2]);
  1562. }
  1563. }
  1564. }
  1565.  
  1566.  
  1567. void showDots(uint8_t dots) {
  1568. // dots 0 = upper dots, dots 1 = lower dots, dots 2 = all dots (right/left/both on Lazy 7 - Quick Build Edition)
  1569. if ( dots == 1 || dots == 2 ) {
  1570. for ( uint8_t i = 0; i < ( sizeof(upperDots) / sizeof(upperDots[0]) ); i++ ) {
  1571. leds[pgm_read_word_near(&upperDots[i])].setHSV(markerHSV[0], markerHSV[1], markerHSV[2]);
  1572. }
  1573. }
  1574. if ( dots == 0 || dots == 2 ) {
  1575. for ( uint8_t i = 0; i < ( sizeof(lowerDots) / sizeof(lowerDots[0]) ); i++ ) {
  1576. leds[pgm_read_word_near(&lowerDots[i])].setHSV(markerHSV[0], markerHSV[1], markerHSV[2]);
  1577. }
  1578. }
  1579. }
  1580.  
  1581.  
  1582. void showDigit(uint8_t digit, uint8_t pos) {
  1583. // This draws numbers using the according segments as defined on top of the sketch (0 - 9) or symbols/characters (index 10+)
  1584. for (uint8_t i = 0; i < 7; i++) {
  1585. if (pgm_read_byte_near(&digits[digit][i]) != 0) showSegment(i, pos);
  1586. }
  1587. }
  1588.  
  1589.  
  1590. void paletteSwitcher() {
  1591. /* As the name suggests this takes care of switching palettes. When adding palettes, make sure paletteCount increases
  1592. accordingly. A few examples of gradients/solid colors by using RGB values or HTML Color Codes below */
  1593. static uint8_t paletteCount = 6;
  1594. static uint8_t currentIndex = 0;
  1595. if ( clockStatus == 1 ) { // Clock is starting up, so load selected palette from eeprom...
  1596. uint8_t tmp = EEPROM.read(0);
  1597. if ( tmp >= 0 && tmp < paletteCount ) {
  1598. currentIndex = tmp; // 255 from eeprom would mean there's nothing been written yet, so checking range...
  1599. } else {
  1600. currentIndex = 0; // ...and default to 0 if returned value from eeprom is not 0 - 6
  1601. }
  1602. #ifdef DEBUG
  1603. Serial.print(F("paletteSwitcher(): loaded EEPROM value "));
  1604. Serial.println(tmp);
  1605. #endif
  1606. }
  1607. switch ( currentIndex ) {
  1608. case 0: currentPalette = CRGBPalette16( CRGB( 224, 0, 32 ),
  1609. CRGB( 0, 0, 244 ),
  1610. CRGB( 128, 0, 128 ),
  1611. CRGB( 224, 0, 64 ) ); break;
  1612. case 1: currentPalette = CRGBPalette16( CRGB( 224, 16, 0 ),
  1613. CRGB( 192, 64, 0 ),
  1614. CRGB( 192, 128, 0 ),
  1615. CRGB( 240, 40, 0 ) ); break;
  1616. case 2: currentPalette = CRGBPalette16( CRGB::Aquamarine,
  1617. CRGB::Turquoise,
  1618. CRGB::Blue,
  1619. CRGB::DeepSkyBlue ); break;
  1620. case 3: currentPalette = RainbowColors_p; break;
  1621. case 4: currentPalette = PartyColors_p; break;
  1622. case 5: currentPalette = CRGBPalette16( CRGB::LawnGreen ); break;
  1623. }
  1624. #ifdef DEBUG
  1625. Serial.print(F("paletteSwitcher(): selected palette "));
  1626. Serial.println(currentIndex);
  1627. #endif
  1628. if ( clockStatus == 0 ) { // only save selected palette to eeprom if clock is in normal running mode, not while in startup/setup/whatever
  1629. EEPROM.put(0, currentIndex);
  1630. #ifdef NODEMCU
  1631. EEPROM.commit();
  1632. #endif
  1633. #ifdef DEBUG
  1634. Serial.print(F("paletteSwitcher(): saved index "));
  1635. Serial.print(currentIndex);
  1636. Serial.println(F(" to eeprom"));
  1637. #endif
  1638. }
  1639. if ( currentIndex < paletteCount - 1 ) {
  1640. currentIndex++;
  1641. } else {
  1642. currentIndex = 0;
  1643. }
  1644. if ( colorPreview ) {
  1645. previewMode();
  1646. }
  1647. #ifdef DEBUG
  1648. Serial.println(F("paletteSwitcher() done"));
  1649. #endif
  1650. }
  1651.  
  1652.  
  1653. void brightnessSwitcher() {
  1654. static uint8_t currentIndex = 0;
  1655. if ( clockStatus == 1 ) { // Clock is starting up, so load selected palette from eeprom...
  1656. uint8_t tmp = EEPROM.read(1);
  1657. if ( tmp >= 0 && tmp < 5 ) {
  1658. currentIndex = tmp; // 255 from eeprom would mean there's nothing been written yet, so checking range...
  1659. } else {
  1660. currentIndex = 0; // ...and default to 0 if returned value from eeprom is not 0 - 2
  1661. }
  1662. #ifdef DEBUG
  1663. Serial.print(F("brightnessSwitcher(): loaded EEPROM value "));
  1664. Serial.println(tmp);
  1665. #endif
  1666. }
  1667. switch ( currentIndex ) {
  1668. case 0: brightness = brightnessLevels[currentIndex]; break;
  1669. case 1: brightness = brightnessLevels[currentIndex]; break;
  1670. case 2: brightness = brightnessLevels[currentIndex]; break;
  1671. case 3: brightness = brightnessLevels[currentIndex]; break;
  1672. case 4: brightness = brightnessLevels[currentIndex]; break;
  1673. }
  1674. #ifdef DEBUG
  1675. Serial.print(F("brightnessSwitcher(): selected brightness index "));
  1676. Serial.println(currentIndex);
  1677. #endif
  1678. if ( clockStatus == 0 ) { // only save selected brightness to eeprom if clock is in normal running mode, not while in startup/setup/whatever
  1679. EEPROM.put(1, currentIndex);
  1680. #ifdef NODEMCU
  1681. EEPROM.commit();
  1682. #endif
  1683. #ifdef DEBUG
  1684. Serial.print(F("brightnessSwitcher(): saved index "));
  1685. Serial.print(currentIndex);
  1686. Serial.println(F(" to eeprom"));
  1687. #endif
  1688. }
  1689. if ( currentIndex < 2 ) {
  1690. currentIndex++;
  1691. } else {
  1692. currentIndex = 0;
  1693. }
  1694. #ifdef DEBUG
  1695. Serial.println(F("brightnessSwitcher() done"));
  1696. #endif
  1697. }
  1698.  
  1699.  
  1700. void colorModeSwitcher() {
  1701. static uint8_t currentIndex = 0;
  1702. if ( clockStatus == 1 ) { // Clock is starting up, so load selected palette from eeprom...
  1703. if ( colorMode != 0 ) return; // 0 is default, if it's different on startup the config is set differently, so exit here
  1704. uint8_t tmp = EEPROM.read(5);
  1705. if ( tmp >= 0 && tmp < 4 ) { // make sure tmp < 3 is increased if color modes are added in colorizeOutput()!
  1706. currentIndex = tmp; // 255 from eeprom would mean there's nothing been written yet, so checking range...
  1707. } else {
  1708. currentIndex = 0; // ...and default to 0 if returned value from eeprom is not 0 - 2
  1709. }
  1710. #ifdef DEBUG
  1711. Serial.print(F("colorModeSwitcher(): loaded EEPROM value "));
  1712. Serial.println(tmp);
  1713. #endif
  1714. }
  1715. colorMode = currentIndex;
  1716. #ifdef DEBUG
  1717. Serial.print(F("colorModeSwitcher(): selected colorMode "));
  1718. Serial.println(currentIndex);
  1719. #endif
  1720. if ( clockStatus == 0 ) { // only save selected colorMode to eeprom if clock is in normal running mode, not while in startup/setup/whatever
  1721. EEPROM.put(5, currentIndex);
  1722. #ifdef NODEMCU
  1723. EEPROM.commit();
  1724. #endif
  1725. #ifdef DEBUG
  1726. Serial.print(F("colorModeSwitcher(): saved index "));
  1727. Serial.print(currentIndex);
  1728. Serial.println(F(" to eeprom"));
  1729. #endif
  1730. }
  1731. if ( currentIndex < 3 ) {
  1732. currentIndex++;
  1733. } else {
  1734. currentIndex = 0;
  1735. }
  1736. if ( colorPreview ) {
  1737. previewMode();
  1738. }
  1739. #ifdef DEBUG
  1740. Serial.println(F("colorModeSwitcher() done"));
  1741. #endif
  1742. }
  1743.  
  1744.  
  1745. void displayModeSwitcher() {
  1746. static uint8_t currentIndex = 0;
  1747. if ( clockStatus == 1 ) { // Clock is starting up, so load selected palette from eeprom...
  1748. if ( displayMode != 0 ) return; // 0 is default, if it's different on startup the config is set differently, so exit here
  1749. uint8_t tmp = EEPROM.read(5);
  1750. if ( tmp >= 0 && tmp < 2 ) { // make sure tmp < 2 is increased if display modes are added
  1751. currentIndex = tmp; // 255 from eeprom would mean there's nothing been written yet, so checking range...
  1752. } else {
  1753. currentIndex = 0; // ...and default to 0 if returned value from eeprom is not 0 - 1 (24h/12h mode)
  1754. }
  1755. #ifdef DEBUG
  1756. Serial.print(F("displayModeSwitcher(): loaded EEPROM value "));
  1757. Serial.println(tmp);
  1758. #endif
  1759. }
  1760. displayMode = currentIndex;
  1761. #ifdef DEBUG
  1762. Serial.print(F("displayModeSwitcher(): selected displayMode "));
  1763. Serial.println(currentIndex);
  1764. #endif
  1765. if ( clockStatus == 0 ) { // only save selected colorMode to eeprom if clock is in normal running mode, not while in startup/setup/whatever
  1766. EEPROM.put(2, currentIndex);
  1767. #ifdef NODEMCU
  1768. EEPROM.commit();
  1769. #endif
  1770. #ifdef DEBUG
  1771. Serial.print(F("displayModeSwitcher(): saved index "));
  1772. Serial.print(currentIndex);
  1773. Serial.println(F(" to eeprom"));
  1774. #endif
  1775. }
  1776. if ( clockStatus == 0 ) { // show 12h/24h for 2 seconds after selected in normal run mode, don't show this on startup (status 1)
  1777. FastLED.clear();
  1778. unsigned long timer = millis();
  1779. while ( millis() - timer <= 2000 ) {
  1780. if ( currentIndex == 0 ) {
  1781. showDigit(2, digitPositions[0]);
  1782. showDigit(4, digitPositions[1]);
  1783. showDigit(19, digitPositions[3]);
  1784. }
  1785. if ( currentIndex == 1 ) {
  1786. showDigit(1, digitPositions[0]);
  1787. showDigit(2, digitPositions[1]);
  1788. showDigit(19, digitPositions[3]);
  1789. }
  1790. colorizeOutput(colorMode);
  1791. if ( millis() % 50 == 0 ) {
  1792. FastLED.show();
  1793. }
  1794. #ifdef NODEMCU
  1795. yield();
  1796. #endif
  1797. }
  1798. }
  1799. if ( currentIndex < 1 ) {
  1800. currentIndex++;
  1801. } else {
  1802. currentIndex = 0;
  1803. }
  1804. #ifdef DEBUG
  1805. Serial.println(F("displayModeSwitcher() done"));
  1806. #endif
  1807. }
  1808.  
  1809.  
  1810. void previewMode() {
  1811. /* This will simply display "8" on all positions, speed up the color cyling and preview the
  1812. selected palette or colorMode */
  1813. if ( clockStatus == 1 ) return; // don't preview when starting up
  1814. unsigned long previewStart = millis();
  1815. uint16_t colorSpeedBak = colorSpeed;
  1816. colorSpeed = 5;
  1817. while ( millis() - previewStart <= uint16_t ( colorPreviewDuration * 1000L ) ) {
  1818. for ( uint8_t i = 0; i < LED_DIGITS; i++ ) {
  1819. showDigit(8, i);
  1820. }
  1821. colorizeOutput(colorMode);
  1822. FastLED.show();
  1823. #ifdef NODEMCU
  1824. yield();
  1825. #endif
  1826. }
  1827. colorSpeed = colorSpeedBak;
  1828. FastLED.clear();
  1829. }
  1830. #endif /* LEDSTUFF */
  1831.  
  1832.  
  1833. bool leapYear(uint16_t y) {
  1834. boolean isLeapYear = false;
  1835. if (y % 4 == 0) isLeapYear = true;
  1836. if (y % 100 == 0 && y % 400 != 0) isLeapYear = false;
  1837. if (y % 400 == 0) isLeapYear = true;
  1838. if ( isLeapYear ) return true; else return false;
  1839. }
  1840.  
  1841.  
  1842. uint8_t inputButtons() {
  1843. /* This scans for button presses and keeps track of delay/repeat for user inputs
  1844. Short keypresses will only be returned when buttons are released before repeatDelay
  1845. is reached. This is to avoid constantly sending 1 or 2 when executing a long button
  1846. press and/or multiple buttons.
  1847. Note: Buttons are using pinMode INPUT_PULLUP, so HIGH = not pressed, LOW = pressed! */
  1848. static uint8_t scanInterval = 30; // only check buttons every 30ms
  1849. static uint16_t repeatDelay = 1000; // delay in milliseconds before repeating detected keypresses
  1850. static uint8_t repeatRate = 1000 / 10; // 10 chars per 1000 milliseconds
  1851. static uint8_t minTime = scanInterval * 2; // minimum time to register a button as pressed
  1852. static unsigned long lastReadout = millis(); // keeps track of when the last readout happened
  1853. static unsigned long lastReturn = millis(); // keeps track of when the last readout value was returned
  1854. static uint8_t lastState = 0; // button state from previous scan
  1855. uint8_t currentState = 0; // button state from current scan
  1856. uint8_t retVal = 0; // return value, will be 0 if no button is pressed
  1857. static unsigned long eventStart = millis(); // keep track of when button states are changing
  1858. if ( millis() - lastReadout < scanInterval ) return 0; // only scan for button presses every <scanInterval> ms
  1859. if ( digitalRead(buttonA) == LOW ) currentState += 1;
  1860. if ( digitalRead(buttonB) == LOW ) currentState += 2;
  1861. if ( currentState == 0 && currentState == lastState ) {
  1862. btnRepeatCounter = 0;
  1863. }
  1864. if ( currentState != 0 && currentState != lastState ) { // if any button is pressed and different from the previous scan...
  1865. eventStart = millis(); // ...reset eventStart to current time
  1866. btnRepeatCounter = 0; // ...and reset global variable btnRepeatCounter
  1867. }
  1868. if ( currentState != 0 && currentState == lastState ) { // if same input has been detected at least twice (2x scanInterval)...
  1869. if ( millis() - eventStart >= repeatDelay ) { // ...and longer than repeatDelay...
  1870. if ( millis() - lastReturn >= repeatRate ) { // ...check for repeatRate...
  1871. retVal = currentState; // ...and set retVal to currentState
  1872. btnRepeatCounter++;
  1873. lastReturn = millis();
  1874. } else retVal = 0; // return 0 if repeatDelay hasn't been reached yet
  1875. }
  1876. }
  1877. if ( currentState == 0 && currentState != lastState && millis() - eventStart >= minTime && btnRepeatCounter == 0 ) {
  1878. retVal = lastState; // return lastState if all buttons are released after having been pressed for <minTime> ms
  1879. btnRepeatCounter = 0;
  1880. }
  1881. lastState = currentState;
  1882. lastReadout = millis();
  1883. #ifdef DEBUG // output some information and read serial input, if available
  1884. uint8_t serialInput = dbgInput();
  1885. if ( serialInput != 0 ) {
  1886. Serial.print(F("inputButtons(): Serial input detected: ")); Serial.println(serialInput);
  1887. retVal = serialInput;
  1888. }
  1889. if ( retVal != 0 ){
  1890. Serial.print(F("inputButtons(): Return value is: ")) ; Serial.print(retVal); Serial.print(F(" - btnRepeatCounter is: ")); Serial.println(btnRepeatCounter);
  1891. }
  1892. #endif
  1893. return retVal;
  1894. }
  1895.  
  1896.  
  1897. // following will only be included if USENTP is defined
  1898. #ifdef USENTP
  1899. /* This syncs system time to the RTC at startup and will periodically do other sync related
  1900. things, like syncing rtc to ntp time */
  1901. void syncHelper() {
  1902. static unsigned long lastSync = millis(); // keeps track of the last time a sync attempt has been made
  1903. if ( millis() - lastSync < 60000 && clockStatus != 1 ) return; // only allow one ntp request per minute
  1904. if ( WiFi.status() != WL_CONNECTED ) {
  1905. #ifdef DEBUG
  1906. Serial.println(F("syncHelper(): No WiFi connection"));
  1907. return;
  1908. #endif
  1909. }
  1910. #ifndef USERTC
  1911. #ifndef USENTP
  1912. #ifdef DEBUG
  1913. Serial.println(F("syncHelper(): No RTC and no NTP configured, nothing to do..."));
  1914. return;
  1915. #endif
  1916. #endif
  1917. #endif
  1918. time_t ntpTime = 0;
  1919. #ifdef USERTC
  1920. RtcDateTime ntpTimeConverted;
  1921. #endif
  1922. if ( clockStatus == 1 ) { // looks like the sketch has just started running...
  1923. #ifdef DEBUG
  1924. Serial.println(F("syncHelper(): Initial sync on power up..."));
  1925. #endif
  1926. ntpTime = getTimeNTP();
  1927. #ifdef DEBUG
  1928. Serial.print(F("syncHelper(): NTP result is "));
  1929. Serial.println(ntpTime);
  1930. #endif
  1931. lastSync = millis();
  1932. } else {
  1933. #ifdef DEBUG
  1934. Serial.println(F("syncHelper(): Resyncing to NTP..."));
  1935. #endif
  1936. ntpTime = getTimeNTP();
  1937. #ifdef DEBUG
  1938. Serial.print(F("syncHelper(): NTP result is "));
  1939. Serial.println(ntpTime);
  1940. #endif
  1941. lastSync = millis();
  1942. }
  1943. #ifdef USERTC
  1944. ntpTimeConverted = { year(ntpTime), month(ntpTime), day(ntpTime),
  1945. hour(ntpTime), minute(ntpTime), second(ntpTime) };
  1946. RtcDateTime rtcTime = Rtc.GetDateTime(); // get current time from the rtc....
  1947. if ( ntpTime > 100 ) {
  1948. Rtc.SetDateTime(ntpTimeConverted);
  1949. }
  1950. #else
  1951. time_t sysTime = now(); // ...or from system
  1952. #ifdef DEBUG
  1953. Serial.println(F("syncHelper(): No RTC configured, using system time"));
  1954. Serial.print(F("syncHelper(): sysTime was "));
  1955. Serial.println(now());
  1956. #endif
  1957. if ( ntpTime > 100 ) {
  1958. setTime(ntpTime);
  1959. }
  1960. #endif
  1961. #ifdef DEBUG
  1962. Serial.println(F("syncHelper() done"));
  1963. #endif
  1964. }
  1965.  
  1966.  
  1967. time_t getTimeNTP() {
  1968. unsigned long startTime = millis();
  1969. time_t timeNTP;
  1970. if ( WiFi.status() != WL_CONNECTED ) {
  1971. #ifdef DEBUG
  1972. Serial.print(F("getTimeNTP(): Not connected, WiFi.status is "));
  1973. Serial.println(WiFi.status());
  1974. #endif
  1975. } // Sometimes the connection doesn't work right away although status is WL_CONNECTED...
  1976. while ( millis() - startTime < 2000 ) { // ...so we'll wait a moment before causing network traffic
  1977. #ifdef NODEMCU
  1978. yield();
  1979. #endif
  1980. }
  1981. timeClient.update();
  1982. timeNTP = timeClient.getEpochTime();
  1983. if ( timeNTP < 100 ) {
  1984. #ifdef DEBUG
  1985. Serial.print(F("getTimeNTP(): NTP returned ")); Serial.println(timeNTP);
  1986. Serial.print(F(" - trying again..."));
  1987. #endif
  1988. }
  1989. timeClient.update();
  1990. timeNTP = timeClient.getEpochTime();
  1991. if ( timeNTP < 100 ) {
  1992. #ifdef DEBUG
  1993. Serial.print(F("getTimeNTP(): NTP returned ")); Serial.println(timeNTP);
  1994. Serial.print(F(" - giving up"));
  1995. #endif
  1996. }
  1997. #ifdef DEBUG
  1998. Serial.println(F("getTimeNTP() done"));
  1999. #endif
  2000. return timeNTP;
  2001. }
  2002. #endif
  2003. // ---
  2004.  
  2005.  
  2006. // functions below will only be included if DEBUG is defined on top of the sketch
  2007. #ifdef DEBUG
  2008. void printTime() {
  2009. /* outputs current system and RTC time to the serial monitor, adds autoDST if defined */
  2010. time_t tmp = now();
  2011. #ifdef USERTC
  2012. RtcDateTime tmp2 = Rtc.GetDateTime();
  2013. setTime(tmp2.Hour(), tmp2.Minute(), tmp2.Second(),
  2014. tmp2.Day(), tmp2.Month(), tmp2.Year() );
  2015. tmp = now();
  2016. #endif
  2017. Serial.println(F("-----------------------------------"));
  2018. Serial.print(F("System time is : "));
  2019. if ( hour(tmp) < 10 ) Serial.print(F("0"));
  2020. Serial.print(hour(tmp)); Serial.print(F(":"));
  2021. if ( minute(tmp) < 10 ) Serial.print(F("0"));
  2022. Serial.print(minute(tmp)); Serial.print(F(":"));
  2023. if ( second(tmp) < 10 ) Serial.print(F("0"));
  2024. Serial.println(second(tmp));
  2025. Serial.print(F("System date is : "));
  2026. Serial.print(year(tmp)); Serial.print("-");
  2027. Serial.print(month(tmp)); Serial.print("-");
  2028. Serial.print(day(tmp)); Serial.println(F(" (Y/M/D)"));
  2029. #ifdef USERTC
  2030. Serial.print(F("RTC time is : "));
  2031. if ( tmp2.Hour() < 10 ) Serial.print(F("0"));
  2032. Serial.print(tmp2.Hour()); Serial.print(F(":"));
  2033. if ( tmp2.Minute() < 10 ) Serial.print(F("0"));
  2034. Serial.print(tmp2.Minute()); Serial.print(F(":"));
  2035. if ( tmp2.Second() < 10 ) Serial.print(F("0"));
  2036. Serial.println(tmp2.Second());
  2037. Serial.print(F("RTC date is : "));
  2038. Serial.print( tmp2.Year()); Serial.print("-");
  2039. Serial.print( tmp2.Month()); Serial.print("-");
  2040. Serial.print( tmp2.Day()); Serial.println(F(" (Y/M/D)"));
  2041. #endif
  2042. #ifdef AUTODST
  2043. tmp = myTimeZone.toLocal(tmp);
  2044. Serial.print(F("autoDST time is: "));
  2045. if ( hour(tmp) < 10 ) Serial.print(F("0"));
  2046. Serial.print(hour(tmp)); Serial.print(F(":"));
  2047. if ( minute(tmp) < 10 ) Serial.print(F("0"));
  2048. Serial.print(minute(tmp)); Serial.print(F(":"));
  2049. if ( second(tmp) < 10 ) Serial.print(F("0"));
  2050. Serial.println(second(tmp));
  2051. Serial.print(F("autoDST date is: "));
  2052. Serial.print(year(tmp)); Serial.print("-");
  2053. Serial.print(month(tmp)); Serial.print("-");
  2054. Serial.print(day(tmp)); Serial.println(F(" (Y/M/D)"));
  2055. #endif
  2056. Serial.println(F("-----------------------------------"));
  2057. }
  2058.  
  2059.  
  2060. uint8_t dbgInput() {
  2061. /* this catches input from the serial console and hands it over to inputButtons() if DEBUG is defined
  2062. Serial input "7" matches buttonA, "8" matches buttonB, "9" matches buttonA + buttonB */
  2063. if ( Serial.available() > 0 ) {
  2064. uint8_t incomingByte = 0;
  2065. incomingByte = Serial.read();
  2066. if ( incomingByte == 52 ) { // 4 - long press buttonA
  2067. btnRepeatCounter = 10;
  2068. return 1;
  2069. }
  2070. if ( incomingByte == 53 ) { // 5 - long press buttonB
  2071. btnRepeatCounter = 10;
  2072. return 2;
  2073. }
  2074. if ( incomingByte == 54 ) { // 6 - long press buttonA + buttonB
  2075. btnRepeatCounter = 10;
  2076. return 3;
  2077. }
  2078. if ( incomingByte == 55 ) return 1; // 7 - buttonA
  2079. if ( incomingByte == 56 ) return 2; // 8 - buttonB
  2080. if ( incomingByte == 57 ) return 3; // 9 - buttonA + buttonB
  2081. }
  2082. return 0;
  2083. }
  2084. #endif
  2085. // ---
  2086.  
  2087.  
  2088. #ifdef USEWIFI
  2089. void connectWPS() { // join network using wps. Will try for 3 times before exiting...
  2090. #ifdef DEBUG
  2091. Serial.println(F("connectWPS(): Initializing WPS setup..."));
  2092. #endif
  2093. uint8_t counter = 1;
  2094. static unsigned long startTimer = millis();
  2095. #ifdef LEDSTUFF
  2096. FastLED.clear();
  2097. showDigit(10, digitPositions[0]);
  2098. showDigit(11, digitPositions[1]);
  2099. showDigit(12, digitPositions[2]);
  2100. showDigit(counter, digitPositions[3]);
  2101. colorizeOutput(colorMode);
  2102. FastLED.show();
  2103. #endif
  2104. while ( counter < 4 ) {
  2105. #ifdef LEDSTUFF
  2106. if ( millis() % 50 == 0 ) {
  2107. FastLED.clear();
  2108. showDigit(10, digitPositions[0]);
  2109. showDigit(11, digitPositions[1]);
  2110. showDigit(12, digitPositions[2]);
  2111. showDigit(counter, digitPositions[3]);
  2112. colorizeOutput(colorMode);
  2113. FastLED.show();
  2114. }
  2115. #endif
  2116. if ( millis() - startTimer > 300 ) {
  2117. #ifdef DEBUG
  2118. Serial.print(F("connectWPS(): Waiting for WiFi/WPS, try "));
  2119. Serial.println(counter);
  2120. #endif
  2121. WiFi.beginWPSConfig();
  2122. if ( WiFi.SSID().length() <= 0 ) counter++; else counter = 4;
  2123. startTimer = millis();
  2124. }
  2125. #ifdef NODEMCU
  2126. yield();
  2127. #endif
  2128. }
  2129. FastLED.clear();
  2130. startTimer = millis();
  2131. if ( WiFi.SSID().length() > 0 ) {
  2132. #ifdef LEDSTUFF
  2133. FastLED.clear();
  2134. showDigit(5, digitPositions[0]);
  2135. showDigit(5, digitPositions[1]);
  2136. showDigit(1, digitPositions[2]);
  2137. showDigit(13, digitPositions[3]);
  2138. colorizeOutput(colorMode);
  2139. FastLED.show();
  2140. #endif
  2141. #ifdef DEBUG
  2142. Serial.print(F("connectWPS(): Connected to SSID: ")); Serial.println(WiFi.SSID());
  2143. #endif
  2144. while ( millis() - startTimer < 2000 ) {
  2145. #ifdef NODEMCU
  2146. yield();
  2147. #endif
  2148. }
  2149. #ifdef USENTP
  2150. clockStatus = 1;
  2151. syncHelper();
  2152. clockStatus = 0;
  2153. #endif USENTP
  2154. } else {
  2155. #ifdef DEBUG
  2156. Serial.println(F("connectWPS(): Failed, no WPS connection established"));
  2157. #endif
  2158. }
  2159. #ifdef DEBUG
  2160. Serial.println(F("connectWPS() done"));
  2161. #endif
  2162. }
  2163. #endif
  2164.  
  2165.  
  2166. #ifdef CUSTOMHELPER
  2167. /* This assists in troubleshooting and basic configuration. Testing all neccessary steps to get
  2168. showSegment(), showDigit(), showDots() to work. Ugly and using delay() but does the job ^^ */
  2169. void customHelper() {
  2170. markerHSV[0] = 96;
  2171. markerHSV[1] = 255;
  2172. markerHSV[2] = 60;
  2173. colorModeSwitcher();
  2174. paletteSwitcher();
  2175. brightness = 50;
  2176. currentPalette = RainbowColors_p;
  2177. uint8_t test = 1;
  2178. #ifdef DEBUG
  2179. Serial.println(F("\n\n\nSome kind of troubleshooting/custom assistant... ^^"));
  2180. Serial.println(F("\nTests will finish before proceeding to the next one.\n"));
  2181. Serial.print(F("The first step is to check all leds, so this test will\nsimply light up all the leds from 0 to ")); Serial.println(LED_COUNT - 1);
  2182. Serial.println(F("Press button A (or send 7 using serial input) to advance to the next step...\n"));
  2183. #endif
  2184. while ( test == 1 ) {
  2185. for ( uint16_t i = 0; i < LED_COUNT; i++ ) {
  2186. leds[i].setHSV(markerHSV[0], markerHSV[1], markerHSV[2]);
  2187. FastLED.show();
  2188. delay(75);
  2189. if ( inputButtons() != 0 ) test++;
  2190. }
  2191. FastLED.clear();
  2192. delay(300);
  2193. }
  2194. #ifdef DEBUG
  2195. Serial.println(F("\n\n\nNext we will light up segments 0-6 (a-g) at position 0, this"));
  2196. Serial.println(F("will show if all of the leds inside segArray[][] for position 0 are correct."));
  2197. Serial.println(F("Press button A (or send 7 using serial input) to advance to the next step...\n"));
  2198. #endif
  2199. FastLED.clear();
  2200. while ( test == 2 ) {
  2201. for ( uint8_t i = 0; i < 7; i++ ) {
  2202. showSegment(i, 0);
  2203. FastLED.show();
  2204. delay(750);
  2205. if ( inputButtons() != 0 ) test++;
  2206. }
  2207. FastLED.clear();
  2208. FastLED.show();
  2209. delay(300);
  2210. firstLoop = false;
  2211. }
  2212. #ifdef DEBUG
  2213. Serial.println(F("\n\nNow let's check this for all the positions as defined (LED_DIGITS 4 or 6), starting from 0..."));
  2214. Serial.println(F("Press button A (or send 7 using serial input) to advance to the next step...\n"));
  2215. #endif
  2216. firstLoop = true;
  2217. while ( test == 3 ) {
  2218. for ( uint8_t pos = 0; pos < LED_DIGITS; pos++ ) {
  2219. for ( uint8_t i = 0; i < 7; i++ ) {
  2220. showSegment(i, pos);
  2221. FastLED.show();
  2222. delay(400);
  2223. if ( inputButtons() != 0 ) test++;
  2224. }
  2225. if ( firstLoop ) Serial.println();
  2226. }
  2227. FastLED.clear();
  2228. FastLED.show();
  2229. delay(300);
  2230. firstLoop = false;
  2231. }
  2232. #ifdef DEBUG
  2233. Serial.println(F("\n\nTesting showDigit() on position 0, displaying 0-9"));
  2234. Serial.println(F("Press button A (or send 7 using serial input) to advance to the next step...\n"));
  2235. #endif
  2236. while ( test == 4 ) {
  2237. for ( uint8_t i = 0; i < 10; i++ ) {
  2238. FastLED.clear();
  2239. showDigit(i, 0);
  2240. if ( inputButtons() != 0 ) test++;
  2241. FastLED.show();
  2242. delay(500);
  2243. }
  2244. FastLED.clear();
  2245. FastLED.show();
  2246. delay(300);
  2247. }
  2248. #ifdef DEBUG
  2249. Serial.println(F("\n\nTesting showDots() lighting up the upper/lower dots in a repeating pattern..."));
  2250. Serial.println(F("Press button A (or send 7 using serial input) to advance to the next step...\n"));
  2251. #endif
  2252. while ( test == 5 ) {
  2253. if ( second() % 2 == 1 ) {
  2254. showDots(0);
  2255. } else {
  2256. showDots(1);
  2257. }
  2258. if ( inputButtons() != 0 ) test++;
  2259. FastLED.show();
  2260. delay(20);
  2261. FastLED.clear();
  2262. }
  2263. FastLED.clear();
  2264. FastLED.show();
  2265. delay(300);
  2266. #ifdef DEBUG
  2267. Serial.println(F("\n\nFinal test, displaying 0-9 on all positions, using colorizeOutput();"));
  2268. Serial.println(F("Press button A (or send 7 using serial input) to start over...\n"));
  2269. #endif
  2270. while ( test == 6 ) {
  2271. for ( uint8_t i = 0; i < 10; i++ ) {
  2272. for ( uint8_t pos = 0; pos < LED_DIGITS; pos++ ) {
  2273. showDigit(i, pos);
  2274. }
  2275. if ( inputButtons() != 0 ) test++;
  2276. colorizeOutput(1);
  2277. FastLED.show();
  2278. delay(500);
  2279. FastLED.clear();
  2280. }
  2281. }
  2282. FastLED.clear();
  2283. FastLED.show();
  2284. delay(500);
  2285. }
  2286. #endif
  2287.  
  2288. /* Wooohaa... this one took a bit longer than expected... ^^ /daniel cikic - 07/2021 */
Advertisement
Add Comment
Please, Sign In to add comment