Guest User

Light Arduino File

a guest
Oct 5th, 2021
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 47.94 KB | None | 0 0
  1. #define FASTLED_INTERRUPT_RETRY_COUNT 0
  2. // #define FASTLED_ESP8266_RAW_PIN_ORDER
  3.  
  4. /****************** LIBRARY SECTION *************************************/
  5.  
  6. #include <FastLED.h> //https://github.com/FastLED/FastLED
  7. #include <SimpleTimer.h> //https://github.com/thehookup/Simple-Timer-Library
  8. #include <PubSubClient.h> //https://github.com/knolleary/pubsubclient
  9. #include <ESP8266WiFi.h> //if you get an error here you need to install the ESP8266 board manager
  10. #include <ESP8266mDNS.h> //if you get an error here you need to install the ESP8266 board manager
  11. #include <ArduinoOTA.h> //ArduinoOTA is now included with the ArduinoIDE
  12. #include <ESP8266WebServer.h>
  13. #include <ESP8266HTTPUpdateServer.h>
  14. #include <WiFiUdp.h>
  15.  
  16. /***************** START USER CONFIG SECTION *********************************/
  17. /***************** START USER CONFIG SECTION *********************************/
  18. /***************** START USER CONFIG SECTION *********************************/
  19. /***************** START USER CONFIG SECTION *********************************/
  20.  
  21. #define USER_SSID "XXXXXXXXXXXXXXX"
  22. #define USER_PASSWORD "XXXXXXXXXXXXXXX"
  23. #define USER_MQTT_SERVER "XXXXXXXXXXXXXXX"
  24. #define USER_MQTT_PORT 0000
  25. #define USER_MQTT_USERNAME "XXXXXXXXXX"
  26. #define USER_MQTT_PASSWORD "XXXXXXXXXXX"
  27. #define USER_MQTT_CLIENT_NAME "LightMCU" // Used to define MQTT topics, MQTT Client ID, and ArduinoOTA
  28.  
  29. #define CHIPSET WS2811
  30. #define COLOR_ORDER GRB
  31.  
  32.  
  33.  
  34. // const int pgLights = 4; //marked as D2 on the board
  35. // const int paLights = 5; //marked as D1 on the board
  36. // const int ggLights = 14; //marked as D5 on the board
  37. // const int gaLights = 12; //marked as D6 on the board
  38.  
  39. #define MAXNUMLEDS 300 // Largest String of Leds
  40. #define NUMZONES 4 // Number of separate zones
  41. #define MAXSECTIONS 2 // Of all the Zones, most number of sections
  42.  
  43. struct CRGB leds[NUMZONES][MAXNUMLEDS]; // Main LED array
  44. int numleds[NUMZONES] = {271, // Length of each string of leds
  45. 285, // Length of each string of leds
  46. 265, // Length of each string of leds
  47. 208 // Length of each string of leds
  48. }; // additional as necessary
  49.  
  50. int sections[NUMZONES][MAXSECTIONS + 2] = { // first entry is number of sections. Subsequent entries are START indecies for sections
  51. { 2, // Number of Sections in this Zone
  52. 0, 137, 272
  53. }, // Not pretty, but start led for each section and then an extra value which is (length of string + 1)
  54. { 1, // Number of Sections in this Zone
  55. 0, 286, 286
  56. }, // Not pretty, but start led for each section and then an extra value which is (length of string + 1)
  57. { 2, // Number of Sections in this Zone
  58. 0, 134, 266
  59. }, // Not pretty, but start led for each section and then an extra value which is (length of string + 1)
  60. { 1, // Number of Sections in this Zone
  61. 0, 209, 209
  62. }
  63. }; // Not pretty, but start led for each section and then an extra value which is (length of string + 1)
  64.  
  65. int firestarters[NUMZONES][2 * MAXSECTIONS] = { // Whether or not to start fire for start/end of each section
  66. {1, 0, 0, 1}, // (Start,End) per section
  67. {1, 1, 0, 0}, // (Start,End) per section
  68. {1, 0, 0, 1}, // (Start,End) per section
  69. {1, 1, 0, 0} // (Start,End) per section
  70. };
  71.  
  72. // Add necessary terms based on NUMZONES
  73. int step[NUMZONES] = { -1, -1, -1, -1}; // Used in Ripple
  74. int lightning[NUMZONES] = {1, 1, 1, 1}; // Used in add Lightning
  75.  
  76. // For fire I like to adjust size based on portion of roof. Bigger flames on A-Frame, less than 50% for flat sections as fire burns from both sides
  77. int firesize[NUMZONES] = {100, 40, 100, 40};
  78.  
  79.  
  80. // No need to edit
  81. int idex[NUMZONES] = {0}; //Used in Police
  82. int previousLED[NUMZONES] = {0} ; // Used in Single Race
  83. int center[NUMZONES] = {0}; // Used in Ripple
  84.  
  85.  
  86.  
  87. /***************** END USER CONFIG SECTION *********************************/
  88.  
  89. /***************** Color Setup *********************************/
  90.  
  91. // Inspired by Dr Zzs Color setup
  92.  
  93. ///////////////DrZzs Palettes for custom BPM effects//////////////////////////
  94. ///////////////Add any custom palettes here//////////////////////////////////
  95.  
  96. // Sam Comments: Use PaletteKnife Booklet to convert cpt-city pages to Gradient Palettes
  97. /*
  98. Using PaletteKnife
  99. Go to cpt-city here, http://soliton.vm.bytemark.co.uk/pub/cpt-city/
  100. Find a palette you like, e.g. http://soliton.vm.bytemark.co.uk/pub/cpt-city/rc/tn/purplefly.png.index.html
  101. Click PaletteKnife in your toolbar. If you get warnings, choose a different palette and try again.
  102. Copy the resulting code and paste it into your source file.
  103.  
  104. */
  105. // Brown to Orange
  106. // Gradient palette "bhw2_thanks_gp", originally from
  107. // http://soliton.vm.bytemark.co.uk/pub/cpt-city/bhw/bhw2/tn/bhw2_thanks.png.index.html
  108. // converted for FastLED with gammas (2.6, 2.2, 2.5)
  109. // Size: 36 bytes of program space.
  110.  
  111. DEFINE_GRADIENT_PALETTE( bhw2_thanks_gp ) {
  112. 0, 9, 5, 1,
  113. 48, 25, 9, 1,
  114. 76, 137, 27, 1,
  115. 96, 98, 42, 1,
  116. 124, 144, 79, 1,
  117. 153, 98, 42, 1,
  118. 178, 137, 27, 1,
  119. 211, 23, 9, 1,
  120. 255, 9, 5, 1
  121. };
  122.  
  123. DEFINE_GRADIENT_PALETTE( blue_gp ) {
  124. 0, 0, 0, 0,
  125. 64, 7, 18, 115,
  126. 128, 13, 35, 228,
  127. 192, 116, 130, 255,
  128. 255, 255, 255, 255
  129. };
  130.  
  131.  
  132. // Red to Pink
  133. // Gradient palette "bhw2_redrosey_gp", originally from
  134. // http://soliton.vm.bytemark.co.uk/pub/cpt-city/bhw/bhw2/tn/bhw2_redrosey.png.index.html
  135.  
  136. // Red to Green
  137. // Gradient palette "bhw2_xmas_gp", originally from
  138. // http://soliton.vm.bytemark.co.uk/pub/cpt-city/bhw/bhw2/tn/bhw2_xmas.png.index.html
  139.  
  140. // Purple to Yellow// Gradient palette "bhw2_xc_gp", originally from
  141. // http://soliton.vm.bytemark.co.uk/pub/cpt-city/bhw/bhw2/tn/bhw2_xc.png.index.html
  142. // converted for Fa
  143.  
  144. // Yellow to Blue
  145. // Gradient palette "bhw1_04_gp", originally from
  146. // http://soliton.vm.bytemark.co.uk/pub/cpt-city/bhw/bhw1/tn/bhw1_04.png.index.html
  147.  
  148. // Blue Yellow Blue
  149. // Gradient palette "bhw4_051_gp", originally from
  150. // http://soliton.vm.bytemark.co.uk/pub/cpt-city/bhw/bhw4/tn/bhw4_051.png.index.html
  151.  
  152. // Color of Cartoon Turkey Feathers
  153. // Gradient palette "fs2006_gp", originally from
  154. // http://soliton.vm.bytemark.co.uk/pub/cpt-city/cl/tn/fs2006.png.index.html
  155. // Gradient palette "fs2006_gp", originally from
  156. // http://soliton.vm.bytemark.co.uk/pub/cpt-city/cl/tn/fs2006.png.index.html
  157. // converted for FastLED with gammas (2.6, 2.2, 2.5)
  158. // Size: 56 bytes of program space.
  159.  
  160. DEFINE_GRADIENT_PALETTE( turkey_feathers_gp ) {
  161. 0, 0, 49, 5,
  162. 34, 0, 49, 5,
  163. 34, 79,168, 66,
  164. 62, 79,168, 66,
  165. 62, 252,168, 92,
  166. 103, 252,168, 92,
  167. 103, 234, 81, 29,
  168. 143, 234, 81, 29,
  169. 143, 222, 30, 1,
  170. 184, 222, 30, 1,
  171. 184, 90, 13, 1,
  172. 238, 90, 13, 1,
  173. 238, 210, 1, 1,
  174. 255, 210, 1, 1};
  175.  
  176. // 2 Shades of Blue
  177. // Gradient palette "blue_g2_5_gp", originally from
  178. // http://soliton.vm.bytemark.co.uk/pub/cpt-city/go2/webtwo/tn/blue-g2-5.png.index.html
  179.  
  180. // Blue White Red
  181. // Gradient palette "bhw3_41_gp", originally from
  182. // http://soliton.vm.bytemark.co.uk/pub/cpt-city/bhw/bhw3/tn/bhw3_41.png.index.html
  183.  
  184. // Green
  185. // Gradient palette "bhw2_greenman_gp", originally from
  186. // http://soliton.vm.bytemark.co.uk/pub/cpt-city/bhw/bhw2/tn/bhw2_greenman.png.index.html
  187.  
  188. // Blue and GoldGradient
  189. // Gradient palette "Pills_3_gp", originally from
  190. // http://soliton.vm.bytemark.co.uk/pub/cpt-city/ds/icons/tn/Pills-3.png.index.html
  191.  
  192. // Orange to Purple
  193. // Gradient palette "Orange_to_Purple_gp", originally from
  194. // http://soliton.vm.bytemark.co.uk/pub/cpt-city/ds/icons/tn/Orange-to-Purple.png.index.html
  195.  
  196.  
  197. CRGBPalette16 setupABStripedPalette( CRGB A, CRGB B)
  198. {
  199. return CRGBPalette16(
  200. A, A, A, A, A, A, A, A, B, B, B, B, B, B, B, B
  201. );
  202. }
  203. CRGBPalette16 setupABCStripedPalette( CRGB A, CRGB B, CRGB C)
  204. {
  205. return CRGBPalette16(
  206. A, A, A, A, A, B, B, B, B, B, C, C, C, C, C, C
  207. );
  208. }
  209.  
  210. CRGBPalette16 setupAbCStripedPalette( CRGB A, CRGB b, CRGB C)
  211. {
  212. return CRGBPalette16(
  213. A, A, A, A, A, A, A, b, b, b, C, C, C, C, C, C
  214. );
  215. }
  216.  
  217. CRGBPalette16 setupABCDStripedPalette( CRGB A, CRGB B, CRGB C, CRGB D)
  218. {
  219. return CRGBPalette16(
  220. A, A, A, A, B, B, B, B, C, C, C, C, D, D, D, D
  221. );
  222. }
  223.  
  224.  
  225.  
  226. /***************** end Arrays ****************************************/
  227.  
  228.  
  229.  
  230. /***************** GENERAL VARIABLES *************************************/
  231.  
  232. CRGBPalette16 gPal;
  233. int glitterFrequency = 100;
  234. int lightningChance = 65280;
  235. int SPARKING = 85;
  236. int COOLING = 120;
  237. bool gReverseDirection = false;
  238. uint8_t mark = 0;
  239. uint8_t gHue = 0;
  240. uint8_t startPosition = 0;
  241. uint8_t glitterChance = 250;
  242. int chaseDelay = 1000;
  243. int lastPosition = 1;
  244. int raceSpeed = 12;
  245. int BeatsPerMinute = 62;
  246. uint8_t numberOfRainbows = 7;
  247. int twinkleChance = 250;
  248. int eyeChance = 248;
  249. bool boot = true;
  250. String effect = "None";
  251. String scene = "None";
  252. bool showGlitter = false;
  253. bool showLightning = false;
  254. bool audioEffects = false;
  255. bool showLights = false;
  256. byte red1 = 255;
  257. byte green1 = 0;
  258. byte blue1 = 0;
  259. byte red2 = 0;
  260. byte green2 = 255;
  261. byte blue2 = 0;
  262. byte red3 = 0;
  263. byte green3 = 0;
  264. byte blue3 = 255;
  265. byte redG = 255;
  266. byte greenG = 255;
  267. byte blueG = 255;
  268. byte brightness = 255;
  269. char charPayload[50];
  270. int maxLEDs = 300;
  271. int locatorLED = 0;
  272. char MQTT_locatorLED[50];
  273. int locatorDelay = 1000;
  274.  
  275.  
  276. // ********************************************* WIFI Setup **********************************
  277.  
  278. const char* ssid = USER_SSID ;
  279. const char* password = USER_PASSWORD ;
  280. const char* mqtt_server = USER_MQTT_SERVER ;
  281. const int mqtt_port = USER_MQTT_PORT ;
  282. const char *mqtt_user = USER_MQTT_USERNAME ;
  283. const char *mqtt_pass = USER_MQTT_PASSWORD ;
  284. const char *mqtt_client_name = USER_MQTT_CLIENT_NAME ;
  285.  
  286.  
  287.  
  288. SimpleTimer timer;
  289.  
  290. /***************** DECLARATIONS ****************************************/
  291. WiFiClient espClient;
  292. PubSubClient client(espClient);
  293.  
  294.  
  295. ESP8266WebServer server(80);
  296. ESP8266HTTPUpdateServer httpUpdater;
  297. const char* serverIndex = "<form method='POST' action='/update' enctype='multipart/form-data'><input type='file' name='update'><input type='submit' value='Update'></form>";
  298.  
  299. /*********************** WIFI AND MQTT SETUP *****************************/
  300.  
  301. void led_wifi()
  302. {
  303.  
  304. //For the life of me I can't get OTA to work with Google Wifi or something else about my setup so I added WebUploader
  305.  
  306. /**** Need to call this from setup() just below setup_wifi below and add to loop() the following *******/
  307. /**** server.handleClient(); *******/
  308. /**** MDNS.update(); *******/
  309.  
  310. httpUpdater.setup(&server);
  311. server.begin();
  312.  
  313.  
  314. MDNS.begin(USER_MQTT_CLIENT_NAME);
  315. server.on("/", HTTP_GET, []() {
  316. server.sendHeader("Connection", "close");
  317. server.send(200, "text/html", serverIndex);
  318. });
  319. server.on("/update", HTTP_POST, []() {
  320. server.sendHeader("Connection", "close");
  321. server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
  322. ESP.restart();
  323. }, []() {
  324. HTTPUpload& upload = server.upload();
  325. if (upload.status == UPLOAD_FILE_START) {
  326. Serial.setDebugOutput(true);
  327. WiFiUDP::stopAll();
  328. Serial.printf("Update: %s\n", upload.filename.c_str());
  329. uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
  330. if (!Update.begin(maxSketchSpace)) { //start with max available size
  331. Update.printError(Serial);
  332. }
  333. } else if (upload.status == UPLOAD_FILE_WRITE) {
  334. if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
  335. Update.printError(Serial);
  336. }
  337. } else if (upload.status == UPLOAD_FILE_END) {
  338. if (Update.end(true)) { //true to set the size to the current progress
  339. Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
  340. } else {
  341. Update.printError(Serial);
  342. }
  343. Serial.setDebugOutput(false);
  344. }
  345. yield();
  346. });
  347. server.begin();
  348. MDNS.addService("http", "tcp", 80);
  349.  
  350. Serial.printf("Ready! Open http://%s.local in your browser\n", USER_MQTT_CLIENT_NAME);
  351. }
  352.  
  353. void setup_wifi()
  354. {
  355. delay(10);
  356.  
  357. // We start by connecting to a WiFi network
  358. Serial.println();
  359. Serial.print("Connecting to ");
  360. Serial.println(ssid);
  361.  
  362. WiFi.hostname(USER_MQTT_CLIENT_NAME);
  363. WiFi.begin(ssid, password);
  364.  
  365. while (WiFi.status() != WL_CONNECTED) {
  366. delay(500);
  367. Serial.print(".");
  368. }
  369.  
  370. Serial.println("");
  371. Serial.println("WiFi connected");
  372. Serial.println("IP address: ");
  373. Serial.println(WiFi.localIP());
  374. }
  375.  
  376. void reconnect()
  377. {
  378. // Loop until we're reconnected
  379. int retries = 0;
  380. while (!client.connected()) {
  381. if (retries < 150)
  382. {
  383. Serial.print("Attempting MQTT connection...");
  384. // Attempt to connect
  385. if (client.connect(mqtt_client_name, mqtt_user, mqtt_pass))
  386. {
  387. Serial.println("connected");
  388. // Once connected, publish an announcement...
  389. if (boot == true)
  390. {
  391. client.publish(USER_MQTT_CLIENT_NAME"/checkIn", "Rebooted");
  392. boot = false;
  393. }
  394. if (boot == false)
  395. {
  396. client.publish(USER_MQTT_CLIENT_NAME"/checkIn", "Reconnected");
  397. }
  398. // ... and resubscribe
  399. client.subscribe(USER_MQTT_CLIENT_NAME"/configure");
  400. client.subscribe(USER_MQTT_CLIENT_NAME"/modifier");
  401. client.subscribe(USER_MQTT_CLIENT_NAME"/effect");
  402. client.subscribe(USER_MQTT_CLIENT_NAME"/scene");
  403. client.subscribe(USER_MQTT_CLIENT_NAME"/state");
  404. client.subscribe(USER_MQTT_CLIENT_NAME"/color1");
  405. client.subscribe(USER_MQTT_CLIENT_NAME"/color2");
  406. client.subscribe(USER_MQTT_CLIENT_NAME"/color3");
  407. client.subscribe(USER_MQTT_CLIENT_NAME"/power");
  408. client.subscribe(USER_MQTT_CLIENT_NAME"/brightness");
  409. client.subscribe(USER_MQTT_CLIENT_NAME"/addEffects");
  410. client.subscribe(USER_MQTT_CLIENT_NAME"/lightningChance");
  411. client.subscribe(USER_MQTT_CLIENT_NAME"/glitterChance");
  412. client.subscribe(USER_MQTT_CLIENT_NAME"/glitterColor");
  413. }
  414. else
  415. {
  416. Serial.print("failed, rc=");
  417. Serial.print(client.state());
  418. Serial.println(" try again in 5 seconds");
  419. retries++;
  420. // Wait 5 seconds before retrying
  421. delay(5000);
  422. }
  423. }
  424. if (retries > 1500)
  425. {
  426. ESP.restart();
  427. }
  428. }
  429. }
  430.  
  431.  
  432. /************************** MQTT CALLBACK ***********************/
  433.  
  434.  
  435. void callback(char* topic, byte* payload, unsigned int length)
  436. {
  437. Serial.print("Message arrived [");
  438. String newTopic = topic;
  439. Serial.print(topic);
  440. Serial.print("] ");
  441. payload[length] = '\0';
  442. String newPayload = String((char *)payload);
  443. int intPayload = newPayload.toInt();
  444. Serial.println(newPayload);
  445. Serial.println();
  446. newPayload.toCharArray(charPayload, newPayload.length() + 1);
  447. if (newTopic == USER_MQTT_CLIENT_NAME"/modifier")
  448. {
  449. client.publish(USER_MQTT_CLIENT_NAME"/modifierState", charPayload);
  450. if (effect == "Double_Crash" || effect == "Single_Race")
  451. {
  452. raceSpeed = (intPayload / 20);
  453. }
  454. if (effect == "BPM")
  455. {
  456. BeatsPerMinute = (intPayload / 4);
  457. }
  458. if (effect == "Color_Chase" || effect == "Blocked_Colors")
  459. {
  460. chaseDelay = (intPayload * 5);
  461. if (chaseDelay < 100)
  462. {
  463. chaseDelay = 100;
  464. }
  465. }
  466. if (effect == "Color_Glitter")
  467. {
  468. glitterChance = (intPayload / 2);
  469. }
  470. if (effect == "Rainbow")
  471. {
  472. numberOfRainbows = (intPayload / 30);
  473. }
  474. if (effect == "Twinkle")
  475. {
  476. twinkleChance = map(intPayload, 0, 500, 0, 255);
  477. }
  478. if (effect == "Spooky_Eyes")
  479. {
  480. eyeChance = map(intPayload, 0, 500, 0, 255);
  481. }
  482. if (effect == "LED_Locator")
  483. {
  484. locatorDelay = map(intPayload, 0, 500, 500, 5000);
  485. }
  486. }
  487. if (newTopic == USER_MQTT_CLIENT_NAME"/addEffects")
  488. {
  489. if (newPayload == "Audio On")
  490. {
  491. audioEffects = true;
  492. client.publish(USER_MQTT_CLIENT_NAME"/audio/state", charPayload);
  493. }
  494. if (newPayload == "Audio Off")
  495. {
  496. audioEffects = false;
  497. client.publish(USER_MQTT_CLIENT_NAME"/audio/state", charPayload);
  498. }
  499. if (newPayload == "Glitter On")
  500. {
  501. showGlitter = true;
  502. client.publish(USER_MQTT_CLIENT_NAME"/glitter/state", charPayload);
  503. }
  504. if (newPayload == "Glitter Off")
  505. {
  506. showGlitter = false;
  507. client.publish(USER_MQTT_CLIENT_NAME"/glitter/state", charPayload);
  508. }
  509. if (newPayload == "Lightning On")
  510. {
  511. showLightning = true;
  512. client.publish(USER_MQTT_CLIENT_NAME"/lightning/state", "Lightning On");
  513. }
  514. if (newPayload == "Lightning Off")
  515. {
  516. showLightning = false;
  517. client.publish(USER_MQTT_CLIENT_NAME"/lightning/state", "Lightning Off");
  518. }
  519. }
  520. if (newTopic == USER_MQTT_CLIENT_NAME"/effect")
  521. {
  522. effect = newPayload;
  523. client.publish(USER_MQTT_CLIENT_NAME"/effectState", charPayload);
  524.  
  525.  
  526. for (int led_i = 0; led_i < NUMZONES; led_i++)
  527. fill_solid(leds[led_i], numleds[led_i], CRGB::Black);
  528.  
  529. if (effect == "LED_Locator")
  530. {
  531. locator_Move();
  532. }
  533.  
  534.  
  535. }
  536.  
  537. if (newTopic == USER_MQTT_CLIENT_NAME"/scene")
  538. {
  539. scene = newPayload;
  540. client.publish(USER_MQTT_CLIENT_NAME"/sceneState", charPayload);
  541.  
  542.  
  543. for (int led_i = 0; led_i < NUMZONES; led_i++)
  544. fill_solid(leds[led_i], numleds[led_i], CRGB::Black);
  545.  
  546.  
  547. }
  548.  
  549.  
  550. if (newTopic == USER_MQTT_CLIENT_NAME "/color1")
  551. {
  552. client.publish(USER_MQTT_CLIENT_NAME "/color1State", charPayload);
  553. // get the position of the first and second commas
  554. uint8_t firstIndex = newPayload.indexOf(',');
  555. uint8_t lastIndex = newPayload.lastIndexOf(',');
  556.  
  557. uint8_t rgb_red = newPayload.substring(0, firstIndex).toInt();
  558. if (rgb_red < 0 || rgb_red > 255) {
  559. return;
  560. } else {
  561. red1 = rgb_red;
  562. }
  563.  
  564. uint8_t rgb_green = newPayload.substring(firstIndex + 1, lastIndex).toInt();
  565. if (rgb_green < 0 || rgb_green > 255) {
  566. return;
  567. } else {
  568. green1 = rgb_green;
  569. }
  570.  
  571. uint8_t rgb_blue = newPayload.substring(lastIndex + 1).toInt();
  572. if (rgb_blue < 0 || rgb_blue > 255) {
  573. return;
  574. } else {
  575. blue1 = rgb_blue;
  576. }
  577. }
  578.  
  579. if (newTopic == USER_MQTT_CLIENT_NAME "/color2")
  580. {
  581. client.publish(USER_MQTT_CLIENT_NAME "/color2State", charPayload);
  582. // get the position of the first and second commas
  583. uint8_t firstIndex = newPayload.indexOf(',');
  584. uint8_t lastIndex = newPayload.lastIndexOf(',');
  585.  
  586. uint8_t rgb_red = newPayload.substring(0, firstIndex).toInt();
  587. if (rgb_red < 0 || rgb_red > 255) {
  588. return;
  589. } else {
  590. red2 = rgb_red;
  591. }
  592.  
  593. uint8_t rgb_green = newPayload.substring(firstIndex + 1, lastIndex).toInt();
  594. if (rgb_green < 0 || rgb_green > 255) {
  595. return;
  596. } else {
  597. green2 = rgb_green;
  598. }
  599.  
  600. uint8_t rgb_blue = newPayload.substring(lastIndex + 1).toInt();
  601. if (rgb_blue < 0 || rgb_blue > 255) {
  602. return;
  603. } else {
  604. blue2 = rgb_blue;
  605. }
  606. }
  607.  
  608. if (newTopic == USER_MQTT_CLIENT_NAME "/color3")
  609. {
  610. client.publish(USER_MQTT_CLIENT_NAME "/color3State", charPayload);
  611. // get the position of the first and second commas
  612. uint8_t firstIndex = newPayload.indexOf(',');
  613. uint8_t lastIndex = newPayload.lastIndexOf(',');
  614.  
  615. uint8_t rgb_red = newPayload.substring(0, firstIndex).toInt();
  616. if (rgb_red < 0 || rgb_red > 255) {
  617. return;
  618. } else {
  619. red3 = rgb_red;
  620. }
  621.  
  622. uint8_t rgb_green = newPayload.substring(firstIndex + 1, lastIndex).toInt();
  623. if (rgb_green < 0 || rgb_green > 255) {
  624. return;
  625. } else {
  626. green3 = rgb_green;
  627. }
  628.  
  629. uint8_t rgb_blue = newPayload.substring(lastIndex + 1).toInt();
  630. if (rgb_blue < 0 || rgb_blue > 255) {
  631. return;
  632. } else {
  633. blue3 = rgb_blue;
  634. }
  635. }
  636.  
  637. if (newTopic == USER_MQTT_CLIENT_NAME "/glitterColor")
  638. {
  639. client.publish(USER_MQTT_CLIENT_NAME "/glitterColorState", charPayload);
  640. // get the position of the first and second commas
  641. uint8_t firstIndex = newPayload.indexOf(',');
  642. uint8_t lastIndex = newPayload.lastIndexOf(',');
  643.  
  644. uint8_t rgb_red = newPayload.substring(0, firstIndex).toInt();
  645. if (rgb_red < 0 || rgb_red > 255) {
  646. return;
  647. } else {
  648. redG = rgb_red;
  649. }
  650.  
  651. uint8_t rgb_green = newPayload.substring(firstIndex + 1, lastIndex).toInt();
  652. if (rgb_green < 0 || rgb_green > 255) {
  653. return;
  654. } else {
  655. greenG = rgb_green;
  656. }
  657.  
  658. uint8_t rgb_blue = newPayload.substring(lastIndex + 1).toInt();
  659. if (rgb_blue < 0 || rgb_blue > 255) {
  660. return;
  661. } else {
  662. blueG = rgb_blue;
  663. }
  664. }
  665.  
  666. if (newTopic == USER_MQTT_CLIENT_NAME"/glitterChance")
  667. {
  668. client.publish(USER_MQTT_CLIENT_NAME "/glitterChanceState", charPayload);
  669. glitterFrequency = intPayload;
  670. }
  671.  
  672. if (newTopic == USER_MQTT_CLIENT_NAME"/lightningChance")
  673. {
  674. client.publish(USER_MQTT_CLIENT_NAME "/lightningChanceState", charPayload);
  675. lightningChance = (65535 - intPayload);
  676. }
  677.  
  678. if (newTopic == USER_MQTT_CLIENT_NAME"/brightness")
  679. {
  680. client.publish(USER_MQTT_CLIENT_NAME "/brightnessState", charPayload);
  681. brightness = intPayload;
  682. }
  683.  
  684. if (newTopic == USER_MQTT_CLIENT_NAME"/power")
  685. {
  686. client.publish(USER_MQTT_CLIENT_NAME "/powerState", charPayload);
  687. if (newPayload == "ON")
  688. {
  689. showLights = true;
  690. }
  691. if (newPayload == "OFF")
  692. {
  693. showLights = false;
  694. }
  695. }
  696. }
  697.  
  698. /*********************** End WIFI MQTT *****************************/
  699.  
  700.  
  701.  
  702.  
  703. /***************** SYSTEM FUNCTIONS *************************************/
  704.  
  705. // Don't think this is issue. WORRIED is startPosition globally conflicted if mult effects chosen
  706. void chase()
  707. {
  708. if (startPosition == 5)
  709. {
  710. startPosition = 0;
  711. }
  712. else
  713. {
  714. startPosition++;
  715. }
  716. timer.setTimeout(chaseDelay, chase);
  717. }
  718.  
  719. void checkIn()
  720. {
  721. client.publish(USER_MQTT_CLIENT_NAME"/checkIn", "OK");
  722. timer.setTimeout(120000, checkIn);
  723. }
  724.  
  725.  
  726. int antipodal_index(int i, int NUM_LEDS) {
  727. int TOP_INDEX = int(NUM_LEDS / 2);
  728. int iN = i + TOP_INDEX;
  729. if (i >= TOP_INDEX) {
  730. iN = ( i + TOP_INDEX ) % NUM_LEDS;
  731. }
  732. return iN;
  733. }
  734.  
  735. void choosePattern()
  736. {
  737. if (showLights == true)
  738. {
  739. if (effect != "Scene") {
  740. if (effect == "Color_Chase")
  741. {
  742. for (int led_i = 0; led_i < NUMZONES; led_i++)
  743. ledRGB(led_i);
  744.  
  745. }
  746. if (effect == "Color_Glitter")
  747. {
  748. for (int led_i = 0; led_i < NUMZONES; led_i++)
  749. ColorGlitter(glitterChance, led_i);
  750. }
  751. if (effect == "Single_Race")
  752. {
  753. for (int led_i = 0; led_i < NUMZONES; led_i++)
  754. SingleRace(led_i);
  755. }
  756. if (effect == "Double_Crash")
  757. {
  758. for (int led_i = 0; led_i < NUMZONES; led_i++)
  759. Crash(led_i);
  760. }
  761. if (effect == "Rainbow")
  762. {
  763. for (int led_i = 0; led_i < NUMZONES; led_i++)
  764. Rainbow(led_i);
  765. }
  766. if (effect == "Blocked_Colors")
  767. {
  768. for (int led_i = 0; led_i < NUMZONES; led_i++)
  769. Blocked(led_i);
  770.  
  771. }
  772. if (effect == "BPM")
  773. {
  774. for (int led_i = 0; led_i < NUMZONES; led_i++)
  775. BPM(led_i);
  776. }
  777. if (effect == "Twinkle")
  778. {
  779. for (int led_i = 0; led_i < NUMZONES; led_i++)
  780. Twinkle(led_i);
  781. }
  782. if (effect == "Fill_Solid")
  783. {
  784. for (int led_i = 0; led_i < NUMZONES; led_i++)
  785. Solid(led_i);
  786. }
  787. if (effect == "Spooky_Eyes")
  788. {
  789. for (int led_i = 0; led_i < NUMZONES; led_i++)
  790. Eyes(led_i, CRGB(red1, green1, blue1));
  791. }
  792. if (effect == "LED_Locator")
  793. {
  794. for (int led_i = 0; led_i < NUMZONES; led_i++)
  795. Locator(led_i);
  796. }
  797. if (effect == "Ripple")
  798. {
  799. for (int led_i = 0; led_i < NUMZONES; led_i++)
  800. Ripple(led_i);
  801. }
  802. if (effect == "Fire")
  803. {
  804. for (int led_i = 0; led_i < NUMZONES; led_i++)
  805. Fire(led_i);
  806. }
  807. if (effect == "Police")
  808. {
  809. for (int led_i = 0; led_i < NUMZONES; led_i++)
  810. Police(led_i);
  811.  
  812. }
  813. // ****************** SCENES ************************
  814.  
  815.  
  816. /* Duke_Blocked
  817. A-Frame: Blocked
  818. Gutters: Solid
  819. */
  820.  
  821. /* Duke Fire
  822. A-Frame: Fire
  823. Gutters: Solid
  824.  
  825. */
  826. /* Halloween
  827. A-Frame: Fire
  828. Gutters: Eyes + Lightning
  829. */
  830.  
  831. /* CandyCane
  832. A-Frame: Fire
  833. Gutters: Eyes
  834. */
  835.  
  836.  
  837.  
  838.  
  839.  
  840. // Color_Chase - Dots Chasing each other
  841. // Color_Glitter – random Fill in Strips
  842. // Single_Race - Kind of tennis. Color streak/fade to one side, bounce back with another color
  843. // Double_Crash – Two Colors from middle and ends, bounce at ends and collide in middle
  844. // Rainbow – Size adjustable
  845. // Blocked_Colors – Banded and move in chunks not gradual
  846. // BPM – Seesaw back and forth (some sort of gradient involved as well within color
  847.  
  848. } else {
  849. if (scene == "Halloween")
  850. {
  851. Fire(0, HeatColors_p);
  852. Fire(2, HeatColors_p);
  853. Eyes(1, CRGB(200, 20, 20));
  854. Eyes(3, CRGB(200, 20, 20));
  855. }
  856. if (scene == "Duke_Blocked")
  857. {
  858. fill_solid(leds[1], numleds[1], CRGB(0, 0, 151));
  859. fill_solid(leds[3], numleds[3], CRGB(0, 0, 151));
  860. Crawl(0, setupABStripedPalette(CRGB::Blue, CRGB::White));
  861. Crawl(2, setupABStripedPalette(CRGB::Blue, CRGB::White));
  862. }
  863. if (scene == "Duke_Fire") {
  864. // for (int led_i = 0;led_i<NUMZONES;led_i++)
  865. // Police(led_i,64,96);
  866. Fire(0, blue_gp );
  867. Fire(2, blue_gp );
  868. Blocked(1,CRGB::Blue,CRGB::White);
  869. Blocked(3,CRGB::Blue,CRGB::White);
  870.  
  871.  
  872.  
  873. }
  874.  
  875. }
  876. }
  877. // **************************** New Scenes/Prototypes **********************************
  878.  
  879. if (scene == "Test") {
  880. // for (int led_i = 0;led_i<NUMZONES;led_i++)
  881. // Police(led_i,64,96);
  882. Crawl(0,setupABCDStripedPalette(CRGB(212,40,36), CRGB(220,120,12),CRGB(255,231,28), CRGB(88,115,36)));
  883. Crawl(1,setupABCDStripedPalette(CRGB(212,40,36), CRGB(220,120,12),CRGB(255,231,28), CRGB(88,115,36)));
  884. Crawl(2,setupABCDStripedPalette(CRGB(212,40,36), CRGB(220,120,12),CRGB(255,231,28), CRGB(88,115,36)));
  885. Crawl(3,setupABCDStripedPalette(CRGB(212,40,36), CRGB(220,120,12),CRGB(255,231,28), CRGB(88,115,36)));
  886.  
  887.  
  888.  
  889. }
  890.  
  891. // **************************** **********************************
  892.  
  893.  
  894. if (showLights == false)
  895. {
  896. for (int led_i = 0; led_i < NUMZONES; led_i++)
  897. fill_solid(leds[led_i], numleds[led_i], CRGB::Black);
  898. }
  899. if (showGlitter == true)
  900. {
  901. for (int led_i = 0; led_i < NUMZONES; led_i++)
  902. addGlitter(led_i);
  903. }
  904. if (showLightning == true)
  905. {
  906. for (int led_i = 0; led_i < NUMZONES; led_i++)
  907. addLightning(led_i);
  908. }
  909. }
  910.  
  911. void setup() {
  912. Serial.begin(115200);
  913. // calculateMax();
  914. FastLED.addLeds<CHIPSET, 5, COLOR_ORDER>(leds[0], numleds[0]);
  915. FastLED.addLeds<CHIPSET, 4, COLOR_ORDER>(leds[1], numleds[1]);
  916. FastLED.addLeds<CHIPSET, 14, COLOR_ORDER>(leds[2], numleds[2]);
  917. FastLED.addLeds<CHIPSET, 12, COLOR_ORDER>(leds[3], numleds[3]);
  918. FastLED.setDither( 0 );
  919. WiFi.setSleepMode(WIFI_NONE_SLEEP);
  920. WiFi.mode(WIFI_STA);
  921. setup_wifi();
  922. led_wifi();
  923. client.setServer(mqtt_server, mqtt_port);
  924. client.setCallback(callback);
  925. // ArduinoOTA.setHostname(USER_MQTT_CLIENT_NAME);
  926. // ArduinoOTA.begin();
  927.  
  928.  
  929. gPal = HeatColors_p;
  930.  
  931. timer.setTimeout(10000, chase);
  932. timer.setTimeout(120000, checkIn);
  933.  
  934. }
  935.  
  936. void loop()
  937. {
  938. server.handleClient();
  939. if (!client.connected())
  940. {
  941. reconnect();
  942. }
  943. client.loop();
  944.  
  945. if (showLights == false)
  946. {
  947. // ArduinoOTA.handle();
  948. server.handleClient();
  949. MDNS.update();
  950. }
  951. timer.run();
  952. EVERY_N_MILLISECONDS( 20 ) {
  953. gHue++;
  954. }
  955. choosePattern();
  956.  
  957. for (int led_i = 0; led_i < NUMZONES; led_i++)
  958. FastLED[led_i].showLeds(brightness);
  959. }
  960.  
  961.  
  962. // Pattern Implementation Functions formerly ... _firstZone()...
  963.  
  964. void locator_Move()
  965. {
  966.  
  967. if (effect == "LED_Locator")
  968. {
  969. if (locatorLED <= maxLEDs)
  970. {
  971. String temp_str = String(locatorLED);
  972. temp_str.toCharArray(MQTT_locatorLED, temp_str.length() + 1);
  973. client.publish(USER_MQTT_CLIENT_NAME"/locator", MQTT_locatorLED);
  974. locatorLED++;
  975. }
  976. else
  977. {
  978. locatorLED = 0;
  979. }
  980. timer.setTimeout(locatorDelay, locator_Move);
  981. }
  982. }
  983.  
  984. void addLightning(int led_i)
  985. {
  986. {
  987. fadeToBlackBy(leds[led_i], numleds[led_i], 1);
  988. unsigned int chance = random16();
  989. char chancestring[50];
  990. char chancestring2[50];
  991. itoa(chance, chancestring, 10);
  992. itoa(-1*lightningChance, chancestring2, 10);
  993. // chancestring = chancestring+"-"+chancestring2;
  994. if ( chance > lightningChance)
  995. {
  996. fill_solid(leds[led_i], numleds[led_i], CRGB::White);
  997. lightning[led_i] = 20;
  998. //
  999. client.publish("lightningtest",chancestring );
  1000. client.publish("lightningtest",chancestring2 );
  1001.  
  1002. if(audioEffects == true)
  1003. {
  1004. int thunder = random8();
  1005. if( thunder > 128)
  1006. {
  1007. client.publish("Audio","1");
  1008. }
  1009. if( thunder < 127)
  1010. {
  1011. client.publish("Audio","1");
  1012. }
  1013. }
  1014.  
  1015. //
  1016. }
  1017. if (lightning[led_i] != 1)
  1018. {
  1019. if (lightning[led_i] > 15)
  1020. {
  1021. fadeToBlackBy(leds[led_i], numleds[led_i], 150);
  1022. lightning[led_i]--;
  1023. }
  1024. if (lightning[led_i] == 15)
  1025. {
  1026. fill_solid(leds[led_i], numleds[led_i], CRGB::White);
  1027. lightning[led_i]--;
  1028. }
  1029. if (lightning[led_i] > 5 && lightning[led_i] < 15)
  1030. {
  1031. fadeToBlackBy(leds[led_i], numleds[led_i], 150);
  1032. lightning[led_i]--;
  1033. }
  1034. if (lightning[led_i] == 5)
  1035. {
  1036. fill_solid(leds[led_i], numleds[led_i], CRGB::White);
  1037. lightning[led_i]--;
  1038. lightning[led_i]--;
  1039. }
  1040. if (lightning[led_i] > 0 && lightning[led_i] < 5)
  1041. {
  1042. fadeToBlackBy(leds[led_i], numleds[led_i], 150);
  1043. lightning[led_i]--;
  1044. }
  1045. }
  1046. }
  1047. }
  1048.  
  1049.  
  1050. void addGlitter(int led_i)
  1051. {
  1052. {
  1053. if ( random8() < glitterFrequency)
  1054. {
  1055. leds[led_i][ random16(numleds[led_i]) ] = CRGB(redG, greenG, blueG);
  1056. }
  1057. }
  1058. }
  1059.  
  1060.  
  1061. void ledRGB( int led_i)
  1062. {
  1063. if (startPosition < 6)
  1064. {
  1065. fadeToBlackBy( leds[led_i], numleds[led_i], 80);
  1066. for (int pixel = startPosition + 1; pixel < numleds[led_i]; pixel += 6)
  1067. {
  1068. if (pixel <= numleds[led_i])
  1069. {
  1070. leds[led_i][pixel] = CRGB(red1, green1, blue1);
  1071. }
  1072. }
  1073. for (int pixel = startPosition + 3; pixel < numleds[led_i]; pixel += 6)
  1074. {
  1075. if (pixel <= numleds[led_i])
  1076. {
  1077. leds[led_i][pixel] = CRGB(red2, green2, blue2);
  1078. }
  1079. }
  1080. for (int pixel = startPosition + 5; pixel < numleds[led_i]; pixel += 6)
  1081. {
  1082. if (pixel <= numleds[led_i])
  1083. {
  1084. leds[led_i][pixel] = CRGB(red3, green3, blue3);
  1085. }
  1086. }
  1087. }
  1088. if (startPosition == 1)
  1089. {
  1090. leds[led_i][0] = CRGB(red3, green3, blue3);
  1091. }
  1092. if (startPosition == 2)
  1093. {
  1094. leds[led_i][1] = CRGB(red3, green3, blue3);
  1095. }
  1096. if (startPosition == 3)
  1097. {
  1098. leds[led_i][2] = CRGB(red3, green3, blue3);
  1099. leds[led_i][0] = CRGB(red2, green2, blue2);
  1100. }
  1101. if (startPosition == 4)
  1102. {
  1103. leds[led_i][3] = CRGB(red3, green3, blue3);
  1104. leds[led_i][1] = CRGB(red2, green2, blue2);
  1105. }
  1106. if (startPosition == 5)
  1107. {
  1108. leds[led_i][4] = CRGB(red3, green3, blue3);
  1109. leds[led_i][2] = CRGB(red2, green2, blue2);
  1110. leds[led_i][0] = CRGB(red1, green1, blue1);
  1111. }
  1112. }
  1113.  
  1114. // ********************************** Blocked Overloaded ***************************************************
  1115.  
  1116. void Blocked(int led_i)
  1117. {
  1118. fadeToBlackBy( leds[led_i], numleds[led_i], 2);
  1119. for ( int mark = 0; mark < numleds[led_i]; mark += 30)
  1120. {
  1121. for ( int i = 0; i < 9; i++)
  1122. {
  1123. if (startPosition == 0 || startPosition == 1)
  1124. {
  1125. if (i + mark <= numleds[led_i])
  1126. {
  1127. leds[led_i][i + mark] = CRGB(red1, green1, blue1);
  1128. }
  1129. if (i + 10 + mark <= numleds[led_i])
  1130. {
  1131. leds[led_i][i + 10 + mark] = CRGB(red2, green2, blue2);
  1132. }
  1133. if (i + 20 + mark <= numleds[led_i])
  1134. {
  1135. leds[led_i][i + 20 + mark] = CRGB(red3, green3, blue3);
  1136. }
  1137. }
  1138. if (startPosition == 2 || startPosition == 3)
  1139. {
  1140. if (i + mark <= numleds[led_i])
  1141. {
  1142. leds[led_i][i + mark] = CRGB(red2, green2, blue2);
  1143. }
  1144. if (i + 10 + mark <= numleds[led_i])
  1145. {
  1146. leds[led_i][i + 10 + mark] = CRGB(red3, green3, blue3);
  1147. }
  1148. if (i + 20 + mark <= numleds[led_i])
  1149. {
  1150. leds[led_i][i + 20 + mark] = CRGB(red1, green1, blue1);
  1151. }
  1152. }
  1153. if (startPosition == 4 || startPosition == 5)
  1154. {
  1155. if (i + mark <= numleds[led_i])
  1156. {
  1157. leds[led_i][i + mark] = CRGB(red3, green3, blue3);
  1158. }
  1159. if (i + 10 + mark <= numleds[led_i])
  1160. {
  1161. leds[led_i][i + 10 + mark] = CRGB(red1, green1, blue1);
  1162. }
  1163. if (i + 20 + mark <= numleds[led_i])
  1164. {
  1165. leds[led_i][i + 20 + mark] = CRGB(red2, green2, blue2);
  1166. }
  1167. }
  1168. }
  1169. }
  1170. }
  1171.  
  1172. void Blocked(int led_i, CRGB color1, CRGB color2)
  1173. {
  1174. fadeToBlackBy( leds[led_i], numleds[led_i], 2);
  1175. for ( int mark = 0; mark < numleds[led_i]; mark += 20)
  1176. {
  1177. for ( int i = 0; i < 9; i++)
  1178. {
  1179. if (startPosition == 0 || startPosition == 1 || startPosition == 2)
  1180. {
  1181. if (i + mark <= numleds[led_i])
  1182. {
  1183. leds[led_i][i + mark] = color1;
  1184. }
  1185. if (i + 10 + mark <= numleds[led_i])
  1186. {
  1187. leds[led_i][i + 10 + mark] = color2;
  1188. }
  1189. }
  1190. if (startPosition == 3 || startPosition == 4 || startPosition == 5)
  1191. {
  1192. if (i + mark <= numleds[led_i])
  1193. {
  1194. leds[led_i][i + mark] = color2;
  1195. }
  1196. if (i + 10 + mark <= numleds[led_i])
  1197. {
  1198. leds[led_i][i + 10 + mark] = color1;
  1199. }
  1200. }
  1201. }
  1202. }
  1203. }
  1204.  
  1205. // ********************************** Blocked Overloaded End ***************************************************
  1206.  
  1207.  
  1208.  
  1209. void Rainbow(int led_i)
  1210. {
  1211. fill_rainbow( leds[led_i], numleds[led_i], gHue, numberOfRainbows);
  1212. }
  1213.  
  1214.  
  1215. // ********************************** Crash Overloaded ***************************************************
  1216. void Crash(int led_i)
  1217. {
  1218. fadeToBlackBy( leds[led_i], numleds[led_i], 15);
  1219. for (int led_j = 1; led_j <= sections[led_i][0]; led_j++) {
  1220. int pos_SECTION = beatsin16( raceSpeed, 0, (sections[led_i][led_j + 1] - 1 - sections[led_i][led_j]) );
  1221. leds[led_i][sections[led_i][led_j] + pos_SECTION] = CRGB(red1, green1, blue1);
  1222. leds[led_i][sections[led_i][led_j + 1] - 1 - pos_SECTION] = CRGB(red2, green2, blue2);
  1223. }
  1224. }
  1225. void Crash(int led_i, CRGB color1, CRGB color2)
  1226. {
  1227. fadeToBlackBy( leds[led_i], numleds[led_i], 15);
  1228. for (int led_j = 1; led_j <= sections[led_i][0]; led_j++) {
  1229. int pos_SECTION = beatsin16( raceSpeed, 0, (sections[led_i][led_j + 1] - 1 - sections[led_i][led_j]) );
  1230. leds[led_i][sections[led_i][led_j] + pos_SECTION] = color1;
  1231. leds[led_i][sections[led_i][led_j + 1] - 1 - pos_SECTION] = color2;
  1232. }
  1233. }
  1234. // ********************************** Crash Overloaded ended ***************************************************
  1235. void Eyes(int led_i, CRGB color)
  1236. {
  1237. unsigned int chance = random8();
  1238. if ( chance > eyeChance)
  1239. {
  1240. unsigned int eye = random16(numleds[led_i]);
  1241. leds[led_i][eye] = color;
  1242. leds[led_i][eye - 4] = color;
  1243. }
  1244. if ( chance > 39)
  1245. {
  1246. fadeToBlackBy( leds[ led_i], numleds[led_i], 10);
  1247. }
  1248. }
  1249.  
  1250.  
  1251. // ********************************** Fire Overloaded ***************************************************
  1252.  
  1253. void Fire(int led_i)
  1254. {
  1255. for (int led_j = 1; led_j <= sections[led_i][0]; led_j++) {
  1256. // int sections[NUMZONES][MAXSECTIONS+2] = { {2,0,137,272}, {1,0,286,286},{2,0,134,266},{1,0,209,209} };
  1257. // int firestarters[NUMZONES][2*MAXSECTIONS]= { {1,0,0,1,0,0}, {1,1,0,0,0,0},{1,0,0,1,0,0},{1,1,0,0,0,0} };
  1258.  
  1259. int numfireleds = sections[led_i][led_j + 1] - sections[led_i][led_j];
  1260.  
  1261. static byte heat[NUMZONES][MAXNUMLEDS];
  1262. for ( int i = sections[led_i][led_j]; i < sections[led_i][led_j + 1]; i++)
  1263. {
  1264. heat[led_i][i] = qsub8( heat[led_i][i], random8(0, ((COOLING * 10) / numfireleds) + 2));
  1265. }
  1266.  
  1267. for ( int k = sections[led_i][led_j + 1] - 1; k >= sections[led_i][led_j] + 2; k--)
  1268. {
  1269. heat[led_i][k] = (heat[led_i][k - 1] + heat[led_i][k - 2] + heat[led_i][k - 2] ) / 3;
  1270. }
  1271. if ( random8() < SPARKING )
  1272. {
  1273. int y = random8(7);
  1274. heat[led_i][sections[led_i][led_j] + y] = qadd8( heat[led_i][sections[led_i][led_j] + y], random8(160, 255) );
  1275. }
  1276. for ( int j = 0; j < numfireleds; j++)
  1277. {
  1278. byte colorindex = scale8( heat[led_i][sections[led_i][led_j] + j], 240);
  1279. CRGB color = ColorFromPalette( gPal, colorindex);
  1280. int pixelnumber;
  1281. if ( gReverseDirection ) {
  1282. pixelnumber = (numfireleds - 1) - j;
  1283. } else {
  1284. pixelnumber = j;
  1285. }
  1286. int thisFlame = ((pixelnumber * firesize[led_i]) / 100);
  1287. // if(thisFlame <= numfireleds )
  1288.  
  1289. if (firestarters[led_i][2 * led_j - 2] == 1)
  1290. {
  1291. leds[led_i][sections[led_i][led_j] + thisFlame] = color;
  1292. }
  1293.  
  1294. if (firestarters[led_i][2 * led_j - 1] == 1)
  1295. {
  1296. leds[led_i][sections[led_i][led_j + 1] - 1 - thisFlame] = color;
  1297. }
  1298. }
  1299. }
  1300. }
  1301.  
  1302.  
  1303. void Fire(int led_i, CRGBPalette16 flames)
  1304. {
  1305. for (int led_j = 1; led_j <= sections[led_i][0]; led_j++) {
  1306. // int sections[NUMZONES][MAXSECTIONS+2] = { {2,0,137,272}, {1,0,286,286},{2,0,134,266},{1,0,209,209} };
  1307. // int firestarters[NUMZONES][2*MAXSECTIONS]= { {1,0,0,1,0,0}, {1,1,0,0,0,0},{1,0,0,1,0,0},{1,1,0,0,0,0} };
  1308.  
  1309. int numfireleds = sections[led_i][led_j + 1] - sections[led_i][led_j];
  1310.  
  1311. static byte heat[NUMZONES][MAXNUMLEDS];
  1312. for ( int i = sections[led_i][led_j]; i < sections[led_i][led_j + 1]; i++)
  1313. {
  1314. heat[led_i][i] = qsub8( heat[led_i][i], random8(0, ((COOLING * 10) / numfireleds) + 2));
  1315. }
  1316.  
  1317. for ( int k = sections[led_i][led_j + 1] - 1; k >= sections[led_i][led_j] + 2; k--)
  1318. {
  1319. heat[led_i][k] = (heat[led_i][k - 1] + heat[led_i][k - 2] + heat[led_i][k - 2] ) / 3;
  1320. }
  1321. if ( random8() < SPARKING )
  1322. {
  1323. int y = random8(7);
  1324. heat[led_i][sections[led_i][led_j] + y] = qadd8( heat[led_i][sections[led_i][led_j] + y], random8(160, 255) );
  1325. }
  1326. for ( int j = 0; j < numfireleds; j++)
  1327. {
  1328. byte colorindex = scale8( heat[led_i][sections[led_i][led_j] + j], 240);
  1329. CRGB color = ColorFromPalette( flames, colorindex);
  1330. int pixelnumber;
  1331. if ( gReverseDirection ) {
  1332. pixelnumber = (numfireleds - 1) - j;
  1333. } else {
  1334. pixelnumber = j;
  1335. }
  1336. int thisFlame = ((pixelnumber * firesize[led_i]) / 100);
  1337. // if(thisFlame <= numfireleds )
  1338.  
  1339. if (firestarters[led_i][2 * led_j - 2] == 1)
  1340. {
  1341. leds[led_i][sections[led_i][led_j] + thisFlame] = color;
  1342. }
  1343.  
  1344. if (firestarters[led_i][2 * led_j - 1] == 1)
  1345. {
  1346. leds[led_i][sections[led_i][led_j + 1] - 1 - thisFlame] = color;
  1347. }
  1348. }
  1349. }
  1350. }
  1351.  
  1352. // ********************************** Fire Overloaded ended ***************************************************
  1353.  
  1354.  
  1355. void ColorGlitter( fract8 chanceOfGlitter, int led_i)
  1356. {
  1357. fadeToBlackBy( leds[led_i], numleds[led_i], 2);
  1358. if (startPosition == 0)
  1359. {
  1360. if ( random8() < chanceOfGlitter)
  1361. {
  1362. leds[led_i][ random16(numleds[led_i]) ] += CRGB::Red;
  1363. }
  1364. }
  1365. if (startPosition == 1)
  1366. {
  1367. if ( random8() < chanceOfGlitter)
  1368. {
  1369. leds[led_i][ random16(numleds[led_i]) ] += CRGB::Orange;
  1370. }
  1371. }
  1372. if (startPosition == 2)
  1373. {
  1374. if ( random8() < chanceOfGlitter)
  1375. {
  1376. leds[led_i][ random16(numleds[led_i]) ] += CRGB::Yellow;
  1377. }
  1378. }
  1379. if (startPosition == 3)
  1380. {
  1381. if ( random8() < chanceOfGlitter)
  1382. {
  1383. leds[led_i][ random16(numleds[led_i]) ] += CRGB::Green;
  1384. }
  1385. }
  1386. if (startPosition == 4)
  1387. {
  1388. if ( random8() < chanceOfGlitter)
  1389. {
  1390. leds[led_i][ random16(numleds[led_i]) ] += CRGB::Blue;
  1391. }
  1392. }
  1393. if (startPosition == 5)
  1394. {
  1395. if ( random8() < chanceOfGlitter)
  1396. {
  1397. leds[led_i][ random16(numleds[led_i]) ] += CRGB::Violet;
  1398. }
  1399. }
  1400. }
  1401. // ********** BPM Overloaded ********************************************
  1402. void BPM(int led_i)
  1403. {
  1404. if ((red1 * 2) > (green1 + blue1))
  1405. {
  1406. CRGBPalette16 palette_Zone = LavaColors_p;
  1407. uint8_t beat = beatsin16( BeatsPerMinute, 64, 255);
  1408. for ( int i = 0; i < numleds[led_i]; i++)
  1409. {
  1410. leds[led_i][i] = ColorFromPalette(palette_Zone, gHue + (i * 2), beat - gHue + (i * 10));
  1411. }
  1412. }
  1413. if ((green1 * 2) > (red1 + blue1))
  1414. {
  1415. CRGBPalette16 palette_Zone = ForestColors_p;
  1416. uint8_t beat = beatsin16( BeatsPerMinute, 64, 255);
  1417. for ( int i = 0; i < numleds[led_i]; i++)
  1418. {
  1419. leds[led_i][i] = ColorFromPalette(palette_Zone, gHue + (i * 2), beat - gHue + (i * 10));
  1420. }
  1421. }
  1422. if ((blue1 * 2) > (green1 + red1))
  1423. {
  1424. CRGBPalette16 palette_Zone = CloudColors_p;
  1425. uint8_t beat = beatsin16( BeatsPerMinute, 64, 255);
  1426. for ( int i = 0; i < numleds[led_i]; i++)
  1427. {
  1428. leds[led_i][i] = ColorFromPalette(palette_Zone, gHue + (i * 2), beat - gHue + (i * 10));
  1429. }
  1430. }
  1431. if ((blue1 * 2) == (green1 + red1))
  1432. {
  1433. CRGBPalette16 palette_Zone = PartyColors_p;
  1434. uint8_t beat = beatsin16( BeatsPerMinute, 64, 255);
  1435. for ( int i = 0; i < numleds[led_i]; i++)
  1436. {
  1437. leds[led_i][i] = ColorFromPalette(palette_Zone, gHue + (i * 2), beat - gHue + (i * 10));
  1438. }
  1439. }
  1440. }
  1441. void BPM (int led_i, CRGBPalette16 palette)
  1442. {
  1443. uint8_t BeatsPerMinute = 62;
  1444. uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
  1445. for ( int i = 0; i < numleds[led_i]; i++) { //9948
  1446. leds[led_i][i] = ColorFromPalette(palette, gHue + (i * 2), beat - gHue + (i * 10));
  1447. }
  1448. }
  1449.  
  1450.  
  1451. // ********** BPM Overloaded End ********************************************
  1452.  
  1453. void Twinkle(int led_i)
  1454. {
  1455. fadeToBlackBy( leds[led_i], numleds[led_i], 80);
  1456. for ( int i = 0; i < numleds[led_i] - 5; i += 5)
  1457. {
  1458. if (i <= numleds[led_i])
  1459. {
  1460. if (random8() > twinkleChance)
  1461. {
  1462. leds[led_i][i] = CRGB( red1, green1, blue1);
  1463. }
  1464. else
  1465. {
  1466. leds[led_i][i] = CRGB( 150, 100, 40);
  1467. }
  1468. }
  1469. }
  1470. }
  1471.  
  1472. // ********** Solid Overloaded ********************************************
  1473. void Solid(int led_i)
  1474. {
  1475. fill_solid(leds[led_i], numleds[led_i], CRGB(red1, green1, blue1));
  1476. }
  1477. void Solid(int led_i, CRGB color)
  1478. {
  1479. fill_solid(leds[led_i], numleds[led_i], color);
  1480. }
  1481.  
  1482. // ********** Solid Overloaded ********************************************
  1483.  
  1484.  
  1485. void Locator(int led_i)
  1486. {
  1487. if (locatorLED <= numleds[led_i])
  1488. {
  1489. fill_solid(leds[led_i], numleds[led_i], CRGB::Black);
  1490. leds[led_i][locatorLED] = CRGB(red1, green1, blue1);
  1491. }
  1492. }
  1493.  
  1494. // ********** SingleRace Overloaded ********************************************
  1495. void SingleRace(int led_i)
  1496. {
  1497. fadeToBlackBy( leds[led_i], numleds[led_i], 1);
  1498. int pos = beatsin16( raceSpeed, 0, numleds[led_i]);
  1499. if (pos > previousLED[led_i])
  1500. {
  1501. leds[led_i][pos] = CRGB(red1, green1, blue1);
  1502. previousLED[led_i] = pos;
  1503. }
  1504. if (pos < previousLED[led_i])
  1505. {
  1506. leds[led_i][pos] = CRGB(red2, green2, blue2);
  1507. previousLED[led_i] = pos;
  1508. }
  1509. }
  1510.  
  1511. void SingleRace(int led_i, CRGB color1, CRGB color2)
  1512. {
  1513. fadeToBlackBy( leds[led_i], numleds[led_i], 1);
  1514. int pos = beatsin16( raceSpeed, 0, numleds[led_i]);
  1515. if (pos > previousLED[led_i])
  1516. {
  1517. leds[led_i][pos] = color1;
  1518. previousLED[led_i] = pos;
  1519. }
  1520. if (pos < previousLED[led_i])
  1521. {
  1522. leds[led_i][pos] = color2;
  1523. previousLED[led_i] = pos;
  1524. }
  1525. }
  1526. // ********** SingleRace Overloaded Ended ********************************************
  1527.  
  1528. // ********** Ripple Overloaded Ended ********************************************
  1529. void Ripple(int led_i)
  1530. {
  1531. for (int i = 0; i < numleds[led_i]; i++)
  1532. {
  1533. leds[led_i][i] = CRGB((red2 / 75), (green2 / 75), (blue2 / 75));
  1534. }
  1535. switch (step[led_i])
  1536. {
  1537. case -1:
  1538. center[led_i] = random16(numleds[led_i]);
  1539. step[led_i] = 0;
  1540. break;
  1541. case 0:
  1542. leds[led_i][center[led_i]] = CRGB(red1, green1, blue1);
  1543. step[led_i] ++;
  1544. break;
  1545. case 12:
  1546. step[led_i] = -1;
  1547. break;
  1548. default: // Middle of the ripples.
  1549. leds[led_i][(center[led_i] + step[led_i] + numleds[led_i]) % numleds[led_i]] += CRGB((red1 / (step[led_i] * 5)), (green1 / (step[led_i] * 5)), (blue1 / (step[led_i] * 5)));
  1550. leds[led_i][(center[led_i] - step[led_i] + numleds[led_i]) % numleds[led_i]] += CRGB((red1 / (step[led_i] * 5)), (green1 / (step[led_i] * 5)), (blue1 / (step[led_i] * 5)));
  1551. step[led_i] ++;
  1552. break;
  1553. }
  1554. }
  1555.  
  1556. void Ripple(int led_i, CRGB color1, CRGB color2)
  1557. {
  1558. for (int i = 0; i < numleds[led_i]; i++)
  1559. {
  1560. leds[led_i][i] = CRGB((color2.red / 75), (color2.green / 75), (color2.blue / 75));
  1561. }
  1562. switch (step[led_i])
  1563. {
  1564. case -1:
  1565. center[led_i] = random16(numleds[led_i]);
  1566. step[led_i] = 0;
  1567. break;
  1568. case 0:
  1569. leds[led_i][center[led_i]] = CRGB(red1, green1, blue1);
  1570. step[led_i] ++;
  1571. break;
  1572. case 12:
  1573. step[led_i] = -1;
  1574. break;
  1575. default: // Middle of the ripples.
  1576. leds[led_i][(center[led_i] + step[led_i] + numleds[led_i]) % numleds[led_i]] += CRGB((color1.red / (step[led_i] * 5)), (color1.green / (step[led_i] * 5)), (color1.blue / (step[led_i] * 5)));
  1577. leds[led_i][(center[led_i] - step[led_i] + numleds[led_i]) % numleds[led_i]] += CRGB((color1.red / (step[led_i] * 5)), (color1.green / (step[led_i] * 5)), (color1.blue / (step[led_i] * 5)));
  1578. step[led_i] ++;
  1579. break;
  1580. }
  1581. }
  1582.  
  1583.  
  1584. // ********** Ripple Overloaded Ended ********************************************
  1585.  
  1586.  
  1587. // *************** New Functions ***********************
  1588.  
  1589. void Crawl (int led_i, CRGBPalette16 colorPalette)
  1590. {
  1591. static uint8_t startIndex[NUMZONES] = {0};
  1592. startIndex[led_i] = startIndex[led_i] + 1; /* higher = faster motion */
  1593. fill_palette( leds[led_i], numleds[led_i],
  1594. startIndex[led_i], 16, /* higher = narrower stripes */
  1595. colorPalette, 255, LINEARBLEND);
  1596. }
  1597.  
  1598.  
  1599.  
  1600. // ********** Police Overloaded ********************************************
  1601. void Police(int led_i)
  1602. { //POLICE LIGHTS (TWO COLOR SOLID)
  1603. idex[led_i]++;
  1604. if (idex[led_i] >= numleds[led_i]) {
  1605. idex[led_i] = 0;
  1606. }
  1607. int idexR = idex[led_i];
  1608. int idexB = antipodal_index(idexR, numleds[led_i]);
  1609. // int thathue = (thishue + delhue) % 255;
  1610. leds[led_i][idexR] = CRGB(red1, green1, blue1); //CHSV(thishue, 255, 255);
  1611. leds[led_i][idexB] = CRGB(red2, green2, blue2); //CHSV(thathue, 255, 255);
  1612.  
  1613. }
  1614.  
  1615. void Police(int led_i, CRGB color1, CRGB color2)
  1616. { //POLICE LIGHTS (TWO COLOR SOLID)
  1617. idex[led_i]++;
  1618. if (idex[led_i] >= numleds[led_i]) {
  1619. idex[led_i] = 0;
  1620. }
  1621. int idexR = idex[led_i];
  1622. int idexB = antipodal_index(idexR, numleds[led_i]);
  1623. // int thathue = (thishue + delhue) % 255;
  1624. leds[led_i][idexR] = color1; //CHSV(thishue, 255, 255);
  1625. leds[led_i][idexB] = color2; //CHSV(thathue, 255, 255);
  1626.  
  1627. }
  1628. // ********** Police Overloaded Ended ********************************************
  1629.  
  1630.  
  1631. // Twinkle
  1632. // Fire
  1633. // Fill_Solid
  1634. // Spooky_Eyes
  1635. // LED_Locator
Advertisement
Add Comment
Please, Sign In to add comment