Advertisement
encor50

mqtt json lights nodemcu code

Oct 11th, 2017
1,906
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 58.66 KB | None | 0 0
  1. #define FASTLED_INTERRUPT_RETRY_COUNT 0
  2. #define FASTLED_ALLOW_INTERRUPTS 0
  3.  
  4. #include <ArduinoJson.h>
  5. #include <ESP8266WiFi.h>
  6. #include <PubSubClient.h>
  7. #include "FastLED.h"
  8. #include <ESP8266mDNS.h>
  9. #include <WiFiUdp.h>
  10. #include <ArduinoOTA.h>
  11.  
  12.  
  13.  
  14. /************ WIFI and MQTT Information (CHANGE THESE FOR YOUR SETUP) ******************/
  15. const char* ssid = "YourSSID"; //type your WIFI information inside the quotes
  16. const char* password = "YourWIFIpassword";
  17. const char* mqtt_server = "your.MQTT.server.ip";
  18. const char* mqtt_username = "yourMQTTusername";
  19. const char* mqtt_password = "yourMQTTpassword";
  20. const int mqtt_port = 1883;
  21.  
  22.  
  23.  
  24. /**************************** FOR OTA **************************************************/
  25. #define SENSORNAME "porch" //change this to whatever you want to call your device
  26. #define OTApassword "yourOTApassword" //the password you will need to enter to upload remotely via the ArduinoIDE
  27. int OTAport = 8266;
  28.  
  29.  
  30.  
  31. /************* MQTT TOPICS (change these topics as you wish) **************************/
  32. const char* light_state_topic = "bruh/porch";
  33. const char* light_set_topic = "bruh/porch/set";
  34.  
  35. const char* on_cmd = "ON";
  36. const char* off_cmd = "OFF";
  37. const char* effect = "solid";
  38. String effectString = "solid";
  39. String oldeffectString = "solid";
  40.  
  41.  
  42.  
  43. /****************************************FOR JSON***************************************/
  44. const int BUFFER_SIZE = JSON_OBJECT_SIZE(10);
  45. #define MQTT_MAX_PACKET_SIZE 512
  46.  
  47.  
  48.  
  49. /*********************************** FastLED Defintions ********************************/
  50. #define NUM_LEDS 60
  51. #define DATA_PIN 5
  52. //#define CLOCK_PIN 5
  53. #define CHIPSET WS2813
  54. #define COLOR_ORDER GRB
  55.  
  56. byte realRed = 0;
  57. byte realGreen = 0;
  58. byte realBlue = 0;
  59.  
  60. byte red = 255;
  61. byte green = 255;
  62. byte blue = 255;
  63. byte brightness = 255;
  64.  
  65. #define qsubd(x, b) ((x>b)?b:0) // Digital unsigned subtraction macro. if result <0, then => 0. Otherwise, take on fixed value.
  66. #define qsuba(x, b) ((x>b)?x-b:0) // Analog Unsigned subtraction macro. if result <0, then => 0
  67.  
  68.  
  69. /******************************** GLOBALS for fade/flash *******************************/
  70. bool stateOn = false;
  71. bool startFade = false;
  72. bool onbeforeflash = false;
  73. unsigned long lastLoop = 0;
  74. int transitionTime = 0;
  75. int effectSpeed = 0;
  76. bool inFade = false;
  77. int loopCount = 0;
  78. int stepR, stepG, stepB;
  79. int redVal, grnVal, bluVal;
  80.  
  81. bool flash = false;
  82. bool startFlash = false;
  83. int flashLength = 0;
  84. unsigned long flashStartTime = 0;
  85. byte flashRed = red;
  86. byte flashGreen = green;
  87. byte flashBlue = blue;
  88. byte flashBrightness = brightness;
  89.  
  90.  
  91.  
  92. /********************************** GLOBALS for EFFECTS ******************************/
  93. //RAINBOW
  94. uint8_t thishue = 0; // Starting hue value.
  95. uint8_t deltahue = 5;
  96.  
  97. //CANDYCANE
  98. CRGBPalette16 currentPalettestriped; //for Candy Cane
  99. CRGBPalette16 gPal; //for fire
  100.  
  101. //NOISE
  102. static uint16_t dist; // A random number for our noise generator.
  103. uint16_t scale = 30; // Wouldn't recommend changing this on the fly, or the animation will be really blocky.
  104. uint8_t maxChanges = 48; // Value for blending between palettes.
  105. CRGBPalette16 targetPalette(OceanColors_p);
  106. CRGBPalette16 currentPalette(CRGB::Black);
  107.  
  108. //TWINKLE
  109. #define DENSITY 80
  110. int twinklecounter = 0;
  111.  
  112. //RIPPLE
  113. uint8_t colour; // Ripple colour is randomized.
  114. int center = 0; // Center of the current ripple.
  115. int step = -1; // -1 is the initializing step.
  116. uint8_t myfade = 255; // Starting brightness.
  117. #define maxsteps 16 // Case statement wouldn't allow a variable.
  118. uint8_t bgcol = 0; // Background colour rotates.
  119. int thisdelay = 20; // Standard delay value.
  120.  
  121. //DOTS
  122. uint8_t count = 0; // Count up to 255 and then reverts to 0
  123. uint8_t fadeval = 224; // Trail behind the LED's. Lower => faster fade.
  124. uint8_t bpm = 30;
  125.  
  126. //LIGHTNING
  127. uint8_t frequency = 50; // controls the interval between strikes
  128. uint8_t flashes = 8; //the upper limit of flashes per strike
  129. unsigned int dimmer = 1;
  130. uint8_t ledstart; // Starting location of a flash
  131. uint8_t ledlen;
  132. int lightningcounter = 0;
  133.  
  134. //FUNKBOX
  135. int idex = 0; //-LED INDEX (0 to NUM_LEDS-1
  136. int TOP_INDEX = int(NUM_LEDS / 2);
  137. int thissat = 255; //-FX LOOPS DELAY VAR
  138. uint8_t thishuepolice = 0;
  139. int antipodal_index(int i) {
  140. int iN = i + TOP_INDEX;
  141. if (i >= TOP_INDEX) {
  142. iN = ( i + TOP_INDEX ) % NUM_LEDS;
  143. }
  144. return iN;
  145. }
  146.  
  147. //FIRE
  148. #define COOLING 55
  149. #define SPARKING 120
  150. bool gReverseDirection = false;
  151.  
  152. //BPM
  153. uint8_t gHue = 0;
  154.  
  155.  
  156. WiFiClient espClient;
  157. PubSubClient client(espClient);
  158. struct CRGB leds[NUM_LEDS];
  159. CRGB leds2[NUM_LEDS];
  160. CRGB leds3[NUM_LEDS];
  161. unsigned long previousMillis;
  162. TBlendType currentBlending;
  163.  
  164. int twinkrate = 100; // The higher the value, the lower the number of twinkles. // A delay value for the sequence(s).
  165. uint8_t thisfade = 8; // How quickly does it fade? Lower = slower fade rate. // The hue.
  166. uint8_t thisbri = 255; // Brightness of a sequence.
  167. bool randhue = 1; // Do we want random colours all the time? 1 = yes.
  168. uint8_t thisinc = 1;
  169. int huediff = 256;
  170. unsigned int ledLoc = 0;
  171. uint16_t xscale = 30; // Wouldn't recommend changing this on the fly, or the animation will be really blocky.
  172. uint16_t yscale = 30;
  173. uint8_t thisbright = 255; // How bright should the LED/display be.
  174. uint8_t thisbeat = 5;
  175. uint8_t curhue = 0;
  176. uint8_t thisdiff = 16;
  177. uint8_t numdots = 4;
  178. int8_t thisspeed = 8;
  179. uint8_t allfreq = 32; // You can change the frequency, thus distance between bars.
  180. int thisphase = 0;
  181. uint8_t thiscutoff = 192; // You can change the cutoff value to display this wave. Lower value = longer wave.
  182. uint8_t bgclr = 0; // A rotating background colour.
  183. uint8_t bgbright = 10; // Brightness of background colour
  184. uint8_t thatbeat = 28;
  185. int myhue = 0;
  186.  
  187.  
  188.  
  189. CRGB clr1;
  190. CRGB clr2;
  191. uint8_t speed;
  192. uint8_t loc1;
  193. uint8_t loc2;
  194. uint8_t ran1;
  195. uint8_t ran2;
  196.  
  197. /********************************** START SETUP*****************************************/
  198. void setup() {
  199. FastLED.addLeds<CHIPSET, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  200.  
  201.  
  202. setupStripedPalette( CRGB::Red, CRGB::Red, CRGB::White, CRGB::White); //for CANDY CANE
  203. gPal = HeatColors_p; //for FIRE
  204.  
  205. setup_wifi();
  206. client.setServer(mqtt_server, mqtt_port);
  207. client.setCallback(callback);
  208.  
  209. //OTA SETUP
  210. ArduinoOTA.setPort(OTAport);
  211. // Hostname defaults to esp8266-[ChipID]
  212. ArduinoOTA.setHostname(SENSORNAME);
  213.  
  214. // No authentication by default
  215. ArduinoOTA.setPassword((const char *)OTApassword);
  216.  
  217. ArduinoOTA.onStart([]() {
  218. Serial.println("Starting");
  219. });
  220. ArduinoOTA.onEnd([]() {
  221. Serial.println("\nEnd");
  222. });
  223. ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
  224. Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  225. });
  226. ArduinoOTA.onError([](ota_error_t error) {
  227. Serial.printf("Error[%u]: ", error);
  228. if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
  229. else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
  230. else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
  231. else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
  232. else if (error == OTA_END_ERROR) Serial.println("End Failed");
  233. });
  234. ArduinoOTA.begin();
  235.  
  236. Serial.println("Ready");
  237. Serial.print("IP Address: ");
  238. Serial.println(WiFi.localIP());
  239. dist = random16(12345);
  240. }
  241.  
  242.  
  243.  
  244.  
  245. /********************************** START SETUP WIFI*****************************************/
  246. void setup_wifi() {
  247.  
  248. delay(10);
  249. // We start by connecting to a WiFi network
  250. Serial.println();
  251. Serial.print("Connecting to ");
  252. Serial.println(ssid);
  253.  
  254. WiFi.mode(WIFI_STA);
  255. WiFi.begin(ssid, password);
  256.  
  257. while (WiFi.status() != WL_CONNECTED) {
  258. delay(500);
  259. Serial.print(".");
  260. }
  261.  
  262. Serial.println("");
  263. Serial.println("WiFi connected");
  264. Serial.println("IP address: ");
  265. Serial.println(WiFi.localIP());
  266. }
  267.  
  268. /*
  269. SAMPLE PAYLOAD:
  270. {
  271. "brightness": 120,
  272. "color": {
  273. "r": 255,
  274. "g": 100,
  275. "b": 100
  276. },
  277. "flash": 2,
  278. "transition": 5,
  279. "state": "ON"
  280. }
  281. */
  282.  
  283.  
  284.  
  285. /********************************** START CALLBACK*****************************************/
  286. void callback(char* topic, byte* payload, unsigned int length) {
  287. Serial.print("Message arrived [");
  288. Serial.print(topic);
  289. Serial.print("] ");
  290.  
  291. char message[length + 1];
  292. for (int i = 0; i < length; i++) {
  293. message[i] = (char)payload[i];
  294. }
  295. message[length] = '\0';
  296. Serial.println(message);
  297.  
  298. if (!processJson(message)) {
  299. return;
  300. }
  301.  
  302. if (stateOn) {
  303.  
  304. realRed = map(red, 0, 255, 0, brightness);
  305. realGreen = map(green, 0, 255, 0, brightness);
  306. realBlue = map(blue, 0, 255, 0, brightness);
  307. }
  308. else {
  309.  
  310. realRed = 0;
  311. realGreen = 0;
  312. realBlue = 0;
  313. }
  314.  
  315. Serial.println(effect);
  316.  
  317. startFade = true;
  318. inFade = false; // Kill the current fade
  319.  
  320. sendState();
  321. }
  322.  
  323.  
  324.  
  325. /********************************** START PROCESS JSON*****************************************/
  326. bool processJson(char* message) {
  327. StaticJsonBuffer<BUFFER_SIZE> jsonBuffer;
  328.  
  329. JsonObject& root = jsonBuffer.parseObject(message);
  330.  
  331. if (!root.success()) {
  332. Serial.println("parseObject() failed");
  333. return false;
  334. }
  335.  
  336. if (root.containsKey("state")) {
  337. if (strcmp(root["state"], on_cmd) == 0) {
  338. stateOn = true;
  339. }
  340. else if (strcmp(root["state"], off_cmd) == 0) {
  341. stateOn = false;
  342. onbeforeflash = false;
  343. }
  344. }
  345.  
  346. // If "flash" is included, treat RGB and brightness differently
  347. if (root.containsKey("flash")) {
  348. flashLength = (int)root["flash"] * 1000;
  349.  
  350. oldeffectString = effectString;
  351.  
  352. if (root.containsKey("brightness")) {
  353. flashBrightness = root["brightness"];
  354. }
  355. else {
  356. flashBrightness = brightness;
  357. }
  358.  
  359. if (root.containsKey("color")) {
  360. flashRed = root["color"]["r"];
  361. flashGreen = root["color"]["g"];
  362. flashBlue = root["color"]["b"];
  363. }
  364. else {
  365. flashRed = red;
  366. flashGreen = green;
  367. flashBlue = blue;
  368. }
  369.  
  370. if (root.containsKey("effect")) {
  371. effect = root["effect"];
  372. effectString = effect;
  373. twinklecounter = 0; //manage twinklecounter
  374. }
  375.  
  376. if (root.containsKey("transition")) {
  377. transitionTime = root["transition"];
  378. }
  379. else if ( effectString == "solid") {
  380. transitionTime = 0;
  381. }
  382.  
  383. flashRed = map(flashRed, 0, 255, 0, flashBrightness);
  384. flashGreen = map(flashGreen, 0, 255, 0, flashBrightness);
  385. flashBlue = map(flashBlue, 0, 255, 0, flashBrightness);
  386.  
  387. flash = true;
  388. startFlash = true;
  389. }
  390. else { // Not flashing
  391. flash = false;
  392.  
  393. if (stateOn) { //if the light is turned on and the light isn't flashing
  394. onbeforeflash = true;
  395. }
  396.  
  397. if (root.containsKey("color")) {
  398. red = root["color"]["r"];
  399. green = root["color"]["g"];
  400. blue = root["color"]["b"];
  401. }
  402.  
  403. if (root.containsKey("color_temp")) {
  404. //temp comes in as mireds, need to convert to kelvin then to RGB
  405. int color_temp = root["color_temp"];
  406. unsigned int kelvin = 1000000 / color_temp;
  407.  
  408. temp2rgb(kelvin);
  409.  
  410. }
  411.  
  412. if (root.containsKey("brightness")) {
  413. brightness = root["brightness"];
  414. }
  415.  
  416. if (root.containsKey("effect")) {
  417. effect = root["effect"];
  418. effectString = effect;
  419. twinklecounter = 0; //manage twinklecounter
  420. }
  421.  
  422. if (root.containsKey("transition")) {
  423. transitionTime = root["transition"];
  424. }
  425. else if ( effectString == "solid") {
  426. transitionTime = 0;
  427. }
  428.  
  429. }
  430.  
  431. return true;
  432. }
  433.  
  434.  
  435.  
  436. /********************************** START SEND STATE*****************************************/
  437. void sendState() {
  438. StaticJsonBuffer<BUFFER_SIZE> jsonBuffer;
  439.  
  440. JsonObject& root = jsonBuffer.createObject();
  441.  
  442. root["state"] = (stateOn) ? on_cmd : off_cmd;
  443. JsonObject& color = root.createNestedObject("color");
  444. color["r"] = red;
  445. color["g"] = green;
  446. color["b"] = blue;
  447.  
  448. root["brightness"] = brightness;
  449. root["effect"] = effectString.c_str();
  450.  
  451.  
  452. char buffer[root.measureLength() + 1];
  453. root.printTo(buffer, sizeof(buffer));
  454.  
  455. client.publish(light_state_topic, buffer, true);
  456. }
  457.  
  458.  
  459.  
  460. /********************************** START RECONNECT*****************************************/
  461. void reconnect() {
  462. // Loop until we're reconnected
  463. while (!client.connected()) {
  464. Serial.print("Attempting MQTT connection...");
  465. // Attempt to connect
  466. if (client.connect(SENSORNAME, mqtt_username, mqtt_password)) {
  467. Serial.println("connected");
  468. client.subscribe(light_set_topic);
  469. setColor(0, 0, 0);
  470. sendState();
  471. } else {
  472. Serial.print("failed, rc=");
  473. Serial.print(client.state());
  474. Serial.println(" try again in 5 seconds");
  475. // Wait 5 seconds before retrying
  476. delay(5000);
  477. }
  478. }
  479. }
  480.  
  481.  
  482.  
  483. /********************************** START Set Color*****************************************/
  484. void setColor(int inR, int inG, int inB) {
  485. for (int i = 0; i < NUM_LEDS; i++) {
  486. leds[i].red = inR;
  487. leds[i].green = inG;
  488. leds[i].blue = inB;
  489. }
  490.  
  491. FastLED.show();
  492.  
  493. Serial.println("Setting LEDs:");
  494. Serial.print("r: ");
  495. Serial.print(inR);
  496. Serial.print(", g: ");
  497. Serial.print(inG);
  498. Serial.print(", b: ");
  499. Serial.println(inB);
  500. }
  501.  
  502.  
  503.  
  504. /********************************** START MAIN LOOP*****************************************/
  505. void loop() {
  506.  
  507. if (!client.connected()) {
  508. reconnect();
  509. }
  510.  
  511. if (WiFi.status() != WL_CONNECTED) {
  512. delay(1);
  513. Serial.print("WIFI Disconnected. Attempting reconnection.");
  514. setup_wifi();
  515. return;
  516. }
  517.  
  518.  
  519.  
  520. client.loop();
  521.  
  522. ArduinoOTA.handle();
  523.  
  524.  
  525. //EFFECT BPM
  526. if (effectString == "bpm") {
  527. uint8_t BeatsPerMinute = 62;
  528. CRGBPalette16 palette = PartyColors_p;
  529. uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
  530. for ( int i = 0; i < NUM_LEDS; i++) { //9948
  531. leds[i] = ColorFromPalette(palette, gHue + (i * 2), beat - gHue + (i * 10));
  532. }
  533. if (transitionTime == 0 or transitionTime == NULL) {
  534. transitionTime = 30;
  535. }
  536. showleds();
  537. }
  538.  
  539.  
  540. //EFFECT Candy Cane
  541. if (effectString == "candy cane") {
  542. static uint8_t startIndex = 0;
  543. startIndex = startIndex + 1; /* higher = faster motion */
  544. fill_palette( leds, NUM_LEDS,
  545. startIndex, 16, /* higher = narrower stripes */
  546. currentPalettestriped, 255, LINEARBLEND);
  547. if (transitionTime == 0 or transitionTime == NULL) {
  548. transitionTime = 0;
  549. }
  550. showleds();
  551. }
  552.  
  553.  
  554. //EFFECT CONFETTI
  555. if (effectString == "confetti" ) {
  556. fadeToBlackBy( leds, NUM_LEDS, 10);
  557. int pos = random16(NUM_LEDS);
  558. leds[pos] += CRGB(realRed + random8(64), realGreen, realBlue);
  559. if (transitionTime == 0 or transitionTime == NULL) {
  560. transitionTime = 30;
  561. }
  562. showleds();
  563. }
  564.  
  565.  
  566. //EFFECT CYCLON RAINBOW
  567. if (effectString == "cyclon rainbow") { //Single Dot Down
  568. static uint8_t hue = 0;
  569. // First slide the led in one direction
  570. for (int i = 0; i < NUM_LEDS; i++) {
  571. // Set the i'th led to red
  572. leds[i] = CHSV(hue++, 255, 255);
  573. // Show the leds
  574. showleds();
  575. // now that we've shown the leds, reset the i'th led to black
  576. // leds[i] = CRGB::Black;
  577. fadeall();
  578. // Wait a little bit before we loop around and do it again
  579. delay(10);
  580. }
  581. for (int i = (NUM_LEDS) - 1; i >= 0; i--) {
  582. // Set the i'th led to red
  583. leds[i] = CHSV(hue++, 255, 255);
  584. // Show the leds
  585. showleds();
  586. // now that we've shown the leds, reset the i'th led to black
  587. // leds[i] = CRGB::Black;
  588. fadeall();
  589. // Wait a little bit before we loop around and do it again
  590. delay(10);
  591. }
  592. }
  593.  
  594.  
  595. //EFFECT DOTS
  596. if (effectString == "dots") {
  597. uint8_t inner = beatsin8(bpm, NUM_LEDS / 4, NUM_LEDS / 4 * 3);
  598. uint8_t outer = beatsin8(bpm, 0, NUM_LEDS - 1);
  599. uint8_t middle = beatsin8(bpm, NUM_LEDS / 3, NUM_LEDS / 3 * 2);
  600. leds[middle] = CRGB::Purple;
  601. leds[inner] = CRGB::Blue;
  602. leds[outer] = CRGB::Aqua;
  603. nscale8(leds, NUM_LEDS, fadeval);
  604.  
  605. if (transitionTime == 0 or transitionTime == NULL) {
  606. transitionTime = 30;
  607. }
  608. showleds();
  609. }
  610.  
  611.  
  612. //EFFECT FIRE
  613. if (effectString == "fire") {
  614. Fire2012WithPalette();
  615. if (transitionTime == 0 or transitionTime == NULL) {
  616. transitionTime = 150;
  617. }
  618. showleds();
  619. }
  620. random16_add_entropy( random8());
  621.  
  622. if (effectString == "animations") {
  623. animationA(); // render the first animation into leds2
  624. animationB();
  625. uint8_t ratio = beatsin8(2);
  626. for (int i = 0; i < NUM_LEDS; i++) { // mix the 2 arrays together
  627. leds[i] = blend( leds2[i], leds3[i], ratio );
  628. }
  629.  
  630. showleds();
  631. }
  632. if (effectString == "template") {
  633. ChangeMe(); // Check the demo loop for changes to the variables.
  634.  
  635. EVERY_N_MILLISECONDS(100) {
  636. uint8_t maxChanges = 24;
  637. nblendPaletteTowardPalette(currentPalette, targetPalette, maxChanges); // AWESOME palette blending capability.
  638. }
  639.  
  640. EVERY_N_MILLISECONDS(thisdelay) { // FastLED based non-blocking delay to update/display the sequence.
  641. twinkle();
  642. }
  643.  
  644. EVERY_N_SECONDS(5) { // Change the target palette to a random one every 5 seconds.
  645. static uint8_t baseC = random8(); // You can use this as a baseline colour if you want similar hues in the next line.
  646. targetPalette = CRGBPalette16(CHSV(random8(), 255, random8(128, 255)), CHSV(random8(), 255, random8(128, 255)), CHSV(random8(), 192, random8(128, 255)), CHSV(random8(), 255, random8(128, 255)));
  647. }
  648. showleds();
  649. }
  650. if (effectString == "beatwave") {
  651. beatwave();
  652.  
  653. EVERY_N_MILLISECONDS(100) {
  654. uint8_t maxChanges = 24;
  655. nblendPaletteTowardPalette(currentPalette, targetPalette, maxChanges); // AWESOME palette blending capability.
  656. }
  657.  
  658. EVERY_N_SECONDS(5) { // Change the target palette to a random one every 5 seconds.
  659. targetPalette = CRGBPalette16(CHSV(random8(), 255, random8(128, 255)), CHSV(random8(), 255, random8(128, 255)), CHSV(random8(), 192, random8(128, 255)), CHSV(random8(), 255, random8(128, 255)));
  660. }
  661. showleds();
  662. }
  663. if (effectString == "blendwave") {
  664. blendwave();
  665. showleds();
  666. }
  667. if (effectString == "blur") {
  668. blur();
  669. showleds();
  670. }
  671. if (effectString == "confetti 2") {
  672. ChangeMe(); // Check the demo loop for changes to the variables.
  673. EVERY_N_MILLISECONDS(thisdelay) { // FastLED based non-blocking delay to update/display the sequence.
  674. confetti();
  675. }
  676. showleds();
  677. }
  678. if (effectString == "confetti pal") {
  679. ChangeMe(); // Check the demo loop for changes to the variables.
  680. EVERY_N_MILLISECONDS(100) {
  681. uint8_t maxChanges = 24;
  682. nblendPaletteTowardPalette(currentPalette, targetPalette, maxChanges); // AWESOME palette blending capability.
  683. }
  684. EVERY_N_MILLISECONDS(thisdelay) { // FastLED based non-blocking delay to update/display the sequence.
  685. confetti_pal();
  686. }
  687. showleds();
  688. }
  689. if (effectString == "dotbeat") {
  690. dot_beat();
  691. showleds();
  692. }
  693. if (effectString == "easing") {
  694. EVERY_N_MILLISECONDS(thisdelay) { // FastLED based non-blocking delay to update/display the sequence.
  695. ease();
  696. }
  697. showleds();
  698. }
  699. if (effectString == "every n example") {
  700. everynex();
  701. showleds();
  702. }
  703. if (effectString == "fill grad") {
  704. blendme();
  705. showleds();
  706. }
  707. if (effectString == "inoise8 mover") {
  708. EVERY_N_MILLISECONDS(10) {
  709. nblendPaletteTowardPalette(currentPalette, targetPalette, maxChanges); // AWESOME palette blending capability.
  710. inoise8_mover(); // Update the LED array with noise at the new location
  711. fadeToBlackBy(leds, NUM_LEDS, 4);
  712. }
  713. EVERY_N_SECONDS(5) { // Change the target palette to a random one every 5 seconds.
  714. targetPalette = CRGBPalette16(CHSV(random8(), 255, random8(128, 255)), CHSV(random8(), 255, random8(128, 255)), CHSV(random8(), 192, random8(128, 255)), CHSV(random8(), 255, random8(128, 255)));
  715. }
  716. showleds();
  717. }
  718. if (effectString == "inoise8 pal") {
  719. EVERY_N_MILLISECONDS(10) {
  720. uint8_t maxChanges = 24;
  721. nblendPaletteTowardPalette(currentPalette, targetPalette, maxChanges); // AWESOME palette blending capability.
  722. fillnoise8(); // Update the LED array with noise at the new location
  723. }
  724.  
  725. EVERY_N_SECONDS(5) { // Change the target palette to a random one every 5 seconds.
  726. targetPalette = CRGBPalette16(CHSV(random8(), 255, random8(128, 255)), CHSV(random8(), 255, random8(128, 255)), CHSV(random8(), 192, random8(128, 255)), CHSV(random8(), 255, random8(128, 255)));
  727. }
  728. showleds();
  729. }
  730. if (effectString == "noise 16 1") {
  731. EVERY_N_MILLISECONDS(50) {
  732. nblendPaletteTowardPalette(currentPalette, targetPalette, maxChanges); // Blend towards the target palette
  733. }
  734.  
  735. EVERY_N_SECONDS(5) { // Change the target palette to a random one every 5 seconds.
  736. targetPalette = CRGBPalette16(CHSV(random8(), 255, random8(128, 255)), CHSV(random8(), 255, random8(128, 255)), CHSV(random8(), 192, random8(128, 255)), CHSV(random8(), 255, random8(128, 255)));
  737. }
  738. uint8_t maxChanges = 24;
  739. noise16_1();
  740. showleds();
  741. }
  742. if (effectString == "noise 16 2") {
  743. EVERY_N_MILLISECONDS(50) {
  744. nblendPaletteTowardPalette(currentPalette, targetPalette, maxChanges); // Blend towards the target palette
  745. }
  746.  
  747. EVERY_N_SECONDS(5) { // Change the target palette to a random one every 5 seconds.
  748. targetPalette = CRGBPalette16(CHSV(random8(), 255, random8(128, 255)), CHSV(random8(), 255, random8(128, 255)), CHSV(random8(), 192, random8(128, 255)), CHSV(random8(), 255, random8(128, 255)));
  749. }
  750. uint8_t maxChanges = 24;
  751. noise16_2();
  752. showleds();
  753. }
  754. if (effectString == "noise 16 3") {
  755. EVERY_N_MILLISECONDS(50) {
  756. nblendPaletteTowardPalette(currentPalette, targetPalette, maxChanges); // Blend towards the target palette
  757. }
  758.  
  759. EVERY_N_SECONDS(5) { // Change the target palette to a random one every 5 seconds.
  760. targetPalette = CRGBPalette16(CHSV(random8(), 255, random8(128, 255)), CHSV(random8(), 255, random8(128, 255)), CHSV(random8(), 192, random8(128, 255)), CHSV(random8(), 255, random8(128, 255)));
  761. }
  762. uint8_t maxChanges = 24;
  763. noise16_3();
  764. showleds();
  765. }
  766. if (effectString == "one sine pal") {
  767. EVERY_N_MILLISECONDS(thisdelay) { // FastLED based non-blocking delay to update/display the sequence.
  768. one_sine_pal(millis() >> 4);
  769. }
  770. EVERY_N_MILLISECONDS(100) {
  771. uint8_t maxChanges = 24;
  772. nblendPaletteTowardPalette(currentPalette, targetPalette, maxChanges); // AWESOME palette blending capability.
  773. }
  774. EVERY_N_SECONDS(5) { // Change the target palette to a random one every 5 seconds.
  775. static uint8_t baseC = random8(); // You can use this as a baseline colour if you want similar hues in the next line.
  776. targetPalette = CRGBPalette16(CHSV(random8(), 255, random8(128, 255)), CHSV(random8(), 255, random8(128, 255)), CHSV(random8(), 192, random8(128, 255)), CHSV(random8(), 255, random8(128, 255)));
  777. }
  778. showleds();
  779. }
  780. if (effectString == "palette cross fade") {
  781. ChangePalettePeriodically();
  782.  
  783.  
  784. EVERY_N_MILLISECONDS(100) {
  785. uint8_t maxChanges = 24;
  786. nblendPaletteTowardPalette(currentPalette, targetPalette, maxChanges);
  787. }
  788.  
  789. EVERY_N_MILLISECONDS(thisdelay) {
  790. static uint8_t startIndex = 0;
  791. startIndex += 1; // motion speed
  792. FillLEDsFromPaletteColors(startIndex);
  793. }
  794. showleds();
  795. }
  796. if (effectString == "rainbow beat") {
  797. rainbow_beat();
  798. showleds();
  799. }
  800. if (effectString == "rainbow march") {
  801. EVERY_N_MILLISECONDS(thisdelay) { // FastLED based non-blocking routine to update/display the sequence.
  802. rainbow_march();
  803. }
  804. showleds();
  805. }
  806. if (effectString == "ripple pal") {
  807. EVERY_N_MILLISECONDS(100) {
  808. uint8_t maxChanges = 24;
  809. nblendPaletteTowardPalette(currentPalette, targetPalette, maxChanges); // AWESOME palette blending capability.
  810. }
  811.  
  812. EVERY_N_SECONDS(3) {
  813. targetPalette = CRGBPalette16(CHSV(random8(), 255, 32), CHSV(random8(), random8(64) + 192, 255), CHSV(random8(), 255, 32), CHSV(random8(), 255, 255));
  814. }
  815.  
  816. EVERY_N_MILLISECONDS(thisdelay) { // FastLED based non-blocking delay to update/display the sequence.
  817. ripple();
  818.  
  819. }
  820. showleds();
  821.  
  822. }
  823. if (effectString == "sinelon") {
  824. EVERY_N_MILLISECONDS(100) {
  825. uint8_t maxChanges = 24;
  826. nblendPaletteTowardPalette(currentPalette, targetPalette, maxChanges); // AWESOME palette blending capability.
  827. }
  828.  
  829. EVERY_N_SECONDS(5) { // Change the target palette to a random one every 5 seconds.
  830. static uint8_t baseC = random8(); // You can use this as a baseline colour if you want similar hues in the next line.
  831. targetPalette = CRGBPalette16(CHSV(random8(), 255, random8(128, 255)), CHSV(random8(), 255, random8(128, 255)), CHSV(random8(), 192, random8(128, 255)), CHSV(random8(), 255, random8(128, 255)));
  832. }
  833.  
  834. EVERY_N_MILLISECONDS(thisdelay) { // FastLED based non-blocking delay to update/display the sequence.
  835. sinelon(); // Call our sequence.
  836. }
  837. showleds();
  838. }
  839.  
  840.  
  841.  
  842.  
  843.  
  844.  
  845.  
  846. //EFFECT Glitter
  847. if (effectString == "glitter") {
  848. fadeToBlackBy( leds, NUM_LEDS, 20);
  849. addGlitterColor(80, realRed, realGreen, realBlue);
  850. if (transitionTime == 0 or transitionTime == NULL) {
  851. transitionTime = 30;
  852. }
  853. showleds();
  854. }
  855.  
  856.  
  857. //EFFECT JUGGLE
  858. if (effectString == "juggle" ) { // eight colored dots, weaving in and out of sync with each other
  859. fadeToBlackBy(leds, NUM_LEDS, 20);
  860. for (int i = 0; i < 8; i++) {
  861. leds[beatsin16(i + 7, 0, NUM_LEDS - 1 )] |= CRGB(realRed, realGreen, realBlue);
  862. }
  863. if (transitionTime == 0 or transitionTime == NULL) {
  864. transitionTime = 130;
  865. }
  866. showleds();
  867. }
  868.  
  869.  
  870. //EFFECT LIGHTNING
  871. if (effectString == "lightning") {
  872. twinklecounter = twinklecounter + 1; //Resets strip if previous animation was running
  873. if (twinklecounter < 2) {
  874. FastLED.clear();
  875. FastLED.show();
  876. }
  877. ledstart = random8(NUM_LEDS); // Determine starting location of flash
  878. ledlen = random8(NUM_LEDS - ledstart); // Determine length of flash (not to go beyond NUM_LEDS-1)
  879. for (int flashCounter = 0; flashCounter < random8(3, flashes); flashCounter++) {
  880. if (flashCounter == 0) dimmer = 5; // the brightness of the leader is scaled down by a factor of 5
  881. else dimmer = random8(1, 3); // return strokes are brighter than the leader
  882. fill_solid(leds + ledstart, ledlen, CHSV(255, 0, 255 / dimmer));
  883. showleds(); // Show a section of LED's
  884. delay(random8(4, 10)); // each flash only lasts 4-10 milliseconds
  885. fill_solid(leds + ledstart, ledlen, CHSV(255, 0, 0)); // Clear the section of LED's
  886. showleds();
  887. if (flashCounter == 0) delay (130); // longer delay until next flash after the leader
  888. delay(50 + random8(100)); // shorter delay between strokes
  889. }
  890. delay(random8(frequency) * 100); // delay between strikes
  891. if (transitionTime == 0 or transitionTime == NULL) {
  892. transitionTime = 0;
  893. }
  894. showleds();
  895. }
  896.  
  897.  
  898. //EFFECT POLICE ALL
  899. if (effectString == "police all") { //POLICE LIGHTS (TWO COLOR SOLID)
  900. idex++;
  901. if (idex >= NUM_LEDS) {
  902. idex = 0;
  903. }
  904. int idexR = idex;
  905. int idexB = antipodal_index(idexR);
  906. int thathue = (thishuepolice + 160) % 255;
  907. leds[idexR] = CHSV(thishuepolice, thissat, 255);
  908. leds[idexB] = CHSV(thathue, thissat, 255);
  909. if (transitionTime == 0 or transitionTime == NULL) {
  910. transitionTime = 30;
  911. }
  912. showleds();
  913. }
  914.  
  915. //EFFECT POLICE ONE
  916. if (effectString == "police one") {
  917. idex++;
  918. if (idex >= NUM_LEDS) {
  919. idex = 0;
  920. }
  921. int idexR = idex;
  922. int idexB = antipodal_index(idexR);
  923. int thathue = (thishuepolice + 160) % 255;
  924. for (int i = 0; i < NUM_LEDS; i++ ) {
  925. if (i == idexR) {
  926. leds[i] = CHSV(thishuepolice, thissat, 255);
  927. }
  928. else if (i == idexB) {
  929. leds[i] = CHSV(thathue, thissat, 255);
  930. }
  931. else {
  932. leds[i] = CHSV(0, 0, 0);
  933. }
  934. }
  935. if (transitionTime == 0 or transitionTime == NULL) {
  936. transitionTime = 30;
  937. }
  938. showleds();
  939. }
  940.  
  941.  
  942. //EFFECT RAINBOW
  943. if (effectString == "rainbow") {
  944. // FastLED's built-in rainbow generator
  945. static uint8_t starthue = 0; thishue++;
  946. fill_rainbow(leds, NUM_LEDS, thishue, deltahue);
  947. if (transitionTime == 0 or transitionTime == NULL) {
  948. transitionTime = 130;
  949. }
  950. showleds();
  951. }
  952.  
  953.  
  954. //EFFECT RAINBOW WITH GLITTER
  955. if (effectString == "rainbow with glitter") { // FastLED's built-in rainbow generator with Glitter
  956. static uint8_t starthue = 0;
  957. thishue++;
  958. fill_rainbow(leds, NUM_LEDS, thishue, deltahue);
  959. addGlitter(80);
  960. if (transitionTime == 0 or transitionTime == NULL) {
  961. transitionTime = 130;
  962. }
  963. showleds();
  964. }
  965.  
  966.  
  967. //EFFECT SIENLON
  968. if (effectString == "sinelon") {
  969. fadeToBlackBy( leds, NUM_LEDS, 20);
  970. int pos = beatsin16(13, 0, NUM_LEDS - 1);
  971. leds[pos] += CRGB(realRed, realGreen, realBlue);
  972. if (transitionTime == 0 or transitionTime == NULL) {
  973. transitionTime = 150;
  974. }
  975. showleds();
  976. }
  977.  
  978.  
  979. //EFFECT TWINKLE
  980. if (effectString == "twinkle") {
  981. twinklecounter = twinklecounter + 1;
  982. if (twinklecounter < 2) { //Resets strip if previous animation was running
  983. FastLED.clear();
  984. FastLED.show();
  985. }
  986. const CRGB lightcolor(8, 7, 1);
  987. for ( int i = 0; i < NUM_LEDS; i++) {
  988. if ( !leds[i]) continue; // skip black pixels
  989. if ( leds[i].r & 1) { // is red odd?
  990. leds[i] -= lightcolor; // darken if red is odd
  991. } else {
  992. leds[i] += lightcolor; // brighten if red is even
  993. }
  994. }
  995. if ( random8() < DENSITY) {
  996. int j = random16(NUM_LEDS);
  997. if ( !leds[j] ) leds[j] = lightcolor;
  998. }
  999.  
  1000. if (transitionTime == 0 or transitionTime == NULL) {
  1001. transitionTime = 0;
  1002. }
  1003. showleds();
  1004. }
  1005.  
  1006.  
  1007. EVERY_N_MILLISECONDS(10) {
  1008.  
  1009. nblendPaletteTowardPalette(currentPalette, targetPalette, maxChanges); // FOR NOISE ANIMATIon
  1010. {
  1011. gHue++;
  1012. }
  1013.  
  1014. //EFFECT NOISE
  1015. if (effectString == "noise") {
  1016. for (int i = 0; i < NUM_LEDS; i++) { // Just onE loop to fill up the LED array as all of the pixels change.
  1017. uint8_t index = inoise8(i * scale, dist + i * scale) % 255; // Get a value from the noise function. I'm using both x and y axis.
  1018. leds[i] = ColorFromPalette(currentPalette, index, 255, LINEARBLEND); // With that value, look up the 8 bit colour palette value and assign it to the current LED.
  1019. }
  1020. dist += beatsin8(10, 1, 4); // Moving along the distance (that random number we started out with). Vary it a bit with a sine wave.
  1021. // In some sketches, I've used millis() instead of an incremented counter. Works a treat.
  1022. if (transitionTime == 0 or transitionTime == NULL) {
  1023. transitionTime = 0;
  1024. }
  1025. showleds();
  1026. }
  1027.  
  1028. //EFFECT RIPPLE
  1029. if (effectString == "ripple") {
  1030. for (int i = 0; i < NUM_LEDS; i++) leds[i] = CHSV(bgcol++, 255, 15); // Rotate background colour.
  1031. switch (step) {
  1032. case -1: // Initialize ripple variables.
  1033. center = random(NUM_LEDS);
  1034. colour = random8();
  1035. step = 0;
  1036. break;
  1037. case 0:
  1038. leds[center] = CHSV(colour, 255, 255); // Display the first pixel of the ripple.
  1039. step ++;
  1040. break;
  1041. case maxsteps: // At the end of the ripples.
  1042. step = -1;
  1043. break;
  1044. default: // Middle of the ripples.
  1045. leds[(center + step + NUM_LEDS) % NUM_LEDS] += CHSV(colour, 255, myfade / step * 2); // Simple wrap from Marc Miller
  1046. leds[(center - step + NUM_LEDS) % NUM_LEDS] += CHSV(colour, 255, myfade / step * 2);
  1047. step ++; // Next step.
  1048. break;
  1049. }
  1050. if (transitionTime == 0 or transitionTime == NULL) {
  1051. transitionTime = 30;
  1052. }
  1053. showleds();
  1054. }
  1055.  
  1056. }
  1057.  
  1058.  
  1059. EVERY_N_SECONDS(5) {
  1060. targetPalette = CRGBPalette16(CHSV(random8(), 255, random8(128, 255)), CHSV(random8(), 255, random8(128, 255)), CHSV(random8(), 192, random8(128, 255)), CHSV(random8(), 255, random8(128, 255)));
  1061. }
  1062.  
  1063. //FLASH AND FADE SUPPORT
  1064. if (flash) {
  1065. if (startFlash) {
  1066. startFlash = false;
  1067. flashStartTime = millis();
  1068. }
  1069.  
  1070. if ((millis() - flashStartTime) <= flashLength) {
  1071. if ((millis() - flashStartTime) % 1000 <= 500) {
  1072. setColor(flashRed, flashGreen, flashBlue);
  1073. }
  1074. else {
  1075. setColor(0, 0, 0);
  1076. // If you'd prefer the flashing to happen "on top of"
  1077. // the current color, uncomment the next line.
  1078. // setColor(realRed, realGreen, realBlue);
  1079. }
  1080. }
  1081. else {
  1082. flash = false;
  1083. effectString = oldeffectString;
  1084. if (onbeforeflash) { //keeps light off after flash if light was originally off
  1085. setColor(realRed, realGreen, realBlue);
  1086. }
  1087. else {
  1088. stateOn = false;
  1089. setColor(0, 0, 0);
  1090. sendState();
  1091. }
  1092. }
  1093. }
  1094.  
  1095. if (startFade && effectString == "solid") {
  1096. // If we don't want to fade, skip it.
  1097. if (transitionTime == 0) {
  1098. setColor(realRed, realGreen, realBlue);
  1099.  
  1100. redVal = realRed;
  1101. grnVal = realGreen;
  1102. bluVal = realBlue;
  1103.  
  1104. startFade = false;
  1105. }
  1106. else {
  1107. loopCount = 0;
  1108. stepR = calculateStep(redVal, realRed);
  1109. stepG = calculateStep(grnVal, realGreen);
  1110. stepB = calculateStep(bluVal, realBlue);
  1111.  
  1112. inFade = true;
  1113. }
  1114. }
  1115.  
  1116. if (inFade) {
  1117. startFade = false;
  1118. unsigned long now = millis();
  1119. if (now - lastLoop > transitionTime) {
  1120. if (loopCount <= 1020) {
  1121. lastLoop = now;
  1122.  
  1123. redVal = calculateVal(stepR, redVal, loopCount);
  1124. grnVal = calculateVal(stepG, grnVal, loopCount);
  1125. bluVal = calculateVal(stepB, bluVal, loopCount);
  1126.  
  1127. if (effectString == "solid") {
  1128. setColor(redVal, grnVal, bluVal); // Write current values to LED pins
  1129. }
  1130. loopCount++;
  1131. }
  1132. else {
  1133. inFade = false;
  1134. }
  1135. }
  1136. }
  1137. }
  1138.  
  1139.  
  1140. /**************************** START TRANSITION FADER *****************************************/
  1141. // From https://www.arduino.cc/en/Tutorial/ColorCrossfader
  1142. /* BELOW THIS LINE IS THE MATH -- YOU SHOULDN'T NEED TO CHANGE THIS FOR THE BASICS
  1143. The program works like this:
  1144. Imagine a crossfade that moves the red LED from 0-10,
  1145. the green from 0-5, and the blue from 10 to 7, in
  1146. ten steps.
  1147. We'd want to count the 10 steps and increase or
  1148. decrease color values in evenly stepped increments.
  1149. Imagine a + indicates raising a value by 1, and a -
  1150. equals lowering it. Our 10 step fade would look like:
  1151. 1 2 3 4 5 6 7 8 9 10
  1152. R + + + + + + + + + +
  1153. G + + + + +
  1154. B - - -
  1155. The red rises from 0 to 10 in ten steps, the green from
  1156. 0-5 in 5 steps, and the blue falls from 10 to 7 in three steps.
  1157. In the real program, the color percentages are converted to
  1158. 0-255 values, and there are 1020 steps (255*4).
  1159. To figure out how big a step there should be between one up- or
  1160. down-tick of one of the LED values, we call calculateStep(),
  1161. which calculates the absolute gap between the start and end values,
  1162. and then divides that gap by 1020 to determine the size of the step
  1163. between adjustments in the value.
  1164. */
  1165. int calculateStep(int prevValue, int endValue) {
  1166. int step = endValue - prevValue; // What's the overall gap?
  1167. if (step) { // If its non-zero,
  1168. step = 1020 / step; // divide by 1020
  1169. }
  1170.  
  1171. return step;
  1172. }
  1173. /* The next function is calculateVal. When the loop value, i,
  1174. reaches the step size appropriate for one of the
  1175. colors, it increases or decreases the value of that color by 1.
  1176. (R, G, and B are each calculated separately.)
  1177. */
  1178. int calculateVal(int step, int val, int i) {
  1179. if ((step) && i % step == 0) { // If step is non-zero and its time to change a value,
  1180. if (step > 0) { // increment the value if step is positive...
  1181. val += 1;
  1182. }
  1183. else if (step < 0) { // ...or decrement it if step is negative
  1184. val -= 1;
  1185. }
  1186. }
  1187.  
  1188. // Defensive driving: make sure val stays in the range 0-255
  1189. if (val > 255) {
  1190. val = 255;
  1191. }
  1192. else if (val < 0) {
  1193. val = 0;
  1194. }
  1195.  
  1196. return val;
  1197. }
  1198.  
  1199.  
  1200.  
  1201. /**************************** START STRIPLED PALETTE *****************************************/
  1202. void setupStripedPalette( CRGB A, CRGB AB, CRGB B, CRGB BA) {
  1203. currentPalettestriped = CRGBPalette16(
  1204. A, A, A, A, A, A, A, A, B, B, B, B, B, B, B, B
  1205. // A, A, A, A, A, A, A, A, B, B, B, B, B, B, B, B
  1206. );
  1207. }
  1208.  
  1209.  
  1210.  
  1211. /********************************** START FADE************************************************/
  1212. void fadeall() {
  1213. for (int i = 0; i < NUM_LEDS; i++) {
  1214. leds[i].nscale8(250); //for CYCLon
  1215. }
  1216. }
  1217.  
  1218.  
  1219.  
  1220. /********************************** START FIRE **********************************************/
  1221. void Fire2012WithPalette()
  1222. {
  1223. // Array of temperature readings at each simulation cell
  1224. static byte heat[NUM_LEDS];
  1225.  
  1226. // Step 1. Cool down every cell a little
  1227. for ( int i = 0; i < NUM_LEDS; i++) {
  1228. heat[i] = qsub8( heat[i], random8(0, ((COOLING * 10) / NUM_LEDS) + 2));
  1229. }
  1230.  
  1231. // Step 2. Heat from each cell drifts 'up' and diffuses a little
  1232. for ( int k = NUM_LEDS - 1; k >= 2; k--) {
  1233. heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
  1234. }
  1235.  
  1236. // Step 3. Randomly ignite new 'sparks' of heat near the bottom
  1237. if ( random8() < SPARKING ) {
  1238. int y = random8(7);
  1239. heat[y] = qadd8( heat[y], random8(160, 255) );
  1240. }
  1241.  
  1242. // Step 4. Map from heat cells to LED colors
  1243. for ( int j = 0; j < NUM_LEDS; j++) {
  1244. // Scale the heat value from 0-255 down to 0-240
  1245. // for best results with color palettes.
  1246. byte colorindex = scale8( heat[j], 240);
  1247. CRGB color = ColorFromPalette( gPal, colorindex);
  1248. int pixelnumber;
  1249. if ( gReverseDirection ) {
  1250. pixelnumber = (NUM_LEDS - 1) - j;
  1251. } else {
  1252. pixelnumber = j;
  1253. }
  1254. leds[pixelnumber] = color;
  1255. }
  1256. }
  1257.  
  1258.  
  1259.  
  1260. /********************************** START ADD GLITTER *********************************************/
  1261. void addGlitter( fract8 chanceOfGlitter)
  1262. {
  1263. if ( random8() < chanceOfGlitter) {
  1264. leds[ random16(NUM_LEDS) ] += CRGB::White;
  1265. }
  1266. }
  1267.  
  1268.  
  1269.  
  1270. /********************************** START ADD GLITTER COLOR ****************************************/
  1271. void addGlitterColor( fract8 chanceOfGlitter, int red, int green, int blue)
  1272. {
  1273. if ( random8() < chanceOfGlitter) {
  1274. leds[ random16(NUM_LEDS) ] += CRGB(red, green, blue);
  1275. }
  1276. }
  1277.  
  1278.  
  1279.  
  1280. /********************************** START SHOW LEDS ***********************************************/
  1281. void showleds() {
  1282.  
  1283. delay(1);
  1284.  
  1285. if (stateOn) {
  1286. FastLED.setBrightness(brightness); //EXECUTE EFFECT COLOR
  1287. FastLED.show();
  1288. if (transitionTime > 0 && transitionTime < 130) { //Sets animation speed based on receieved value
  1289. FastLED.delay(1000 / transitionTime);
  1290. //delay(10*transitionTime);
  1291. }
  1292. }
  1293. else if (startFade) {
  1294. setColor(0, 0, 0);
  1295. startFade = false;
  1296. }
  1297. }
  1298. void temp2rgb(unsigned int kelvin) {
  1299. int tmp_internal = kelvin / 100.0;
  1300.  
  1301. // red
  1302. if (tmp_internal <= 66) {
  1303. red = 255;
  1304. } else {
  1305. float tmp_red = 329.698727446 * pow(tmp_internal - 60, -0.1332047592);
  1306. if (tmp_red < 0) {
  1307. red = 0;
  1308. } else if (tmp_red > 255) {
  1309. red = 255;
  1310. } else {
  1311. red = tmp_red;
  1312. }
  1313. }
  1314.  
  1315. // green
  1316. if (tmp_internal <= 66) {
  1317. float tmp_green = 99.4708025861 * log(tmp_internal) - 161.1195681661;
  1318. if (tmp_green < 0) {
  1319. green = 0;
  1320. } else if (tmp_green > 255) {
  1321. green = 255;
  1322. } else {
  1323. green = tmp_green;
  1324. }
  1325. } else {
  1326. float tmp_green = 288.1221695283 * pow(tmp_internal - 60, -0.0755148492);
  1327. if (tmp_green < 0) {
  1328. green = 0;
  1329. } else if (tmp_green > 255) {
  1330. green = 255;
  1331. } else {
  1332. green = tmp_green;
  1333. }
  1334. }
  1335.  
  1336. // blue
  1337. if (tmp_internal >= 66) {
  1338. blue = 255;
  1339. } else if (tmp_internal <= 19) {
  1340. blue = 0;
  1341. } else {
  1342. float tmp_blue = 138.5177312231 * log(tmp_internal - 10) - 305.0447927307;
  1343. if (tmp_blue < 0) {
  1344. blue = 0;
  1345. } else if (tmp_blue > 255) {
  1346. blue = 255;
  1347. } else {
  1348. blue = tmp_blue;
  1349. }
  1350. }
  1351. }
  1352. void animationA() { // running red stripe.
  1353.  
  1354. for (int i = 0; i < NUM_LEDS; i++) {
  1355. uint8_t red = (millis() / 10) + (i * 12); // speed, length
  1356. if (red > 128) red = 0;
  1357. leds2[i] = CRGB(red, 0, 0);
  1358. }
  1359. } // animationA()
  1360.  
  1361.  
  1362.  
  1363. void animationB() { // running green stripe in opposite direction.
  1364. for (int i = 0; i < NUM_LEDS; i++) {
  1365. uint8_t green = (millis() / 5) - (i * 12); // speed, length
  1366. if (green > 128) green = 0;
  1367. leds3[i] = CRGB(0, green, 0);
  1368. }
  1369. } // animationB()
  1370. void twinkle() {
  1371.  
  1372. if (random8() < twinkrate) leds[random16(NUM_LEDS)] += ColorFromPalette(currentPalette, (randhue ? random8() : thishue), 255, currentBlending);
  1373. fadeToBlackBy(leds, NUM_LEDS, thisfade);
  1374.  
  1375. } // twinkle()
  1376.  
  1377.  
  1378.  
  1379. void ChangeMe() { // A time (rather than loop) based demo sequencer. This gives us full control over the length of each sequence.
  1380.  
  1381. uint8_t secondHand = (millis() / 1000) % 10; // IMPORTANT!!! Change '15' to a different value to change duration of the loop.
  1382. static uint8_t lastSecond = 99; // Static variable, means it's only defined once. This is our 'debounce' variable.
  1383. if (lastSecond != secondHand) { // Debounce to make sure we're not repeating an assignment.
  1384. lastSecond = secondHand;
  1385. switch (secondHand) {
  1386. case 0: thisdelay = 10; randhue = 1; thissat = 255; thisfade = 8; twinkrate = 150; break; // You can change values here, one at a time , or altogether.
  1387. case 5: thisdelay = 100; randhue = 0; thishue = random8(); thisfade = 2; twinkrate = 20; break;
  1388. case 10: break;
  1389. }
  1390. }
  1391.  
  1392. } // ChangeMe()
  1393. void beatwave() {
  1394.  
  1395. uint8_t wave1 = beatsin8(9, 0, 255); // That's the same as beatsin8(9);
  1396. uint8_t wave2 = beatsin8(8, 0, 255);
  1397. uint8_t wave3 = beatsin8(7, 0, 255);
  1398. uint8_t wave4 = beatsin8(6, 0, 255);
  1399.  
  1400. for (int i = 0; i < NUM_LEDS; i++) {
  1401. leds[i] = ColorFromPalette( currentPalette, i + wave1 + wave2 + wave3 + wave4, 255, currentBlending);
  1402. }
  1403.  
  1404. } // beatwave()
  1405.  
  1406. void blendwave() {
  1407.  
  1408. speed = beatsin8(6, 0, 255);
  1409.  
  1410. clr1 = blend(CHSV(beatsin8(3, 0, 255), 255, 255), CHSV(beatsin8(4, 0, 255), 255, 255), speed);
  1411. clr2 = blend(CHSV(beatsin8(4, 0, 255), 255, 255), CHSV(beatsin8(3, 0, 255), 255, 255), speed);
  1412.  
  1413. loc1 = beatsin8(10, 0, NUM_LEDS - 1);
  1414.  
  1415. fill_gradient_RGB(leds, 0, clr2, loc1, clr1);
  1416. fill_gradient_RGB(leds, loc1, clr2, NUM_LEDS - 1, clr1);
  1417.  
  1418. } // blendwave()
  1419. void blur() {
  1420. uint8_t blurAmount = dim8_raw( beatsin8(3, 64, 192) ); // A sinewave at 3 Hz with values ranging from 64 to 192.
  1421. blur1d( leds, NUM_LEDS, blurAmount); // Apply some blurring to whatever's already on the strip, which will eventually go black.
  1422.  
  1423. uint8_t i = beatsin8( 9, 0, NUM_LEDS);
  1424. uint8_t j = beatsin8( 7, 0, NUM_LEDS);
  1425. uint8_t k = beatsin8( 5, 0, NUM_LEDS);
  1426.  
  1427. // The color of each point shifts over time, each at a different speed.
  1428. uint16_t ms = millis();
  1429. leds[(i + j) / 2] = CHSV( ms / 29, 200, 255);
  1430. leds[(j + k) / 2] = CHSV( ms / 41, 200, 255);
  1431. leds[(k + i) / 2] = CHSV( ms / 73, 200, 255);
  1432. leds[(k + i + j) / 3] = CHSV( ms / 53, 200, 255);
  1433. }//blur()
  1434. void confetti() { // random colored speckles that blink in and fade smoothly
  1435. fadeToBlackBy(leds, NUM_LEDS, thisfade); // Low values = slower fade.
  1436. int pos = random16(NUM_LEDS); // Pick an LED at random.
  1437. leds[pos] += CHSV((thishue + random16(huediff)) / 4 , thissat, thisbri); // I use 12 bits for hue so that the hue increment isn't too quick.
  1438. thishue = thishue + thisinc; // It increments here.
  1439. } // confetti()
  1440. void confetti_pal() { // random colored speckles that blink in and fade smoothly
  1441.  
  1442. fadeToBlackBy(leds, NUM_LEDS, thisfade); // Low values = slower fade.
  1443. int pos = random16(NUM_LEDS); // Pick an LED at random.
  1444. leds[pos] = ColorFromPalette(currentPalette, thishue + random16(huediff) / 4 , thisbri, currentBlending);
  1445. thishue = thishue + thisinc; // It increments here.
  1446.  
  1447. } // confetti_pal()
  1448. void dot_beat() {
  1449.  
  1450. uint8_t inner = beatsin8(bpm, NUM_LEDS / 4, NUM_LEDS / 4 * 3); // Move 1/4 to 3/4
  1451. uint8_t outer = beatsin8(bpm, 0, NUM_LEDS - 1); // Move entire length
  1452. uint8_t middle = beatsin8(bpm, NUM_LEDS / 3, NUM_LEDS / 3 * 2); // Move 1/3 to 2/3
  1453.  
  1454. leds[middle] = CRGB::Purple;
  1455. leds[inner] = CRGB::Blue;
  1456. leds[outer] = CRGB::Aqua;
  1457.  
  1458. nscale8(leds, NUM_LEDS, fadeval); // Fade the entire array. Or for just a few LED's, use nscale8(&leds[2], 5, fadeval);
  1459.  
  1460. } // dot_beat()
  1461. void ease() {
  1462.  
  1463. static uint8_t easeOutVal = 0;
  1464. static uint8_t easeInVal = 0;
  1465. static uint8_t lerpVal = 0;
  1466.  
  1467. easeOutVal = ease8InOutQuad(easeInVal); // Start with easeInVal at 0 and then go to 255 for the full easing.
  1468. easeInVal++;
  1469.  
  1470. lerpVal = lerp8by8(0, NUM_LEDS, easeOutVal); // Map it to the number of LED's you have.
  1471.  
  1472. leds[lerpVal] = CRGB::Red;
  1473. fadeToBlackBy(leds, NUM_LEDS, 16); // 8 bit, 1 = slow fade, 255 = fast fade
  1474.  
  1475. } // ease()
  1476. void everynex() {
  1477. EVERY_N_MILLIS_I(thisTimer, 100) { // This only sets the Initial timer delay. To change this value, you need to use thisTimer.setPeriod(); You could also call it thatTimer and so on.
  1478. uint8_t timeval = beatsin8(10, 5, 100); // Create/modify a variable based on the beastsin8() function.
  1479. thisTimer.setPeriod(timeval); // Use that as our update timer value.
  1480. ledLoc = (ledLoc + 1) % (NUM_LEDS - 1); // A simple routine to just move the active LED UP the strip.
  1481. leds[ledLoc] = ColorFromPalette(currentPalette, ledLoc, 255, currentBlending); // Pick a slightly rotating colour from the Palette
  1482. }
  1483.  
  1484. fadeToBlackBy(leds, NUM_LEDS, 8); // Leave a nice comet trail behind.
  1485.  
  1486. }//every n example
  1487.  
  1488. void blendme() {
  1489. uint8_t starthue = beatsin8(20, 0, 255);
  1490. uint8_t endhue = beatsin8(35, 0, 255);
  1491. if (starthue < endhue) {
  1492. fill_gradient(leds, NUM_LEDS, CHSV(starthue, 255, 255), CHSV(endhue, 255, 255), FORWARD_HUES); // If we don't have this, the colour fill will flip around
  1493. } else {
  1494. fill_gradient(leds, NUM_LEDS, CHSV(starthue, 255, 255), CHSV(endhue, 255, 255), BACKWARD_HUES);
  1495. }
  1496. } // blendme()
  1497. void inoise8_mover() {
  1498.  
  1499. uint8_t locn = inoise8(xscale, dist + yscale) % 255; // Get a new pixel location from moving noise.
  1500. uint8_t pixlen = map(locn, 0, 255, 0, NUM_LEDS); // Map that to the length of the strand.
  1501. leds[pixlen] = ColorFromPalette(currentPalette, pixlen, 255, LINEARBLEND); // Use that value for both the location as well as the palette index colour for the pixel.
  1502.  
  1503. dist += beatsin8(10, 1, 4); // Moving along the distance (that random number we started out with). Vary it a bit with a sine wave.
  1504.  
  1505. } // inoise8_mover()
  1506. void fillnoise8() {
  1507.  
  1508. for (int i = 0; i < NUM_LEDS; i++) { // Just ONE loop to fill up the LED array as all of the pixels change.
  1509. uint8_t index = inoise8(i * xscale, dist + i * yscale) % 255; // Get a value from the noise function. I'm using both x and y axis.
  1510. leds[i] = ColorFromPalette(currentPalette, index, 255, LINEARBLEND); // With that value, look up the 8 bit colour palette value and assign it to the current LED.
  1511. }
  1512.  
  1513. dist += beatsin8(10, 1, 4); // Moving along the distance (that random number we started out with). Vary it a bit with a sine wave.
  1514. // In some sketches, I've used millis() instead of an incremented counter. Works a treat.
  1515. } // fillnoise8()
  1516. void noise16_1() { // moves a noise up and down while slowly shifting to the side
  1517.  
  1518. uint16_t scale = 1000; // the "zoom factor" for the noise
  1519.  
  1520. for (uint16_t i = 0; i < NUM_LEDS; i++) {
  1521.  
  1522. uint16_t shift_x = beatsin8(5); // the x position of the noise field swings @ 17 bpm
  1523. uint16_t shift_y = millis() / 100; // the y position becomes slowly incremented
  1524.  
  1525.  
  1526. uint16_t real_x = (i + shift_x) * scale; // the x position of the noise field swings @ 17 bpm
  1527. uint16_t real_y = (i + shift_y) * scale; // the y position becomes slowly incremented
  1528. uint32_t real_z = millis() * 20; // the z position becomes quickly incremented
  1529.  
  1530. uint8_t noise = inoise16(real_x, real_y, real_z) >> 8; // get the noise data and scale it down
  1531.  
  1532. uint8_t index = sin8(noise * 3); // map LED color based on noise data
  1533. uint8_t bri = noise;
  1534.  
  1535. leds[i] = ColorFromPalette(currentPalette, index, bri, LINEARBLEND); // With that value, look up the 8 bit colour palette value and assign it to the current LED.
  1536. }
  1537.  
  1538. } // noise16_1()
  1539. void noise16_2() { // just moving along one axis = "lavalamp effect"
  1540.  
  1541. uint8_t scale = 1000; // the "zoom factor" for the noise
  1542.  
  1543. for (uint16_t i = 0; i < NUM_LEDS; i++) {
  1544.  
  1545. uint16_t shift_x = millis() / 10; // x as a function of time
  1546. uint16_t shift_y = 0;
  1547.  
  1548. uint32_t real_x = (i + shift_x) * scale; // calculate the coordinates within the noise field
  1549. uint32_t real_y = (i + shift_y) * scale; // based on the precalculated positions
  1550. uint32_t real_z = 4223;
  1551.  
  1552. uint8_t noise = inoise16(real_x, real_y, real_z) >> 8; // get the noise data and scale it down
  1553.  
  1554. uint8_t index = sin8(noise * 3); // map led color based on noise data
  1555. uint8_t bri = noise;
  1556.  
  1557. leds[i] = ColorFromPalette(currentPalette, index, bri, LINEARBLEND); // With that value, look up the 8 bit colour palette value and assign it to the current LED.
  1558.  
  1559. }
  1560.  
  1561. } // noise16_2()
  1562. void noise16_3() { // no x/y shifting but scrolling along
  1563.  
  1564. uint8_t scale = 1000; // the "zoom factor" for the noise
  1565.  
  1566. for (uint16_t i = 0; i < NUM_LEDS; i++) {
  1567.  
  1568. uint16_t shift_x = 4223; // no movement along x and y
  1569. uint16_t shift_y = 1234;
  1570.  
  1571. uint32_t real_x = (i + shift_x) * scale; // calculate the coordinates within the noise field
  1572. uint32_t real_y = (i + shift_y) * scale; // based on the precalculated positions
  1573. uint32_t real_z = millis() * 2; // increment z linear
  1574.  
  1575. uint8_t noise = inoise16(real_x, real_y, real_z) >> 7; // get the noise data and scale it down
  1576.  
  1577. uint8_t index = sin8(noise * 3); // map led color based on noise data
  1578. uint8_t bri = noise;
  1579.  
  1580. leds[i] = ColorFromPalette(currentPalette, index, bri, LINEARBLEND); // With that value, look up the 8 bit colour palette value and assign it to the current LED.
  1581. }
  1582.  
  1583. } // noise16_3()
  1584. void one_sine_pal(uint8_t colorIndex) { // This is the heart of this program. Sure is short.
  1585.  
  1586. thisphase += thisspeed; // You can change direction and speed individually.
  1587.  
  1588. for (int k = 0; k < NUM_LEDS - 1; k++) { // For each of the LED's in the strand, set a brightness based on a wave as follows:
  1589. int thisbright = qsubd(cubicwave8((k * allfreq) + thisphase), thiscutoff); // qsub sets a minimum value called thiscutoff. If < thiscutoff, then bright = 0. Otherwise, bright = 128 (as defined in qsub)..
  1590. leds[k] = CHSV(bgclr, 255, bgbright); // First set a background colour, but fully saturated.
  1591. leds[k] += ColorFromPalette( currentPalette, colorIndex, thisbright, currentBlending); // Let's now add the foreground colour.
  1592. colorIndex += 3;
  1593. }
  1594.  
  1595. bgclr++;
  1596.  
  1597. } // one_sine_pal()
  1598. void ChangePalettePeriodically() {
  1599.  
  1600. uint8_t secondHand = (millis() / 1000) % 60;
  1601. static uint8_t lastSecond = 99;
  1602.  
  1603. if (lastSecond != secondHand) {
  1604. lastSecond = secondHand;
  1605. CRGB p = CHSV(HUE_PURPLE, 255, 255);
  1606. CRGB g = CHSV(HUE_GREEN, 255, 255);
  1607. CRGB b = CRGB::Black;
  1608. CRGB w = CRGB::White;
  1609. if (secondHand == 0) {
  1610. targetPalette = RainbowColors_p;
  1611. }
  1612. if (secondHand == 10) {
  1613. targetPalette = CRGBPalette16(g, g, b, b, p, p, b, b, g, g, b, b, p, p, b, b);
  1614. }
  1615. if (secondHand == 20) {
  1616. targetPalette = CRGBPalette16(b, b, b, w, b, b, b, w, b, b, b, w, b, b, b, w);
  1617. }
  1618. if (secondHand == 30) {
  1619. targetPalette = LavaColors_p;
  1620. }
  1621. if (secondHand == 40) {
  1622. targetPalette = CloudColors_p;
  1623. }
  1624. if (secondHand == 50) {
  1625. targetPalette = PartyColors_p;
  1626. }
  1627. }
  1628.  
  1629. } // ChangePalettePeriodically()
  1630. void FillLEDsFromPaletteColors(uint8_t colorIndex) {
  1631.  
  1632. for (int i = 0; i < NUM_LEDS; i++) {
  1633. leds[i] = ColorFromPalette(currentPalette, colorIndex + sin8(i * 16), 255);
  1634. colorIndex += 3;
  1635. }
  1636.  
  1637. } // FillLEDsFromPaletteColors()
  1638. void sinelon() { // a colored dot sweeping back and forth, with fading trails
  1639.  
  1640. fadeToBlackBy( leds, NUM_LEDS, thisfade);
  1641. int pos1 = beatsin16(thisbeat, 0, NUM_LEDS);
  1642. int pos2 = beatsin16(thatbeat, 0, NUM_LEDS);
  1643.  
  1644. leds[(pos1 + pos2) / 2] += ColorFromPalette(currentPalette, myhue++, thisbri, currentBlending);
  1645.  
  1646. } // sinelon()
  1647. void ripple() {
  1648.  
  1649. fadeToBlackBy(leds, NUM_LEDS, fadeval); // 8 bit, 1 = slow, 255 = fast
  1650.  
  1651. switch (step) {
  1652.  
  1653. case -1: // Initialize ripple variables.
  1654. center = random(NUM_LEDS);
  1655. colour = random8();
  1656. step = 0;
  1657. break;
  1658.  
  1659. case 0:
  1660. leds[center] = ColorFromPalette(currentPalette, colour, myfade, currentBlending);
  1661.  
  1662. step ++;
  1663. break;
  1664.  
  1665. case maxsteps: // At the end of the ripples.
  1666. step = -1;
  1667. break;
  1668.  
  1669. default: // Middle of the ripples.
  1670. leds[(center + step + NUM_LEDS) % NUM_LEDS] += ColorFromPalette(currentPalette, colour, myfade / step * 2, currentBlending); // Simple wrap from Marc Miller
  1671. leds[(center - step + NUM_LEDS) % NUM_LEDS] += ColorFromPalette(currentPalette, colour, myfade / step * 2, currentBlending);
  1672. step ++; // Next step.
  1673. break;
  1674. } // switch step
  1675.  
  1676. } // ripple()
  1677. void rainbow_march() { // The fill_rainbow call doesn't support brightness levels
  1678. thishue++; // Increment the starting hue.
  1679. fill_rainbow(leds, NUM_LEDS, thishue, deltahue); // Use FastLED's fill_rainbow routine.
  1680. } // rainbow_march()
  1681. void rainbow_beat() {
  1682.  
  1683. uint8_t beatA = beatsin8(17, 0, 255); // Starting hue
  1684. uint8_t beatB = beatsin8(13, 0, 255);
  1685. fill_rainbow(leds, NUM_LEDS, (beatA + beatB) / 2, 8); // Use FastLED's fill_rainbow routine.
  1686.  
  1687. } // rainbow_beat()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement