Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define NOT_FULL_TREE
- #define MINI_TREE
- #define NOT_MID_TREE
- #define WIFI_CLIENT
- #define ENABLE_OTA
- #define ENABLE_DNS
- #define FASTLED_ALLOW_INTERRUPTS 0
- #define FASTLED_INTERRUPT_RETRY_COUNT 1
- #include <FastLED.h>
- #include <ESP8266WiFi.h>
- #include <WiFiClient.h>
- #include <ESP8266WebServer.h>
- // #include <WiFiUdp.h>
- #ifdef ENABLE_DNS
- #include <ESP8266mDNS.h>
- #endif
- #ifdef ENABLE_OTA
- #include <ArduinoOTA.h>
- #endif
- #ifdef FULL_TREE
- #define HOSTNAME "mytree"
- #define NUM_LEDS 100
- #define BRIGHTNESS 220
- #define DATA_PIN D3
- #define COLOR_ORDER RGB
- static int loadInterval = 50;
- #else
- #ifdef MID_TREE
- #define HOSTNAME "mobiletree"
- #define NUM_LEDS 20
- #define BRIGHTNESS 220
- #define DATA_PIN D4
- #define COLOR_ORDER GRB
- static int loadInterval = 100;
- #else
- #ifdef MINI_TREE
- #define HOSTNAME "minitree"
- #define NUM_LEDS 10
- #define BRIGHTNESS 10
- #define DATA_PIN D4
- #define COLOR_ORDER GRB
- static int loadInterval = 100;
- #endif
- #endif
- #endif
- #ifndef WIFI_CLIENT
- #define SS_ID "Mobile Tree"
- #define AP_IP 192,168,13,1
- #define SWITCH_LOW D7
- #define SWITCH D6
- #define SWITCH_HIGH D5
- #else
- #define SS_ID "XXXXXX"
- #endif
- #define PASSWORD "XXXXXX"
- #define STATUS_LED D4
- #define DEFAULT_COLOR "#0000FF"
- #define RANDOM_BRIGHT BRIGHTNESS / 2
- #define RACE_DIV 4
- #define BUFFER_SZ 2000
- #define LINE_SZ 200
- // Pattern name/IDs
- #define CYLON 0
- #define RACING 1
- #define RANDOM 2
- #define SOLID 3
- #define TWINKLE_RANDOM 4
- #define TWINKLE_SOLID 5
- #define XMAS_RACING 6
- #define XMAS_SWEEP 7
- #define XMAS_TWINKLE 8
- #define RAINBOW 9
- #define XMAS_MARCH 10
- ESP8266WebServer server(80);
- uint8_t _red[NUM_LEDS];
- uint8_t _green[NUM_LEDS];
- uint8_t _blue[NUM_LEDS];
- CRGB _leds[NUM_LEDS];
- String color = DEFAULT_COLOR;
- int currentMode = XMAS_MARCH;
- boolean changed = true;
- #ifndef FULL_TREE
- boolean serving = false;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // TODO Weird flashes near the end of the string when changing modes
- // Fixed: See https://github.com/FastLED/FastLED/wiki/Interrupt-problems
- // TODO More patterns
- // Blink versions
- // Multi-twinkle
- // Random Marching
- /////////////////////////////////////////////////////////////////////////////
- inline int max(int a,int b) {return ((a)>(b)?(a):(b)); }
- void loop(void){
- static unsigned long previousMillis = millis();
- unsigned long currentMillis = millis();
- #ifndef WIFI_CLIENT
- if (digitalRead(SWITCH) == HIGH && !serving) {
- activateWifi();
- }
- if (digitalRead(SWITCH) == LOW && serving) {
- deactivateWifi();
- }
- #endif
- #ifdef ENABLE_OTA
- // Handle OTA processing
- ArduinoOTA.handle();
- #endif
- // Handle web processing
- server.handleClient();
- switch (currentMode) {
- case CYLON:
- cylon(currentMillis, changed);
- break;
- case RACING:
- racing(currentMillis, color, changed);
- break;
- case RANDOM:
- randomColors(currentMillis, changed);
- break;
- case RAINBOW:
- rainbow(currentMillis, changed);
- break;
- case SOLID:
- solidColor(currentMillis, color, changed);
- break;
- case TWINKLE_RANDOM:
- twinkleRandom(currentMillis, changed);
- break;
- case TWINKLE_SOLID:
- twinkleSolid(currentMillis, color, changed);
- break;
- case XMAS_MARCH:
- xmasMarch(currentMillis, changed);
- break;
- case XMAS_RACING:
- xmasRacing(currentMillis, changed);
- break;
- case XMAS_SWEEP:
- xmasSweep(currentMillis, changed);
- break;
- case XMAS_TWINKLE:
- xmasTwinkle(currentMillis, changed);
- break;
- default:
- Serial.print("Oh no!!");
- }
- changed = false;
- }
- void setup(void){
- pinMode(STATUS_LED, OUTPUT);
- Serial.begin(115200);
- Serial.println("");
- FastLED.addLeds<WS2812, DATA_PIN, COLOR_ORDER>(_leds, NUM_LEDS);
- // FastLED.setDither(DISABLE_DITHER);
- setAll("#000000");
- show();
- #ifdef WIFI_CLIENT
- activateWifi();
- #else
- pinMode(SWITCH_LOW, OUTPUT);
- digitalWrite(SWITCH_LOW, LOW);
- pinMode(SWITCH_HIGH, OUTPUT);
- digitalWrite(SWITCH_HIGH, HIGH);
- pinMode(SWITCH, INPUT);
- if (digitalRead(SWITCH) == HIGH) {
- activateWifi();
- }
- #endif
- #ifdef ENABLE_OTA
- ArduinoOTA.setHostname(HOSTNAME);
- ArduinoOTA.setPassword((const char *)"XXXXXX");
- ArduinoOTA.onStart([]() {Serial.println("Start");});
- ArduinoOTA.onEnd([]() {Serial.println("\nEnd");});
- ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {Serial.printf("Progress: %u%%\r", (progress / (total / 100)));});
- ArduinoOTA.onError([](ota_error_t error) {
- Serial.printf("Error[%u]: ", error);
- if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
- else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
- else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
- else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
- else if (error == OTA_END_ERROR) Serial.println("End Failed");
- });
- ArduinoOTA.begin();
- #endif
- server.on("/", handleRoot);
- server.on("/inline", [](){
- server.send(200, "text/plain", "this works as well");
- });
- server.onNotFound(handleNotFound);
- server.begin();
- Serial.println("HTTP server started");
- randomSeed(ESP.getCycleCount());
- }
- void activateWifi() {
- #ifdef WIFI_CLIENT
- WiFi.hostname(HOSTNAME);
- WiFi.begin(SS_ID, PASSWORD);
- // Wait for connection
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.print("Connected to ");
- Serial.println(SS_ID);
- Serial.print("IP address: ");
- Serial.println(WiFi.localIP());
- Serial.print("Hostname: ");
- Serial.println(HOSTNAME);
- #ifdef ENABLE_DNS
- if (MDNS.begin(HOSTNAME)) {
- Serial.println("MDNS responder started");
- }
- #endif
- #else
- WiFi.hostname(HOSTNAME);
- WiFi.mode(WIFI_AP);
- WiFi.softAPConfig(IPAddress(AP_IP), IPAddress(AP_IP), IPAddress(255,255,255,0));
- WiFi.softAP(SS_ID, PASSWORD);
- Serial.print("IP address: ");
- Serial.println(WiFi.softAPIP());
- Serial.print("Hostname: ");
- Serial.println(HOSTNAME);
- #ifdef ENABLE_DNS
- if (MDNS.begin(HOSTNAME, WiFi.softAPIP())) {
- Serial.println("MDNS responder started");
- }
- #endif
- serving = true;
- #endif
- switchStatus();
- }
- void deactivateWifi() {
- #ifdef WIFI_CLIENT
- WiFi.disconnect();
- Serial.println("Disconnected from WiFi");
- #else
- WiFi.softAPdisconnect(true);
- Serial.println("AP shutdown");
- serving = false;
- #endif
- switchStatus();
- }
- void switchStatus() {
- static boolean status = false;
- status = !status;
- digitalWrite(STATUS_LED, status ? LOW : HIGH);
- }
- void handleRoot() {
- Serial.println("Client connected");
- switchStatus();
- String newMode = server.arg("m");
- if (newMode.length() >= 1) {
- currentMode = newMode.toInt();
- changed = true;
- }
- // data from the colorpicker (e.g. #FF00FF)
- String newColor = server.arg("c");
- if (newColor.length() == 7) {
- color = newColor;
- changed = true;
- }
- // building a website
- static char temp[BUFFER_SZ];
- temp[0] = '\0';
- int sec = millis() / 1000;
- int min = sec / 60;
- int hr = min / 60;
- char clr [8];
- color.toCharArray(clr, 8);
- strcat(temp, "<!DOCTYPE html><html>\n");
- strcat(temp, "<head>\n");
- strcat(temp, " <title>My Christmas Tree Controller</title>\n");
- strcat(temp, " <style> body { background-color: #cccccc; font-family: Arial; Color: #008; } </style>\n");
- strcat(temp, " <meta name=\"viewport\" content=\"width=device-width, height=device-height, \n");
- strcat(temp, " initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0\" />\n");
- if (changed) {
- strcat(temp, " <meta http-equiv=\"refresh\" content=\"0; url=/\">\n");
- }
- strcat(temp, "</head>\n");
- strcat(temp, "<body>\n");
- strcat(temp, " <center>\n");
- strcat(temp, " <h1>My<br>Christmas Tree</h1>\n");
- strcatf(temp, " <p>Uptime: %02d:%02d:%02d</p>", hr, min % 60, sec % 60);
- strcat(temp, " </center>\n");
- strcat(temp, " <form action=\"/\" id=\"pick\" method=\"get\">\n");
- strcatf(temp, " <label><input type=\"radio\" name=\"m\" value=\"%d\"%s/> Solid [pick]</label><br>\n", SOLID, currentMode == SOLID ? " checked" : "");
- strcatf(temp, " <label><input type=\"radio\" name=\"m\" value=\"%d\"%s/> Solid Twinkle [pick]</label><br>\n", TWINKLE_SOLID, currentMode == TWINKLE_SOLID ? " checked" : "");
- strcatf(temp, " <label><input type=\"radio\" name=\"m\" value=\"%d\"%s/> Racing [pick]</label><br>\n", RACING, currentMode == RACING ? " checked" : "");
- strcatf(temp, " <label><input type=\"radio\" name=\"m\" value=\"%d\"%s/> Rainbow</label><br>\n", RAINBOW, currentMode == RAINBOW ? " checked" : "");
- strcatf(temp, " <label><input type=\"radio\" name=\"m\" value=\"%d\"%s/> Random</label><br>\n", RANDOM, currentMode == RANDOM ? " checked" : "");
- strcatf(temp, " <label><input type=\"radio\" name=\"m\" value=\"%d\"%s/> Random Twinkle</label><br>\n", TWINKLE_RANDOM, currentMode == TWINKLE_RANDOM ? " checked" : "");
- strcatf(temp, " <label><input type=\"radio\" name=\"m\" value=\"%d\"%s/> Cylon</label><br>\n", CYLON, currentMode == CYLON ? " checked" : "");
- strcatf(temp, " <label><input type=\"radio\" name=\"m\" value=\"%d\"%s/> R/G March</label><br>\n", XMAS_MARCH, currentMode == XMAS_MARCH ? " checked" : "");
- strcatf(temp, " <label><input type=\"radio\" name=\"m\" value=\"%d\"%s/> R/G Racing</label><br>\n", XMAS_RACING, currentMode == XMAS_RACING ? " checked" : "");
- strcatf(temp, " <label><input type=\"radio\" name=\"m\" value=\"%d\"%s/> R/G Sweep</label><br>\n", XMAS_SWEEP, currentMode == XMAS_SWEEP ? " checked" : "");
- strcatf(temp, " <label><input type=\"radio\" name=\"m\" value=\"%d\"%s/> R/G Twinkle</label><br>\n", XMAS_TWINKLE, currentMode == XMAS_TWINKLE ? " checked" : "");
- strcat(temp, " <center>\n");
- strcatf(temp, " <br><input type=\"color\" name=\"c\" value=\"%s\"/><br>\n", clr);
- strcat(temp, " </center>\n");
- strcat(temp, " </form>\n");
- strcat(temp, " <center>\n");
- strcat(temp, " <button type=\"submit\" form=\"pick\" value=\"X\">UPDATE</button>\n");
- strcat(temp, " </center>\n");
- strcat(temp, "</body>\n");
- strcat(temp, "</html>\n");
- server.send(200, "text/html", temp);
- switchStatus();
- }
- void strcatf(char * str, const char * format, ... ) {
- static char tmp[LINE_SZ];
- tmp[0] = '\0';
- va_list args;
- va_start (args, format);
- vsnprintf(tmp, LINE_SZ, format, args);
- va_end (args);
- strncat(str, tmp, LINE_SZ);
- }
- void handleNotFound(){
- switchStatus();
- String message = "File Not Found\n\n";
- message += "URI: ";
- message += server.uri();
- message += "\nMethod: ";
- message += (server.method() == HTTP_GET)?"GET":"POST";
- message += "\nArguments: ";
- message += server.args();
- message += "\n";
- for (uint8_t i=0; i<server.args(); i++){
- message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
- }
- server.send(404, "text/plain", message);
- switchStatus();
- }
- void setAll(String color) {
- // converting Hex to Int
- int number = (int) strtol(&color[1], NULL, 16);
- // splitting into three parts
- int r = number >> 16;
- int g = number >> 8 & 0xFF;
- int b = number & 0xFF;
- setAll(r, g, b);
- }
- void set(int pixel, String color) {
- // converting Hex to Int
- int number = (int) strtol(&color[1], NULL, 16);
- // splitting into three parts
- uint8_t r = number >> 16;
- uint8_t g = number >> 8 & 0xFF;
- uint8_t b = number & 0xFF;
- set(pixel, r, g, b);
- }
- void dim(int pixel, String color) {
- set(pixel, color);
- dim(pixel);
- }
- void setRandom(int pixel) {
- int number = random((int) strtol("FFFFFF", NULL, 16));
- // splitting into three parts
- set(pixel, max(number >> 16, RANDOM_BRIGHT), max(number >> 8 & 0xFF, RANDOM_BRIGHT), max(number & 0xFF, RANDOM_BRIGHT));
- }
- void setAll(uint8_t red, uint8_t green, uint8_t blue) {
- for (int i = 0; i < NUM_LEDS; i++) {
- set(i, red, green, blue);
- }
- }
- void set(int pixel, CHSV color) {
- set(pixel, CRGB(color));
- }
- void set(int pixel, uint8_t r, uint8_t g, uint8_t b) {
- set(pixel, CRGB(r, g, b));
- }
- void dim(int pixel, uint8_t r, uint8_t g, uint8_t b) {
- set(pixel, CRGB(r, g, b));
- dim(pixel);
- }
- void set(int pixel) {
- setX(pixel, CRGB(_red[pixel], _green[pixel], _blue[pixel]));
- }
- void dim(int pixel) {
- setX(pixel, CRGB(_red[pixel]/RACE_DIV, _green[pixel]/RACE_DIV, _blue[pixel]/RACE_DIV));
- }
- void dark(int pixel) {
- setX(pixel, CRGB(0, 0, 0));
- }
- void set(int pixel, CRGB color) {
- _red[pixel] = color.r;
- _green[pixel] = color.g;
- _blue[pixel] = color.b;
- setX(pixel, color);
- }
- void setX(int pixel, CRGB color) {
- _leds[pixel] = color;
- }
- void show() {
- FastLED.show(BRIGHTNESS);
- }
- void show(int brightness) {
- FastLED.setBrightness(brightness);
- FastLED.show();
- }
- /////////////////////////////////////////////////////////////////////////////
- // Modes/Patterns/Animations
- /////////////////////////////////////////////////////////////////////////////
- void cylon(unsigned long currentMillis, boolean reset) {
- static unsigned long previousMillis = 0;
- static int cycle = 1;
- static int i = -1;
- static uint8_t hue = 0;
- static int interval = 150;
- if (reset) {
- setAll(0, 0, 0);
- show();
- previousMillis = 0;
- cycle = 1;
- i = -1;
- hue = random((int) 255);;
- }
- if (currentMillis - previousMillis >= interval) {
- previousMillis = currentMillis;
- if (cycle == 1) {
- i++;
- if (i < NUM_LEDS) {
- set(i, CHSV(hue++, 255, 255));
- show();
- } else {
- cycle = 2;
- }
- }
- if (cycle == 2) {
- i--;
- if (i >= 0) {
- set(i, CHSV(hue++, 255, 255));
- show();
- } else {
- cycle = 1;
- }
- }
- }
- }
- void xmasSweep(unsigned long currentMillis, boolean reset) {
- static unsigned long previousMillis = 0;
- static int cycle = 1;
- static int i = -1;
- static int interval = 200;
- if (reset) {
- previousMillis = 0;
- cycle = 1;
- i = -1;
- }
- if (currentMillis - previousMillis >= interval) {
- previousMillis = currentMillis;
- if (cycle == 1) {
- i++;
- if (i < NUM_LEDS) {
- set(i, 255, 0, 0);
- show();
- } else {
- cycle = 2;
- }
- }
- if (cycle == 2) {
- i--;
- if (i >= 0) {
- set(i, 0, 255, 0);
- show();
- } else {
- cycle = 1;
- }
- }
- }
- }
- void twinkleSolid(unsigned long currentMillis, String newColor, boolean reset) {
- static unsigned long previousMillis = 0;
- static int i = -1;
- static int interval = 200;
- static boolean loading = true;
- static int blinker = -1;
- if (reset) {
- Serial.print("Setting LEDs(");
- Serial.print(newColor);
- Serial.print(")...");
- previousMillis = 0;
- i = -1;
- blinker = -1;
- loading = true;
- }
- if (loading) {
- if (currentMillis - previousMillis >= loadInterval) {
- previousMillis = currentMillis;
- i++;
- if (i < NUM_LEDS) {
- set(i, newColor);
- show();
- } else {
- Serial.println("Done.");
- loading = false;
- }
- }
- } else {
- if (blinker == -1 && currentMillis - previousMillis >= interval) {
- previousMillis = currentMillis;
- blinker = random((int) NUM_LEDS);
- dark(blinker);
- show();
- } else {
- if (blinker >= 0 && currentMillis - previousMillis >= interval) {
- previousMillis = currentMillis;
- set(blinker);
- show();
- blinker = -1;
- }
- }
- }
- }
- void xmasTwinkle(unsigned long currentMillis, boolean reset) {
- static unsigned long previousMillis = 0;
- static int i = -1;
- static int interval = 200;
- static boolean loading = true;
- static int blinker = -1;
- if (reset) {
- Serial.print("Setting LEDs(R/G)...");
- previousMillis = 0;
- i = -1;
- loading = true;
- }
- if (loading) {
- if (currentMillis - previousMillis >= loadInterval) {
- previousMillis = currentMillis;
- i++;
- if (i < NUM_LEDS) {
- set(i, 255 * ( i % 2 ), 255 * ( ( i + 1 ) % 2), 0);
- show();
- } else {
- Serial.println("Done.");
- loading = false;
- }
- }
- } else {
- if (blinker == -1 && currentMillis - previousMillis >= interval) {
- previousMillis = currentMillis;
- blinker = random((int) NUM_LEDS);
- dark(blinker);
- show();
- } else {
- if (blinker >= 0 && currentMillis - previousMillis >= interval) {
- previousMillis = currentMillis;
- set(blinker);
- show();
- blinker = -1;
- }
- }
- }
- }
- void twinkleRandom(unsigned long currentMillis, boolean reset) {
- static unsigned long previousMillis = 0;
- static int i = -1;
- static int interval = 200;
- static boolean loading = true;
- static int blinker = -1;
- if (reset) {
- Serial.println("Setting LEDs randomly...");
- previousMillis = 0;
- i = -1;
- loading = true;
- }
- if (loading) {
- if (currentMillis - previousMillis >= loadInterval) {
- previousMillis = currentMillis;
- i++;
- if (i < NUM_LEDS) {
- setRandom(i);
- show(RANDOM_BRIGHT);
- } else {
- Serial.println("Done.");
- loading = false;
- }
- }
- } else {
- if (blinker == -1 && currentMillis - previousMillis >= interval) {
- previousMillis = currentMillis;
- blinker = random((int) NUM_LEDS);
- dark(blinker);
- show(RANDOM_BRIGHT);
- } else {
- if (blinker >= 0 && currentMillis - previousMillis >= interval) {
- previousMillis = currentMillis;
- set(blinker);
- show(RANDOM_BRIGHT);
- blinker = -1;
- }
- }
- }
- }
- void racing(unsigned long currentMillis, String newColor, boolean reset) {
- static unsigned long previousMillis = 0;
- static int cycle = 1;
- static int i = -1;
- static int interval = 50;
- static boolean loading = true;
- if (reset) {
- Serial.print("Setting LEDs(");
- Serial.print(newColor);
- Serial.print(")...");
- previousMillis = 0;
- i = -1;
- loading = true;
- }
- if (loading) {
- if (currentMillis - previousMillis >= loadInterval) {
- previousMillis = currentMillis;
- i++;
- if (i < NUM_LEDS) {
- dim(i, newColor);
- show();
- } else {
- Serial.println("Done.");
- loading = false;
- }
- }
- } else {
- if (currentMillis - previousMillis >= interval) {
- previousMillis = currentMillis;
- if (cycle == 1) {
- i++;
- if (i < NUM_LEDS) {
- set(i);
- if (i > 0) {
- dim(i-1);
- }
- show();
- } else {
- cycle = 2;
- i--;
- }
- }
- if (cycle == 2) {
- i--;
- if (i >= 0) {
- set(i);
- if (i < (NUM_LEDS - 1)) {
- dim(i+1);
- }
- show();
- } else {
- cycle = 1;
- i++;
- }
- }
- }
- }
- }
- void xmasRacing(unsigned long currentMillis, boolean reset) {
- static unsigned long previousMillis = 0;
- static int cycle = 1;
- static int i = -1;
- static int interval = 100;
- static boolean loading = true;
- if (reset) {
- Serial.print("Setting LEDs(GREEN)...");
- // converting Hex to Int
- previousMillis = 0;
- i = -1;
- loading = true;
- }
- if (loading) {
- if (currentMillis - previousMillis >= loadInterval) {
- previousMillis = currentMillis;
- i++;
- if (i < NUM_LEDS) {
- set(i, 0, 255, 0);
- show();
- } else {
- Serial.println("Done.");
- loading = false;
- }
- }
- } else {
- if (currentMillis - previousMillis >= interval) {
- previousMillis = currentMillis;
- if (cycle == 1) {
- i++;
- if (i < NUM_LEDS) {
- set(i, 255, 0, 0);
- if (i > 0) {
- set(i-1, 0, 255, 0);
- }
- show();
- } else {
- cycle = 2;
- i--;
- }
- } else {
- if (cycle == 2) {
- i--;
- if (i >= 0) {
- set(i, 255, 0, 0);
- if (i < (NUM_LEDS - 1)) {
- set(i+1, 0, 255, 0);
- }
- show();
- } else {
- cycle = 1;
- i++;
- }
- }
- }
- }
- }
- }
- void randomColors(unsigned long currentMillis, boolean reset){
- static unsigned long previousMillis = 0;
- static int i = -1;
- static boolean loading = true;
- if (reset) {
- Serial.println("Setting LEDs randomly...");
- previousMillis = 0;
- i = -1;
- loading = true;
- }
- if (loading) {
- if (currentMillis - previousMillis >= loadInterval) {
- previousMillis = currentMillis;
- i++;
- if (i < NUM_LEDS) {
- setRandom(i);
- show(RANDOM_BRIGHT);
- } else {
- Serial.println("Done.");
- loading = false;
- }
- }
- }
- }
- void solidColor(unsigned long currentMillis, String newColor, boolean reset) {
- static unsigned long previousMillis = 0;
- static int i = -1;
- static boolean loading = true;
- static int r = 0;
- static int g = 0;
- static int b = 0;
- if (reset) {
- Serial.print("Setting LEDs(");
- Serial.print(newColor);
- Serial.print(")...");
- previousMillis = 0;
- i = -1;
- loading = true;
- }
- if (loading) {
- if (currentMillis - previousMillis >= loadInterval) {
- previousMillis = currentMillis;
- i++;
- if (i < NUM_LEDS) {
- set(i, newColor);
- show();
- } else {
- Serial.println("Done.");
- loading = false;
- }
- }
- }
- }
- void rainbow(unsigned long currentMillis, boolean reset) {
- static unsigned long previousMillis = 0;
- static int i = -1;
- static boolean loading = true;
- static uint8_t hue = 0;
- if (reset) {
- Serial.print("Setting LEDs(Rainbow)...");
- previousMillis = 0;
- i = -1;
- loading = true;
- hue = random((int) 255);;
- }
- if (loading) {
- if (currentMillis - previousMillis >= loadInterval) {
- previousMillis = currentMillis;
- i++;
- if (i < NUM_LEDS) {
- hue += (255 / NUM_LEDS);
- set(i, CHSV(hue, 255, 255));
- show();
- } else {
- Serial.println("Done.");
- loading = false;
- }
- }
- }
- }
- void xmasMarch(unsigned long currentMillis, boolean reset) {
- static unsigned long previousMillis = -1000;
- static int i = -1;
- static int interval = 1000;
- static boolean loading = true;
- static int cycle = 1;
- if (reset) {
- Serial.print("Setting LEDs(R/G)...");
- previousMillis = -1000;
- i = -1;
- loading = true;
- }
- if (loading) {
- if (currentMillis - previousMillis >= loadInterval) {
- previousMillis = currentMillis;
- i++;
- if (i < NUM_LEDS) {
- set(i, 255 * ( i % 2 ), 255 * ( ( i + 1 ) % 2 ), 0);
- show();
- } else {
- Serial.println("Done.");
- loading = false;
- }
- }
- } else {
- if (currentMillis - previousMillis >= interval) {
- previousMillis = currentMillis;
- if (cycle == 1) {
- for (int j = 0; j < NUM_LEDS; j++) {
- set(j, 255 * ( j % 2 ), 255 * ( ( j + 1 ) % 2 ), 0);
- show();
- cycle = 2;
- }
- } else {
- if (cycle == 2) {
- for (int j = 0; j < NUM_LEDS; j++) {
- set(j, 255 * ( ( j + 1 ) % 2 ), 255 * ( j % 2 ), 0);
- show();
- cycle = 1;
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement