Advertisement
CleTechnologist

myTree

Dec 11th, 2017
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 23.78 KB | None | 0 0
  1. #define NOT_FULL_TREE
  2. #define MINI_TREE
  3. #define NOT_MID_TREE
  4.  
  5. #define WIFI_CLIENT
  6.  
  7. #define ENABLE_OTA
  8. #define ENABLE_DNS
  9.  
  10. #define FASTLED_ALLOW_INTERRUPTS 0
  11. #define FASTLED_INTERRUPT_RETRY_COUNT 1
  12.  
  13. #include <FastLED.h>
  14. #include <ESP8266WiFi.h>
  15. #include <WiFiClient.h>
  16. #include <ESP8266WebServer.h>
  17. // #include <WiFiUdp.h>
  18. #ifdef ENABLE_DNS
  19. #include <ESP8266mDNS.h>
  20. #endif
  21. #ifdef ENABLE_OTA
  22. #include <ArduinoOTA.h>
  23. #endif
  24.  
  25. #ifdef FULL_TREE
  26.   #define HOSTNAME      "mytree"
  27.   #define NUM_LEDS      100
  28.   #define BRIGHTNESS    220
  29.   #define DATA_PIN      D3
  30.   #define COLOR_ORDER   RGB
  31.   static int loadInterval = 50;
  32. #else
  33.   #ifdef MID_TREE
  34.     #define HOSTNAME      "mobiletree"
  35.     #define NUM_LEDS      20
  36.     #define BRIGHTNESS    220
  37.     #define DATA_PIN      D4
  38.     #define COLOR_ORDER   GRB
  39.     static int loadInterval = 100;
  40.   #else
  41.     #ifdef MINI_TREE
  42.       #define HOSTNAME      "minitree"
  43.       #define NUM_LEDS      10
  44.       #define BRIGHTNESS    10
  45.       #define DATA_PIN      D4
  46.       #define COLOR_ORDER   GRB
  47.       static int loadInterval = 100;
  48.     #endif
  49.   #endif
  50. #endif
  51.  
  52. #ifndef WIFI_CLIENT
  53.   #define SS_ID         "Mobile Tree"
  54.   #define AP_IP         192,168,13,1
  55.   #define SWITCH_LOW    D7
  56.   #define SWITCH        D6
  57.   #define SWITCH_HIGH   D5
  58. #else
  59.   #define SS_ID         "XXXXXX"
  60. #endif
  61.  
  62. #define PASSWORD      "XXXXXX"
  63. #define STATUS_LED    D4
  64. #define DEFAULT_COLOR "#0000FF"
  65. #define RANDOM_BRIGHT BRIGHTNESS / 2
  66. #define RACE_DIV      4
  67. #define BUFFER_SZ     2000
  68. #define LINE_SZ       200
  69.  
  70. // Pattern name/IDs
  71. #define CYLON           0
  72. #define RACING          1
  73. #define RANDOM          2
  74. #define SOLID           3
  75. #define TWINKLE_RANDOM  4
  76. #define TWINKLE_SOLID   5
  77. #define XMAS_RACING     6
  78. #define XMAS_SWEEP      7
  79. #define XMAS_TWINKLE    8
  80. #define RAINBOW         9
  81. #define XMAS_MARCH     10
  82.  
  83. ESP8266WebServer server(80);
  84. uint8_t _red[NUM_LEDS];
  85. uint8_t _green[NUM_LEDS];
  86. uint8_t _blue[NUM_LEDS];
  87. CRGB _leds[NUM_LEDS];
  88.  
  89. String color = DEFAULT_COLOR;
  90. int currentMode = XMAS_MARCH;
  91. boolean changed = true;
  92.  
  93. #ifndef FULL_TREE
  94.   boolean serving = false;
  95. #endif
  96.  
  97. /////////////////////////////////////////////////////////////////////////////
  98. // TODO Weird flashes near the end of the string when changing modes
  99. //        Fixed: See https://github.com/FastLED/FastLED/wiki/Interrupt-problems
  100. // TODO More patterns
  101. //        Blink versions
  102. //        Multi-twinkle
  103. //        Random Marching
  104. /////////////////////////////////////////////////////////////////////////////
  105.  
  106. inline int max(int a,int b) {return ((a)>(b)?(a):(b)); }
  107.  
  108. void loop(void){
  109.   static unsigned long previousMillis = millis();
  110.   unsigned long currentMillis = millis();
  111.  
  112. #ifndef WIFI_CLIENT
  113.   if (digitalRead(SWITCH) == HIGH && !serving) {
  114.     activateWifi();
  115.   }
  116.   if (digitalRead(SWITCH) == LOW && serving) {
  117.     deactivateWifi();
  118.   }
  119. #endif
  120.  
  121. #ifdef ENABLE_OTA
  122.   // Handle OTA processing
  123.   ArduinoOTA.handle();
  124. #endif
  125.  
  126.   // Handle web processing
  127.   server.handleClient();
  128.  
  129.   switch (currentMode) {
  130.     case CYLON:
  131.       cylon(currentMillis, changed);
  132.       break;
  133.     case RACING:
  134.       racing(currentMillis, color, changed);
  135.       break;
  136.     case RANDOM:
  137.       randomColors(currentMillis, changed);
  138.       break;
  139.     case RAINBOW:
  140.       rainbow(currentMillis, changed);
  141.       break;
  142.     case SOLID:
  143.       solidColor(currentMillis, color, changed);
  144.       break;
  145.     case TWINKLE_RANDOM:
  146.       twinkleRandom(currentMillis, changed);
  147.       break;
  148.     case TWINKLE_SOLID:
  149.       twinkleSolid(currentMillis, color, changed);
  150.       break;
  151.     case XMAS_MARCH:
  152.       xmasMarch(currentMillis, changed);
  153.       break;
  154.     case XMAS_RACING:
  155.       xmasRacing(currentMillis, changed);
  156.       break;
  157.     case XMAS_SWEEP:
  158.       xmasSweep(currentMillis, changed);
  159.       break;
  160.     case XMAS_TWINKLE:
  161.       xmasTwinkle(currentMillis, changed);
  162.       break;
  163.     default:
  164.       Serial.print("Oh no!!");
  165.   }
  166.   changed = false;
  167. }
  168.  
  169. void setup(void){
  170.   pinMode(STATUS_LED, OUTPUT);
  171.   Serial.begin(115200);
  172.   Serial.println("");
  173.  
  174.   FastLED.addLeds<WS2812, DATA_PIN, COLOR_ORDER>(_leds, NUM_LEDS);
  175.   // FastLED.setDither(DISABLE_DITHER);
  176.   setAll("#000000");
  177.   show();
  178.  
  179. #ifdef WIFI_CLIENT
  180.   activateWifi();
  181. #else
  182.   pinMode(SWITCH_LOW, OUTPUT);
  183.   digitalWrite(SWITCH_LOW, LOW);
  184.   pinMode(SWITCH_HIGH, OUTPUT);
  185.   digitalWrite(SWITCH_HIGH, HIGH);
  186.   pinMode(SWITCH, INPUT);
  187.   if (digitalRead(SWITCH) == HIGH) {
  188.     activateWifi();
  189.   }
  190. #endif
  191.  
  192. #ifdef ENABLE_OTA
  193.   ArduinoOTA.setHostname(HOSTNAME);
  194.   ArduinoOTA.setPassword((const char *)"XXXXXX");
  195.   ArduinoOTA.onStart([]() {Serial.println("Start");});
  196.   ArduinoOTA.onEnd([]() {Serial.println("\nEnd");});
  197.   ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {Serial.printf("Progress: %u%%\r", (progress / (total / 100)));});
  198.   ArduinoOTA.onError([](ota_error_t error) {
  199.     Serial.printf("Error[%u]: ", error);
  200.     if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
  201.     else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
  202.     else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
  203.     else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
  204.     else if (error == OTA_END_ERROR) Serial.println("End Failed");
  205.   });
  206.   ArduinoOTA.begin();
  207. #endif
  208.  
  209.   server.on("/", handleRoot);
  210.   server.on("/inline", [](){
  211.     server.send(200, "text/plain", "this works as well");
  212.   });
  213.   server.onNotFound(handleNotFound);
  214.   server.begin();
  215.   Serial.println("HTTP server started");
  216.  
  217.   randomSeed(ESP.getCycleCount());
  218. }
  219.  
  220. void activateWifi() {
  221. #ifdef WIFI_CLIENT
  222.   WiFi.hostname(HOSTNAME);
  223.   WiFi.begin(SS_ID, PASSWORD);
  224.   // Wait for connection
  225.   while (WiFi.status() != WL_CONNECTED) {
  226.     delay(500);
  227.     Serial.print(".");
  228.   }
  229.   Serial.println("");
  230.   Serial.print("Connected to ");
  231.   Serial.println(SS_ID);
  232.   Serial.print("IP address: ");
  233.   Serial.println(WiFi.localIP());
  234.   Serial.print("Hostname: ");
  235.   Serial.println(HOSTNAME);
  236. #ifdef ENABLE_DNS
  237.   if (MDNS.begin(HOSTNAME)) {
  238.     Serial.println("MDNS responder started");
  239.   }
  240. #endif
  241. #else
  242.   WiFi.hostname(HOSTNAME);
  243.   WiFi.mode(WIFI_AP);
  244.   WiFi.softAPConfig(IPAddress(AP_IP), IPAddress(AP_IP), IPAddress(255,255,255,0));
  245.   WiFi.softAP(SS_ID, PASSWORD);
  246.   Serial.print("IP address: ");
  247.   Serial.println(WiFi.softAPIP());
  248.   Serial.print("Hostname: ");
  249.   Serial.println(HOSTNAME);
  250. #ifdef ENABLE_DNS
  251.   if (MDNS.begin(HOSTNAME, WiFi.softAPIP())) {
  252.     Serial.println("MDNS responder started");
  253.   }
  254. #endif
  255.   serving = true;
  256. #endif
  257.   switchStatus();
  258. }
  259.  
  260. void deactivateWifi() {
  261. #ifdef WIFI_CLIENT
  262.   WiFi.disconnect();
  263.   Serial.println("Disconnected from WiFi");
  264. #else
  265.   WiFi.softAPdisconnect(true);
  266.   Serial.println("AP shutdown");
  267.   serving = false;
  268. #endif
  269.   switchStatus();
  270. }
  271.  
  272. void switchStatus() {
  273.   static boolean status = false;
  274.   status = !status;
  275.   digitalWrite(STATUS_LED, status ? LOW : HIGH);
  276. }
  277.  
  278. void handleRoot() {
  279.   Serial.println("Client connected");
  280.   switchStatus();
  281.  
  282.   String newMode = server.arg("m");
  283.   if (newMode.length() >= 1) {
  284.     currentMode = newMode.toInt();
  285.     changed = true;
  286.   }
  287.  
  288.   // data from the colorpicker (e.g. #FF00FF)
  289.   String newColor = server.arg("c");
  290.   if (newColor.length() == 7) {
  291.     color = newColor;
  292.     changed = true;
  293.   }
  294.  
  295.   // building a website
  296.   static char temp[BUFFER_SZ];
  297.   temp[0] = '\0';
  298.   int sec = millis() / 1000;
  299.   int min = sec / 60;
  300.   int hr = min / 60;
  301.   char clr [8];
  302.   color.toCharArray(clr, 8);
  303.   strcat(temp,   "<!DOCTYPE html><html>\n");
  304.   strcat(temp,   "<head>\n");
  305.   strcat(temp,   "  <title>My Christmas Tree Controller</title>\n");
  306.   strcat(temp,   "  <style> body { background-color: #cccccc; font-family: Arial; Color: #008; } </style>\n");
  307.   strcat(temp,   "  <meta name=\"viewport\" content=\"width=device-width, height=device-height, \n");
  308.   strcat(temp,   "    initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0\" />\n");
  309.   if (changed) {
  310.     strcat(temp, "  <meta http-equiv=\"refresh\" content=\"0; url=/\">\n");
  311.   }
  312.   strcat(temp,   "</head>\n");
  313.   strcat(temp,   "<body>\n");
  314.   strcat(temp,   "  <center>\n");
  315.   strcat(temp,   "    <h1>My<br>Christmas Tree</h1>\n");
  316.   strcatf(temp,  "    <p>Uptime: %02d:%02d:%02d</p>", hr, min % 60, sec % 60);
  317.   strcat(temp,   "  </center>\n");
  318.   strcat(temp,   "  <form action=\"/\" id=\"pick\" method=\"get\">\n");
  319.   strcatf(temp,  "    <label><input type=\"radio\" name=\"m\" value=\"%d\"%s/> Solid [pick]</label><br>\n", SOLID, currentMode == SOLID ? " checked" : "");
  320.   strcatf(temp,  "    <label><input type=\"radio\" name=\"m\" value=\"%d\"%s/> Solid Twinkle [pick]</label><br>\n", TWINKLE_SOLID, currentMode == TWINKLE_SOLID ? " checked" : "");
  321.   strcatf(temp,  "    <label><input type=\"radio\" name=\"m\" value=\"%d\"%s/> Racing [pick]</label><br>\n", RACING, currentMode == RACING ? " checked" : "");
  322.   strcatf(temp,  "    <label><input type=\"radio\" name=\"m\" value=\"%d\"%s/> Rainbow</label><br>\n", RAINBOW, currentMode == RAINBOW ? " checked" : "");
  323.   strcatf(temp,  "    <label><input type=\"radio\" name=\"m\" value=\"%d\"%s/> Random</label><br>\n", RANDOM, currentMode == RANDOM ? " checked" : "");
  324.   strcatf(temp,  "    <label><input type=\"radio\" name=\"m\" value=\"%d\"%s/> Random Twinkle</label><br>\n", TWINKLE_RANDOM, currentMode == TWINKLE_RANDOM ? " checked" : "");
  325.   strcatf(temp,  "    <label><input type=\"radio\" name=\"m\" value=\"%d\"%s/> Cylon</label><br>\n", CYLON, currentMode == CYLON ? " checked" : "");
  326.   strcatf(temp,  "    <label><input type=\"radio\" name=\"m\" value=\"%d\"%s/> R/G March</label><br>\n", XMAS_MARCH, currentMode == XMAS_MARCH ? " checked" : "");
  327.   strcatf(temp,  "    <label><input type=\"radio\" name=\"m\" value=\"%d\"%s/> R/G Racing</label><br>\n", XMAS_RACING, currentMode == XMAS_RACING ? " checked" : "");
  328.   strcatf(temp,  "    <label><input type=\"radio\" name=\"m\" value=\"%d\"%s/> R/G Sweep</label><br>\n", XMAS_SWEEP, currentMode == XMAS_SWEEP ? " checked" : "");
  329.   strcatf(temp,  "    <label><input type=\"radio\" name=\"m\" value=\"%d\"%s/> R/G Twinkle</label><br>\n", XMAS_TWINKLE, currentMode == XMAS_TWINKLE ? " checked" : "");
  330.   strcat(temp,   "    <center>\n");
  331.   strcatf(temp,  "      <br><input type=\"color\" name=\"c\" value=\"%s\"/><br>\n", clr);
  332.   strcat(temp,   "    </center>\n");
  333.   strcat(temp,   "  </form>\n");
  334.   strcat(temp,   "  <center>\n");
  335.   strcat(temp,   "    <button type=\"submit\" form=\"pick\" value=\"X\">UPDATE</button>\n");
  336.   strcat(temp,   "  </center>\n");
  337.   strcat(temp,   "</body>\n");
  338.   strcat(temp,   "</html>\n");
  339.   server.send(200, "text/html", temp);
  340.   switchStatus();
  341. }
  342.  
  343. void strcatf(char * str, const char * format, ... ) {
  344.   static char tmp[LINE_SZ];
  345.   tmp[0] = '\0';
  346.   va_list args;
  347.   va_start (args, format);
  348.   vsnprintf(tmp, LINE_SZ, format, args);
  349.   va_end (args);
  350.   strncat(str, tmp, LINE_SZ);
  351. }
  352.  
  353. void handleNotFound(){
  354.   switchStatus();
  355.   String message = "File Not Found\n\n";
  356.   message += "URI: ";
  357.   message += server.uri();
  358.   message += "\nMethod: ";
  359.   message += (server.method() == HTTP_GET)?"GET":"POST";
  360.   message += "\nArguments: ";
  361.   message += server.args();
  362.   message += "\n";
  363.   for (uint8_t i=0; i<server.args(); i++){
  364.     message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  365.   }
  366.   server.send(404, "text/plain", message);
  367.   switchStatus();
  368. }
  369.  
  370. void setAll(String color) {
  371.   // converting Hex to Int
  372.   int number = (int) strtol(&color[1], NULL, 16);
  373.   // splitting into three parts
  374.   int r = number >> 16;
  375.   int g = number >> 8 & 0xFF;
  376.   int b = number & 0xFF;
  377.   setAll(r, g, b);
  378. }
  379.  
  380. void set(int pixel, String color) {
  381.   // converting Hex to Int
  382.   int number = (int) strtol(&color[1], NULL, 16);
  383.   // splitting into three parts
  384.   uint8_t r = number >> 16;
  385.   uint8_t g = number >> 8 & 0xFF;
  386.   uint8_t b = number & 0xFF;
  387.   set(pixel, r, g, b);
  388. }
  389.  
  390. void dim(int pixel, String color) {
  391.   set(pixel, color);
  392.   dim(pixel);
  393. }
  394.  
  395. void setRandom(int pixel) {
  396.   int number = random((int) strtol("FFFFFF", NULL, 16));
  397.   // splitting into three parts
  398.   set(pixel, max(number >> 16, RANDOM_BRIGHT), max(number >> 8 & 0xFF, RANDOM_BRIGHT), max(number & 0xFF, RANDOM_BRIGHT));
  399. }
  400.  
  401. void setAll(uint8_t red, uint8_t green, uint8_t blue) {
  402.   for (int i = 0; i < NUM_LEDS; i++) {
  403.     set(i, red, green, blue);
  404.   }
  405. }
  406.  
  407. void set(int pixel, CHSV color) {
  408.   set(pixel, CRGB(color));
  409. }
  410.  
  411. void set(int pixel, uint8_t r, uint8_t g, uint8_t b) {
  412.   set(pixel, CRGB(r, g, b));
  413. }
  414.  
  415. void dim(int pixel, uint8_t r, uint8_t g, uint8_t b) {
  416.   set(pixel, CRGB(r, g, b));
  417.   dim(pixel);
  418. }
  419.  
  420. void set(int pixel) {
  421.   setX(pixel, CRGB(_red[pixel], _green[pixel], _blue[pixel]));
  422. }
  423.  
  424. void dim(int pixel) {
  425.   setX(pixel, CRGB(_red[pixel]/RACE_DIV, _green[pixel]/RACE_DIV, _blue[pixel]/RACE_DIV));
  426. }
  427.  
  428. void dark(int pixel) {
  429.   setX(pixel, CRGB(0, 0, 0));
  430. }
  431.  
  432. void set(int pixel, CRGB color) {
  433.   _red[pixel] = color.r;
  434.   _green[pixel] = color.g;
  435.   _blue[pixel] = color.b;
  436.   setX(pixel, color);
  437. }
  438.  
  439. void setX(int pixel, CRGB color) {
  440.   _leds[pixel] = color;
  441. }
  442.  
  443. void show() {
  444.   FastLED.show(BRIGHTNESS);
  445. }
  446.  
  447. void show(int brightness) {
  448.   FastLED.setBrightness(brightness);
  449.   FastLED.show();
  450. }
  451.  
  452. /////////////////////////////////////////////////////////////////////////////
  453. // Modes/Patterns/Animations
  454. /////////////////////////////////////////////////////////////////////////////
  455.  
  456. void cylon(unsigned long currentMillis, boolean reset) {
  457.   static unsigned long previousMillis = 0;
  458.   static int cycle = 1;
  459.   static int i = -1;
  460.   static uint8_t hue = 0;
  461.   static int interval = 150;
  462.   if (reset) {
  463.     setAll(0, 0, 0);
  464.     show();
  465.     previousMillis = 0;
  466.     cycle = 1;
  467.     i = -1;
  468.     hue = random((int) 255);;
  469.   }
  470.   if (currentMillis - previousMillis >= interval) {
  471.     previousMillis = currentMillis;
  472.     if (cycle == 1) {
  473.       i++;
  474.       if (i < NUM_LEDS) {
  475.         set(i, CHSV(hue++, 255, 255));
  476.         show();
  477.       } else {
  478.         cycle = 2;
  479.       }
  480.     }
  481.     if (cycle == 2) {
  482.       i--;
  483.       if (i >= 0) {
  484.         set(i, CHSV(hue++, 255, 255));
  485.         show();
  486.       } else {
  487.         cycle = 1;
  488.       }
  489.     }
  490.   }
  491. }
  492.  
  493. void xmasSweep(unsigned long currentMillis, boolean reset) {
  494.   static unsigned long previousMillis = 0;
  495.   static int cycle = 1;
  496.   static int i = -1;
  497.   static int interval = 200;
  498.   if (reset) {
  499.     previousMillis = 0;
  500.     cycle = 1;
  501.     i = -1;
  502.   }
  503.   if (currentMillis - previousMillis >= interval) {
  504.     previousMillis = currentMillis;
  505.     if (cycle == 1) {
  506.       i++;
  507.       if (i < NUM_LEDS) {
  508.         set(i, 255, 0, 0);
  509.         show();
  510.       } else {
  511.         cycle = 2;
  512.       }
  513.     }
  514.     if (cycle == 2) {
  515.       i--;
  516.       if (i >= 0) {
  517.         set(i, 0, 255, 0);
  518.         show();
  519.       } else {
  520.         cycle = 1;
  521.       }
  522.     }
  523.   }
  524. }
  525.  
  526. void twinkleSolid(unsigned long currentMillis, String newColor, boolean reset) {
  527.   static unsigned long previousMillis = 0;
  528.   static int i = -1;
  529.   static int interval = 200;
  530.   static boolean loading = true;
  531.   static int blinker = -1;
  532.  
  533.   if (reset) {
  534.     Serial.print("Setting LEDs(");
  535.     Serial.print(newColor);
  536.     Serial.print(")...");
  537.     previousMillis = 0;
  538.     i = -1;
  539.     blinker = -1;
  540.     loading = true;
  541.   }
  542.   if (loading) {
  543.     if (currentMillis - previousMillis >= loadInterval) {
  544.       previousMillis = currentMillis;
  545.       i++;
  546.       if (i < NUM_LEDS) {
  547.         set(i, newColor);
  548.         show();
  549.       } else {
  550.         Serial.println("Done.");
  551.         loading = false;
  552.       }
  553.     }
  554.   } else {
  555.     if (blinker == -1 && currentMillis - previousMillis >= interval) {
  556.       previousMillis = currentMillis;
  557.       blinker = random((int) NUM_LEDS);
  558.       dark(blinker);
  559.       show();
  560.     } else {
  561.       if (blinker >= 0 && currentMillis - previousMillis >= interval) {
  562.         previousMillis = currentMillis;
  563.         set(blinker);
  564.         show();
  565.         blinker = -1;
  566.       }
  567.     }
  568.   }
  569. }
  570.  
  571. void xmasTwinkle(unsigned long currentMillis, boolean reset) {
  572.   static unsigned long previousMillis = 0;
  573.   static int i = -1;
  574.   static int interval = 200;
  575.   static boolean loading = true;
  576.   static int blinker = -1;
  577.  
  578.   if (reset) {
  579.     Serial.print("Setting LEDs(R/G)...");
  580.     previousMillis = 0;
  581.     i = -1;
  582.     loading = true;
  583.   }
  584.   if (loading) {
  585.     if (currentMillis - previousMillis >= loadInterval) {
  586.       previousMillis = currentMillis;
  587.       i++;
  588.       if (i < NUM_LEDS) {
  589.         set(i, 255 * ( i % 2 ), 255 * ( ( i + 1 ) % 2), 0);
  590.         show();
  591.       } else {
  592.         Serial.println("Done.");
  593.         loading = false;
  594.       }
  595.     }
  596.   } else {
  597.     if (blinker == -1 && currentMillis - previousMillis >= interval) {
  598.       previousMillis = currentMillis;
  599.       blinker = random((int) NUM_LEDS);
  600.       dark(blinker);
  601.       show();
  602.     } else {
  603.       if (blinker >= 0 && currentMillis - previousMillis >= interval) {
  604.         previousMillis = currentMillis;
  605.         set(blinker);
  606.         show();
  607.         blinker = -1;
  608.       }
  609.     }
  610.   }
  611. }
  612.  
  613. void twinkleRandom(unsigned long currentMillis, boolean reset) {
  614.   static unsigned long previousMillis = 0;
  615.   static int i = -1;
  616.   static int interval = 200;
  617.   static boolean loading = true;
  618.   static int blinker = -1;
  619.  
  620.   if (reset) {
  621.     Serial.println("Setting LEDs randomly...");
  622.     previousMillis = 0;
  623.     i = -1;
  624.     loading = true;
  625.   }
  626.  
  627.   if (loading) {
  628.     if (currentMillis - previousMillis >= loadInterval) {
  629.       previousMillis = currentMillis;
  630.       i++;
  631.       if (i < NUM_LEDS) {
  632.         setRandom(i);
  633.         show(RANDOM_BRIGHT);
  634.       } else {
  635.         Serial.println("Done.");
  636.         loading = false;
  637.       }
  638.     }
  639.   } else {
  640.     if (blinker == -1 && currentMillis - previousMillis >= interval) {
  641.       previousMillis = currentMillis;
  642.       blinker = random((int) NUM_LEDS);
  643.       dark(blinker);
  644.       show(RANDOM_BRIGHT);
  645.     } else {
  646.       if (blinker >= 0 && currentMillis - previousMillis >= interval) {
  647.         previousMillis = currentMillis;
  648.         set(blinker);
  649.         show(RANDOM_BRIGHT);
  650.         blinker = -1;
  651.       }
  652.     }
  653.   }
  654. }
  655.  
  656. void racing(unsigned long currentMillis, String newColor, boolean reset) {
  657.   static unsigned long previousMillis = 0;
  658.   static int cycle = 1;
  659.   static int i = -1;
  660.   static int interval = 50;
  661.   static boolean loading = true;
  662.   if (reset) {
  663.     Serial.print("Setting LEDs(");
  664.     Serial.print(newColor);
  665.     Serial.print(")...");
  666.     previousMillis = 0;
  667.     i = -1;
  668.     loading = true;
  669.   }
  670.   if (loading) {
  671.     if (currentMillis - previousMillis >= loadInterval) {
  672.       previousMillis = currentMillis;
  673.       i++;
  674.       if (i < NUM_LEDS) {
  675.         dim(i, newColor);
  676.         show();
  677.       } else {
  678.         Serial.println("Done.");
  679.         loading = false;
  680.       }
  681.     }
  682.   } else {
  683.     if (currentMillis - previousMillis >= interval) {
  684.       previousMillis = currentMillis;
  685.       if (cycle == 1) {
  686.         i++;
  687.         if (i < NUM_LEDS) {
  688.           set(i);
  689.           if (i > 0) {
  690.             dim(i-1);
  691.           }
  692.           show();
  693.         } else {
  694.           cycle = 2;
  695.           i--;
  696.         }
  697.       }
  698.       if (cycle == 2) {
  699.         i--;
  700.         if (i >= 0) {
  701.           set(i);
  702.           if (i < (NUM_LEDS - 1)) {
  703.             dim(i+1);
  704.           }
  705.           show();
  706.         } else {
  707.           cycle = 1;
  708.           i++;
  709.         }
  710.       }
  711.     }
  712.   }
  713. }
  714.  
  715. void xmasRacing(unsigned long currentMillis, boolean reset) {
  716.   static unsigned long previousMillis = 0;
  717.   static int cycle = 1;
  718.   static int i = -1;
  719.   static int interval = 100;
  720.   static boolean loading = true;
  721.   if (reset) {
  722.     Serial.print("Setting LEDs(GREEN)...");
  723.     // converting Hex to Int
  724.     previousMillis = 0;
  725.     i = -1;
  726.     loading = true;
  727.   }
  728.   if (loading) {
  729.     if (currentMillis - previousMillis >= loadInterval) {
  730.       previousMillis = currentMillis;
  731.       i++;
  732.       if (i < NUM_LEDS) {
  733.         set(i, 0, 255, 0);
  734.         show();
  735.       } else {
  736.         Serial.println("Done.");
  737.         loading = false;
  738.       }
  739.     }
  740.   } else {
  741.     if (currentMillis - previousMillis >= interval) {
  742.       previousMillis = currentMillis;
  743.       if (cycle == 1) {
  744.         i++;
  745.         if (i < NUM_LEDS) {
  746.           set(i, 255, 0, 0);
  747.           if (i > 0) {
  748.             set(i-1, 0, 255, 0);
  749.           }
  750.           show();
  751.         } else {
  752.           cycle = 2;
  753.           i--;
  754.         }
  755.       } else {
  756.         if (cycle == 2) {
  757.           i--;
  758.           if (i >= 0) {
  759.             set(i, 255, 0, 0);
  760.             if (i < (NUM_LEDS - 1)) {
  761.               set(i+1, 0, 255, 0);
  762.             }
  763.             show();
  764.           } else {
  765.             cycle = 1;
  766.             i++;
  767.           }
  768.         }
  769.       }
  770.     }
  771.   }
  772. }
  773.  
  774. void randomColors(unsigned long currentMillis, boolean reset){
  775.   static unsigned long previousMillis = 0;
  776.   static int i = -1;
  777.   static boolean loading = true;
  778.   if (reset) {
  779.     Serial.println("Setting LEDs randomly...");
  780.     previousMillis = 0;
  781.     i = -1;
  782.     loading = true;
  783.   }
  784.   if (loading) {
  785.     if (currentMillis - previousMillis >= loadInterval) {
  786.       previousMillis = currentMillis;
  787.       i++;
  788.       if (i < NUM_LEDS) {
  789.         setRandom(i);
  790.         show(RANDOM_BRIGHT);
  791.       } else {
  792.         Serial.println("Done.");
  793.         loading = false;
  794.       }
  795.     }
  796.   }
  797. }
  798.  
  799. void solidColor(unsigned long currentMillis, String newColor, boolean reset) {
  800.   static unsigned long previousMillis = 0;
  801.   static int i = -1;
  802.   static boolean loading = true;
  803.   static int r = 0;
  804.   static int g = 0;
  805.   static int b = 0;
  806.   if (reset) {
  807.     Serial.print("Setting LEDs(");
  808.     Serial.print(newColor);
  809.     Serial.print(")...");
  810.     previousMillis = 0;
  811.     i = -1;
  812.     loading = true;
  813.   }
  814.   if (loading) {
  815.     if (currentMillis - previousMillis >= loadInterval) {
  816.       previousMillis = currentMillis;
  817.       i++;
  818.       if (i < NUM_LEDS) {
  819.         set(i, newColor);
  820.         show();
  821.       } else {
  822.         Serial.println("Done.");
  823.         loading = false;
  824.       }
  825.     }
  826.   }
  827. }
  828.  
  829. void rainbow(unsigned long currentMillis, boolean reset) {
  830.   static unsigned long previousMillis = 0;
  831.   static int i = -1;
  832.   static boolean loading = true;
  833.   static uint8_t hue = 0;
  834.   if (reset) {
  835.     Serial.print("Setting LEDs(Rainbow)...");
  836.     previousMillis = 0;
  837.     i = -1;
  838.     loading = true;
  839.     hue = random((int) 255);;
  840.   }
  841.   if (loading) {
  842.     if (currentMillis - previousMillis >= loadInterval) {
  843.       previousMillis = currentMillis;
  844.       i++;
  845.       if (i < NUM_LEDS) {
  846.         hue += (255 / NUM_LEDS);
  847.         set(i, CHSV(hue, 255, 255));
  848.         show();
  849.       } else {
  850.         Serial.println("Done.");
  851.         loading = false;
  852.       }
  853.     }
  854.   }
  855. }
  856.  
  857. void xmasMarch(unsigned long currentMillis, boolean reset) {
  858.   static unsigned long previousMillis = -1000;
  859.   static int i = -1;
  860.   static int interval = 1000;
  861.   static boolean loading = true;
  862.   static int cycle = 1;
  863.  
  864.   if (reset) {
  865.     Serial.print("Setting LEDs(R/G)...");
  866.     previousMillis = -1000;
  867.     i = -1;
  868.     loading = true;
  869.   }
  870.   if (loading) {
  871.     if (currentMillis - previousMillis >= loadInterval) {
  872.       previousMillis = currentMillis;
  873.       i++;
  874.       if (i < NUM_LEDS) {
  875.         set(i, 255 * ( i % 2 ), 255 * ( ( i + 1 ) % 2 ), 0);
  876.         show();
  877.       } else {
  878.         Serial.println("Done.");
  879.         loading = false;
  880.       }
  881.     }
  882.   } else {
  883.     if (currentMillis - previousMillis >= interval) {
  884.       previousMillis = currentMillis;
  885.       if (cycle == 1) {
  886.         for (int j = 0; j < NUM_LEDS; j++) {
  887.           set(j, 255 * ( j % 2 ),  255 * ( ( j + 1 ) % 2 ), 0);
  888.           show();
  889.           cycle = 2;
  890.         }
  891.       } else {
  892.         if (cycle == 2) {
  893.           for (int j = 0; j < NUM_LEDS; j++) {
  894.             set(j, 255 * ( ( j + 1 ) % 2 ), 255 * ( j % 2 ), 0);
  895.             show();
  896.             cycle = 1;
  897.           }
  898.         }
  899.       }
  900.     }
  901.   }
  902. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement