Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*-----------------------------------------------------------------------------------------------------
- ESP8266 FastLED Webserver code by Jeurgen Bruegl aka Gyro Gearloose
- Access Point Web Server e.g.
- http://192.168.4.1 http://192.168.1.252 (depends on your network)
- Spaceship Menorah Project
- https://hackaday.io/project/10148-spaceship-menorah
- Using Adruino IDE with ESP8266 core (Boards manager), selected WeMos D1 Mini as the board.
- --------------------------------------------------------------------------------------*/
- // FastLED setup ---------- FastLED has to be declared BEFORE the Webserver ---------------------
- #define FASTLED_ESP8266_RAW_PIN_ORDER //maybe not needed, see https://github.com/FastLED/FastLED/wiki/ESP8266-notes
- #include "FastLED.h"
- FASTLED_USING_NAMESPACE
- #if defined(FASTLED_VERSION) && (FASTLED_VERSION < 3001000)
- #warning "Requires FastLED 3.1 or later; check github for latest code."
- #endif
- #define P9813_PIN D6
- #define CLOCK_PIN D7
- #define LED_TYPE P9813
- #define msg7RESET D8 //digital pin (no PWM)
- #define msg7Strobe D5 //digital pin (no PWM)
- #define COLOR_ORDER RGB
- #define NUM_LEDS 7
- CRGB leds[NUM_LEDS];
- //#define BRIGHTNESS 128
- int BRIGHTNESS = 128; // this is half brightness
- int new_BRIGHTNESS = 128; // shall be initially the same as brightness
- #define MILLI_AMPERE 15000 // IMPORTANT: set here the max milli-Amps of your power supply 5V 2A = 2000
- #define FRAMES_PER_SECOND 120 // here you can control the speed. With the Access Point / Web Server the
- // animations run a bit slower.
- int ledMode = 4; // this is the starting animation (Confetti)
- /*/ variables for colorpal_beat
- CRGBPalette16 currentPalette;
- CRGBPalette16 targetPalette;
- uint8_t maxChanges = 24;
- TBlendType currentBlending;
- *///end colorpal_beat variables
- // Select EITHER ACCESS-Point OR WEB SERVER setup
- /*
- // ACCESS-Point setup ------------------------------------------------------------------------------------------------------
- #include <ESP8266WiFi.h>
- // comes with Huzzah installation. Enter in Arduino settings:
- // http://arduino.esp8266.com/package_esp8266com_index.json
- const char* ssid = "ESP_FastLED_Access_Point";
- const char* password = ""; // set to "" for open access point w/o password; or any other pw (min length = 8 characters)
- unsigned long ulReqcount;
- // Create an instance of the server on Port 80
- WiFiServer server(80);
- //IPAddress apIP(192, 168, 1, 1); // if you want to configure another IP address
- void setup()
- {
- // setup globals
- ulReqcount=0;
- // prepare GPIO2 // not necessary for FastLED
- pinMode(2, OUTPUT);
- digitalWrite(2, 0);
- // start serial
- Serial.begin(9600);
- delay(1);
- // Acess Point (AP) mode
- WiFi.mode(WIFI_AP);
- // WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0)); // if you want to configure another IP address
- WiFi.softAP(ssid, password);
- server.begin();
- // end ACCESS-Point setup ---------------------------------------------------------------------------------------------------
- */
- // WEB SERVER setup ---------------------------------------------------------------------------------------------------------
- #include <ESP8266WiFi.h>
- // comes with Huzzah installation. Enter in Arduino settings:
- // http://arduino.esp8266.com/package_esp8266com_index.json
- const char* ssid = "...";
- const char* password = "...";
- unsigned long ulReqcount;
- unsigned long ulReconncount;
- WiFiServer server(80); // Create an instance of the server on Port 80
- ///////variables for matrix()
- // Palette definitions
- CRGBPalette16 currentPalette;
- CRGBPalette16 targetPalette;
- TBlendType currentBlending;
- void WiFiStart()
- {
- // Connect to WiFi network
- Serial.println();
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected");
- // Start the server
- server.begin();
- Serial.println("Server started");
- // Print the IP address
- Serial.println(WiFi.localIP());
- }
- void setup() {
- ulReqcount=0; // setup globals for Webserver
- ulReconncount=0;
- // prepare GPIO2 // not necessary for FastLED
- // pinMode(2, OUTPUT);
- // digitalWrite(2, 0);
- pinMode(msg7RESET, OUTPUT);
- pinMode(msg7Strobe, OUTPUT);
- // start serial
- Serial.begin(115200);
- delay(1);
- // inital connect
- WiFi.mode(WIFI_STA);
- WiFiStart();
- // end WEB SERVER setup -----------------------------------------------------------------------------------------------------
- // now the settings for FastLED
- delay(2000); // sanity delay for LEDs
- FastLED.addLeds<LED_TYPE,P9813_PIN,CLOCK_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(DirectSunlight); // for APA102 (Dotstar)
- //FastLED.addLeds<WS2812,WS2811_PIN,BGR>(dots, NUM_LEDS).setCorrection(DirectSunlight);
- ////////////setup variables for matrix()
- currentPalette = CRGBPalette16(CRGB::Black);
- targetPalette = RainbowColors_p; // Used for smooth transitioning.
- currentBlending = LINEARBLEND;
- }
- uint8_t gHue = 0; // rotating "base color" used by many of the patterns
- uint32_t ticker; //used with millis() to time effects
- void webserver() { /// complete web server (same for access point) ////////////////////////////////////////////////////////
- // Check if a client has connected
- WiFiClient client = server.available();
- if (!client)
- {
- return;
- }
- // Wait until the client sends some data
- Serial.println("new client");
- unsigned long ultimeout = millis()+250;
- while(!client.available() && (millis()<ultimeout) )
- {
- delay(1);
- }
- if(millis()>ultimeout)
- {
- Serial.println("client connection time-out!");
- return;
- }
- // Read the first line of the request
- String sRequest = client.readStringUntil('\r');
- Serial.println(sRequest);
- client.flush();
- // stop client, if request is empty
- if(sRequest=="")
- {
- Serial.println("empty request! - stopping client");
- client.stop();
- return;
- }
- // get path; end of path is either space or ?
- // Syntax is e.g. GET /?pin=MOTOR1STOP HTTP/1.1
- String sPath="",sParam="", sCmd="";
- String sGetstart="GET ";
- int iStart,iEndSpace,iEndQuest;
- iStart = sRequest.indexOf(sGetstart);
- // Serial.print("iStart ");
- // Serial.println(iStart);
- if (iStart>=0)
- {
- iStart+=+sGetstart.length();
- iEndSpace = sRequest.indexOf(" ",iStart);
- iEndQuest = sRequest.indexOf("?",iStart);
- if(iEndSpace>0)
- {
- if(iEndQuest>0)
- {
- // there are parameters
- sPath = sRequest.substring(iStart,iEndQuest);
- sParam = sRequest.substring(iEndQuest,iEndSpace);
- }
- else
- {
- // NO parameters
- sPath = sRequest.substring(iStart,iEndSpace);
- }
- }
- }
- //////////////////////////////////////////////////////////////////////////////////
- // output parameters to serial, you may connect e.g. an Arduino and react on it //
- //////////////////////////////////////////////////////////////////////////////////
- if(sParam.length()>0)
- {
- int iEqu=sParam.indexOf("=");
- if(iEqu>=0)
- {
- sCmd = sParam.substring(iEqu+1,sParam.length());
- Serial.print("We are in output Parameters, value is: ");
- Serial.println(sCmd);
- char carray[4]; // values 0..255 = 3 digits; array = digits + 1
- sCmd.toCharArray(carray, sizeof(carray)); // convert char to the array
- new_BRIGHTNESS = atoi(carray); // atoi() converts an ascii character array to an integer
- if (new_BRIGHTNESS == 0) {new_BRIGHTNESS = BRIGHTNESS; } // if something else is selected (no change in brightness)
- BRIGHTNESS = new_BRIGHTNESS; // works not this way
- FastLED.setBrightness(new_BRIGHTNESS); // that's how the new value is assigned
- Serial.print("new Brightness: ");
- Serial.println(new_BRIGHTNESS);
- }
- }
- //////////////////////////////
- // format the html response //
- //////////////////////////////
- String sResponse,sHeader;
- ///////////////////////////////
- // 404 for non-matching path //
- ///////////////////////////////
- if(sPath!="/")
- {
- sResponse="<html><head><title>404 Not Found</title></head><body><h1>Not Found</h1><p>The requested URL was not found on this server.</p></body></html>";
- sHeader = "HTTP/1.1 404 Not found\r\n";
- sHeader += "Content-Length: ";
- sHeader += sResponse.length();
- sHeader += "\r\n";
- sHeader += "Content-Type: text/html\r\n";
- sHeader += "Connection: close\r\n";
- sHeader += "\r\n";
- }
- //////////////////////////
- // format the html page //
- //////////////////////////
- else
- {
- ulReqcount++;
- sResponse = "<html><head><title>ESP_FastLED_Access_Point</title></head><body>";
- // sResponse += "<font color=\"#FFFFF0\"><body bgclror=\"#000000\">";
- sResponse += "<font color=\"#FFFFF0\"><body bgcolor=\"#151B54\">";
- sResponse += "<FONT SIZE=-1>";
- sResponse += "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=yes\">";
- sResponse += "<h1>ESP FastLED DemoReel 100<br>";
- sResponse += " Light Controller</h1>";
- /* this creates a list with ON / OFF buttons
- //   is a non-breaking space; moves next character over
- sResponse += "<p>Rainbow <a href=\"?pin=FUNCTION1ON\"><button>--ON--</button></a> <a href=\"?pin=FUNCTION1OFF\"><button>--OFF--</button></a><br>";
- sResponse += "<p>Rainbow Glitter<a href=\"?pin=FUNCTION2ON\"><button>--ON--</button></a> <a href=\"?pin=FUNCTION2OFF\"><button>--OFF--</button></a><br>";
- sResponse += "<p>Confetti <a href=\"?pin=FUNCTION3ON\"><button>--ON--</button></a> <a href=\"?pin=FUNCTION3OFF\"><button>--OFF--</button></a><br>";
- sResponse += "<p>Sinelon <a href=\"?pin=FUNCTION4ON\"><button>--ON--</button></a> <a href=\"?pin=FUNCTION4OFF\"><button>--OFF--</button></a><br>";
- sResponse += "<p>Juggle <a href=\"?pin=FUNCTION5ON\"><button>--ON--</button></a> <a href=\"?pin=FUNCTION5OFF\"><button>--OFF--</button></a></p>";
- sResponse += "<p>BPM <a href=\"?pin=FUNCTION6ON\"><button>--ON--</button></a> <a href=\"?pin=FUNCTION6OFF\"><button>--OFF--</button></a></p>";
- sResponse += "<p>Function 7 <a href=\"?pin=FUNCTION7ON\"><button>--ON--</button></a> <a href=\"?pin=FUNCTION7OFF\"><button>--OFF--</button></a></p><br>";
- */
- // This is a nice drop down menu
- sResponse += "<FONT SIZE=+1>";
- sResponse += "<form>";
- // sResponse += "Select Animation<br>";
- sResponse += "<p>Select:</p>";
- sResponse += "<select name=\"sCmd\" size=\"7\" >";
- sResponse += "<option value=\"FUNCTION1OFF\">All OFF</option>";
- sResponse += "<option value=\"FUNCTION1ON\"selected>Fast Circle</option>";
- sResponse += "<option value=\"FUNCTION2ON\">Rainbow Glitter</option>";
- sResponse += "<option value=\"FUNCTION3ON\">Confetti</option>";
- sResponse += "<option value=\"FUNCTION4ON\">Sinelon</option>";
- sResponse += "<option value=\"FUNCTION5ON\">Juggle</option>";
- sResponse += "<option value=\"FUNCTION6ON\">BPM</option>";
- sResponse += "<option value=\"FUNCTION7ON\">Fire 2012</option>";
- sResponse += "<option value=\"FUNCTION8ON\">Lightning</option>";
- sResponse += "<option value=\"FUNCTION9ON\">The Matrix</option>";
- sResponse += "<option value=\"FUNCTION10ON\">Stripes</option>";
- sResponse += "<option value=\"FUNCTION11ON\">Equalize</option>";
- sResponse += "<option value=\"FUNCTION12ON\">Sound 2 Light</option><br>";
- sResponse += "</select>";
- sResponse += "<br><br>";
- sResponse += "<input type= submit>";
- sResponse += "</form>";
- // sResponse += "<FONT SIZE=-1>";
- // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- // Slider this works, however I got http://192.168.4.1/sCmd?FUNCTION_200=80 and the page was not found
- // I needed to take the FUNCTION_200=80 apart and call only FUNCTION_200 and assign
- // the value (=80) in "react on parameters" (line 512) to new_BRIGHTNESS
- sResponse += "</p>";
- sResponse += "<form action=\"?sCmd\" >"; // ?sCmd forced the '?' at the right spot
- sResponse += "<BR>Brightness  "; // perhaps we can show here the current value
- sResponse += round(new_BRIGHTNESS /2.5); // this is just a scale depending on the max value; round for better readability
- sResponse += " %";
- sResponse += "<BR>";
- sResponse += "<input style=\"width:200px; height:50px\" type=\"range\" name=\"=FUNCTION_200\" id=\"cmd\" value=\""; // '=' in front of FUNCTION_200 forced the = at the right spot
- sResponse += BRIGHTNESS;
- sResponse += "\" min=10 max=250 step=10 onchange=\"showValue(points)\" />";
- sResponse += "<BR><BR>";
- sResponse += "<input type=\"submit\">";
- sResponse += "</form>";
- sResponse += "<p>";
- // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- sResponse += "<FONT SIZE=-1>";
- /////////////////////////
- // react on parameters //
- /////////////////////////
- if (sCmd.length()>0)
- {
- // write received command to html page
- // switch the animiation (based on your choice in the case statement (main loop)
- if(sCmd.indexOf("FUNCTION1ON")>=0)
- {
- Serial.println("1 ON");
- ledMode = 2;
- }
- else if(sCmd.indexOf("FUNCTION1OFF")>=0)
- {
- Serial.println("1 OFF");
- ledMode = 1;
- }
- if(sCmd.indexOf("FUNCTION2ON")>=0)
- {
- Serial.println("2 ON");
- ledMode = 3;
- }
- else if(sCmd.indexOf("FUNCTION2OFF")>=0)
- {
- Serial.println("2 OFF");
- ledMode = 1;
- }
- if(sCmd.indexOf("FUNCTION3ON")>=0)
- {
- Serial.println("3 ON");
- ledMode = 4;
- }
- else if(sCmd.indexOf("FUNCTION3OFF")>=0)
- {
- Serial.println("3 OFF");
- ledMode = 1;
- }
- if(sCmd.indexOf("FUNCTION4ON")>=0)
- {
- Serial.println("4 ON");
- ledMode = 5;
- }
- else if(sCmd.indexOf("FUNCTION4OFF")>=0)
- {
- Serial.println("4 OFF");
- ledMode = 1;
- }
- if(sCmd.indexOf("FUNCTION5ON")>=0)
- {
- Serial.println("5 ON");
- ledMode = 6;
- }
- else if(sCmd.indexOf("FUNCTION5OFF")>=0)
- {
- Serial.println("5 OFF");
- ledMode = 1;
- }
- if(sCmd.indexOf("FUNCTION6ON")>=0)
- {
- Serial.println("6 ON");
- ledMode = 7;
- }
- else if(sCmd.indexOf("FUNCTION6OFF")>=0)
- {
- Serial.println("6 OFF");
- ledMode = 1;
- }
- if(sCmd.indexOf("FUNCTION7ON")>=0)
- {
- Serial.println("7 ON");
- ledMode = 8;
- }
- else if(sCmd.indexOf("FUNCTION7OFF")>=0)
- {
- Serial.println("7 OFF");
- ledMode = 1;
- }
- if(sCmd.indexOf("FUNCTION8ON")>=0)
- {
- Serial.println("8 ON");
- ledMode = 9;
- }
- else if(sCmd.indexOf("FUNCTION8OFF")>=0)
- {
- Serial.println("8 OFF");
- ledMode = 1;
- }
- if(sCmd.indexOf("FUNCTION9ON")>=0)
- {
- Serial.println("9 ON");
- ledMode = 10;
- }
- else if(sCmd.indexOf("FUNCTION9OFF")>=0)
- {
- Serial.println("9 OFF");
- ledMode = 1;
- }
- if(sCmd.indexOf("FUNCTION10ON")>=0)
- {
- Serial.println("10 ON");
- ledMode = 11; //case 10: matrix()
- }
- else if(sCmd.indexOf("FUNCTION10OFF")>=0)
- {
- Serial.println("10 OFF");
- ledMode = 1;
- }
- if(sCmd.indexOf("FUNCTION11ON")>=0)
- {
- Serial.println("11 ON");
- ledMode = 12;
- }
- else if(sCmd.indexOf("FUNCTION11OFF")>=0)
- {
- Serial.println("11 OFF");
- ledMode = 1;
- }
- if(sCmd.indexOf("FUNCTION12ON")>=0)
- {
- Serial.println("12 ON");
- ledMode = 13;
- }
- else if(sCmd.indexOf("FUNCTION12OFF")>=0)
- {
- Serial.println("12 OFF");
- ledMode = 1;
- }
- //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- // oh well, I was a bit frustrated. Came up with the idea to make
- // 10 digits increments and let the URL (not) react on it.
- // However, I was able to assign a new_BRIGHTNESS value;
- // what after all serves the purpose. Maybe someone comes up with
- // a more ellegant way - HOPEFULLY
- // (more than 400 have downloaded my code but nobody felt the need
- // to help. wtf - this is my very first attempt on HTML !
- // Guys, I'm a simple electrician, so PLEASE help :( )
- // do not call a new page when the slider is moved, but assign the new value
- // to BRIGHTNESS (this is done in "output parameters to serial", line 314
- if(sCmd.indexOf("FUNCTION_200=20")>=0) { }
- if(sCmd.indexOf("FUNCTION_200=30")>=0) { }
- if(sCmd.indexOf("FUNCTION_200=40")>=0) { }
- if(sCmd.indexOf("FUNCTION_200=50")>=0) { }
- if(sCmd.indexOf("FUNCTION_200=60")>=0) { }
- if(sCmd.indexOf("FUNCTION_200=70")>=0) { }
- if(sCmd.indexOf("FUNCTION_200=80")>=0) { }
- if(sCmd.indexOf("FUNCTION_200=90")>=0) { }
- if(sCmd.indexOf("FUNCTION_200=100")>=0) { }
- if(sCmd.indexOf("FUNCTION_200=110")>=0) { }
- if(sCmd.indexOf("FUNCTION_200=120")>=0) { }
- if(sCmd.indexOf("FUNCTION_200=130")>=0) { }
- if(sCmd.indexOf("FUNCTION_200=140")>=0) { }
- if(sCmd.indexOf("FUNCTION_200=150")>=0) { }
- if(sCmd.indexOf("FUNCTION_200=160")>=0) { }
- if(sCmd.indexOf("FUNCTION_200=170")>=0) { }
- if(sCmd.indexOf("FUNCTION_200=180")>=0) { }
- if(sCmd.indexOf("FUNCTION_200=190")>=0) { }
- if(sCmd.indexOf("FUNCTION_200=200")>=0) { }
- if(sCmd.indexOf("FUNCTION_200=210")>=0) { }
- if(sCmd.indexOf("FUNCTION_200=220")>=0) { }
- if(sCmd.indexOf("FUNCTION_200=230")>=0) { }
- if(sCmd.indexOf("FUNCTION_200=240")>=0) { }
- if(sCmd.indexOf("FUNCTION_200=250")>=0) { }
- //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- } // end sCmd.length()>0
- sResponse += "<BR>";
- sResponse += "<BR>";
- sResponse += "<BR>";
- sResponse += "<FONT SIZE=-2>";
- sResponse += "<font color=\"#FFDE00\">";
- sResponse += "DemoReel 100 by Mark Kriegsman<BR>";
- sResponse += "MSGEQ7 integration by Garrett Durland<BR>";
- sResponse += "<font color=\"#FFFFF0\">";
- sResponse += "Gyro Gearloose Feb 2016<BR>";
- sResponse += "</body></html>";
- sHeader = "HTTP/1.1 200 OK\r\n";
- sHeader += "Content-Length: ";
- sHeader += sResponse.length();
- sHeader += "\r\n";
- sHeader += "Content-Type: text/html\r\n";
- sHeader += "Connection: close\r\n";
- sHeader += "\r\n";
- }
- client.print(sHeader); // Send the response to the client
- client.print(sResponse);
- client.stop(); // and stop the client
- Serial.println("Client disonnected");
- } // end of web server
- /// END of complete web server //////////////////////////////////////////////////////////////////
- // LED animations ###############################################################################
- void all_off() {
- fill_solid(leds, NUM_LEDS, CRGB::Black);
- // show_at_max_brightness_for_power();
- // delay_at_max_brightness_for_power(1000/FRAMES_PER_SECOND);
- FastLED.show();
- FastLED.delay(1000/FRAMES_PER_SECOND);
- }
- void rainbow()
- {
- // FastLED's built-in rainbow generator
- EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow
- fill_rainbow( leds, NUM_LEDS, gHue, 7);
- show_at_max_brightness_for_power();
- delay_at_max_brightness_for_power(1000/FRAMES_PER_SECOND);
- }
- void addGlitter( fract8 chanceOfGlitter)
- {
- if( random8() < chanceOfGlitter) {
- leds[ random16(NUM_LEDS) ] += CRGB::White;
- }
- }
- void rainbowWithGlitter()
- {
- // built-in FastLED rainbow, plus some random sparkly glitter
- rainbow();
- addGlitter(80);
- }
- void confetti()
- {
- // random colored speckles that blink in and fade smoothly
- fadeToBlackBy( leds, NUM_LEDS, 10);
- int pos = random16(NUM_LEDS);
- leds[pos] += CHSV( gHue + random8(64), 200, 255);
- show_at_max_brightness_for_power();
- delay_at_max_brightness_for_power(1000/FRAMES_PER_SECOND);
- }
- void sinelon()
- {
- // a colored dot sweeping back and forth, with fading trails
- fadeToBlackBy( leds, NUM_LEDS, 20);
- int pos = beatsin16(13,0,NUM_LEDS);
- leds[pos] += CHSV( gHue, 255, 192);
- show_at_max_brightness_for_power();
- delay_at_max_brightness_for_power(1000/FRAMES_PER_SECOND);
- }
- void bpm()
- {
- // colored stripes pulsing at a defined Beats-Per-Minute (BPM)
- uint8_t BeatsPerMinute = 62;
- CRGBPalette16 palette = PartyColors_p;
- uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
- for( int i = 0; i < NUM_LEDS; i++) { //9948
- leds[i] = ColorFromPalette(palette, gHue+(i*2), beat-gHue+(i*10));
- }
- show_at_max_brightness_for_power();
- delay_at_max_brightness_for_power(1000/FRAMES_PER_SECOND);
- }
- void juggle() {
- // eight colored dots, weaving in and out of sync with each other
- fadeToBlackBy( leds, NUM_LEDS, 80);
- byte dothue = 0;
- for( int i = 0; i < 5; i++) {
- leds[beatsin16(i+5,0,NUM_LEDS)] |= CHSV(dothue, 200, 255);
- dothue += 51;
- }
- show_at_max_brightness_for_power();
- delay_at_max_brightness_for_power(1000/FRAMES_PER_SECOND);
- }
- //########Constants for Fire2012#########
- // COOLING: How much does the air cool as it rises?
- // Less cooling = taller flames. More cooling = shorter flames.
- // Default 50, suggested range 20-100
- #define COOLING 55
- // SPARKING: What chance (out of 255) is there that a new spark will be lit?
- // Higher chance = more roaring fire. Lower chance = more flickery fire.
- // Default 120, suggested range 50-200.
- #define SPARKING 120
- bool gReverseDirection = false;
- void Fire2012()
- {
- // Array of temperature readings at each simulation cell
- static byte heat[NUM_LEDS];
- // Step 1. Cool down every cell a little
- for( int i = 0; i < NUM_LEDS; i++) {
- heat[i] = qsub8( heat[i], random8(0, ((COOLING * 10) / NUM_LEDS) + 2));
- }
- // Step 2. Heat from each cell drifts 'up' and diffuses a little
- for( int k= NUM_LEDS - 1; k >= 2; k--) {
- heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
- }
- // Step 3. Randomly ignite new 'sparks' of heat near the bottom
- if( random8() < SPARKING ) {
- int y = random8(7);
- heat[y] = qadd8( heat[y], random8(160,255) );
- }
- // Step 4. Map from heat cells to LED colors
- for( int j = 0; j < NUM_LEDS; j++) {
- CRGB color = HeatColor( heat[j]);
- int pixelnumber;
- if( gReverseDirection ) {
- pixelnumber = (NUM_LEDS-1) - j;
- } else {
- pixelnumber = j;
- }
- leds[pixelnumber] = color;
- }
- }
- // Initialize global variables for fastcirc_beat_g
- int thiscount = 0;
- int thishue = 0;
- int thisdir = 1;
- void fast_circ () {
- uint8_t beatval = beatsin8(10,5,50);
- thiscount = (thiscount + 1)%NUM_LEDS;
- leds[thiscount] = CHSV(thishue, 255, 255);
- fadeToBlackBy(leds, NUM_LEDS, 224);
- delay(beatval*2); // I tried to use EVERY_N_MILLISECONDS, but it didn't work for some reason.
- show_at_max_brightness_for_power();
- if (thishue >= 1024) thishue=0;
- thishue+=1;
- }
- // begin global variables for lightning()
- uint8_t frequency = 50; // controls the interval between strikes
- uint8_t flashes = 8; //the upper limit of flashes per strike
- unsigned int dimmer = 1;
- uint8_t ledstart; // Starting location of a flash
- uint8_t ledlen;
- void lightning() {
- for( int i = 0; i < NUM_LEDS; i++)
- leds[i]=0;
- ledstart = random16(NUM_LEDS); // Determine starting location of flash
- ledlen = random16(NUM_LEDS-ledstart); // Determine length of flash (not to go beyond NUM_LEDS-1)
- for (int flashCounter = 0; flashCounter < random8(3,flashes); flashCounter++) {
- if(flashCounter == 0) dimmer = 5; // the brightness of the leader is scaled down by a factor of 5
- else dimmer = random16(1,3); // return strokes are brighter than the leader
- fill_solid(leds+ledstart,ledlen,CHSV(255, 0, 255/dimmer));
- FastLED.show(); // Show a section of LED's
- delay(random8(4,10)); // each flash only lasts 4-10 milliseconds
- fill_solid(leds+ledstart,ledlen,CHSV(255,0,0)); // Clear the section of LED's
- FastLED.show();
- if (flashCounter == 0) delay (150); // longer delay until next flash after the leader
- delay(50+random8(100)); // shorter delay between strokes
- } // for()
- delay(random8(frequency)*100); // delay between strikes
- } // loop()
- ///////////////////////
- // Initialize global variables for matrix()
- int thisdelay = 50; // A delay value for the sequence(s)
- bool huerot = 0; // Does the hue rotate? 1 = yes
- uint8_t bgclr = 0;
- uint8_t bgbri = 0;
- //variable "thisdir" (for direction) defined earlier
- void matrix () {
- uint8_t secondHand = (millis() / 1000) % 25; // Change '25' to a different value to change length of the loop.
- static uint8_t lastSecond = 99; // Static variable, means it's only defined once. This is our 'debounce' variable.
- if (lastSecond != secondHand) { // Debounce to make sure we're not repeating an assignment.
- lastSecond = secondHand;
- switch(secondHand) {
- case 0: thisdelay=50; thishue=95; bgclr=140; bgbri=16; huerot=0; break;
- case 5: targetPalette = OceanColors_p; thisdir=1; bgbri=0; huerot=1; break;
- case 10: targetPalette = LavaColors_p; thisdir=0; thisdelay=30; thishue=0; bgclr=50; bgbri=15; huerot=0; break;
- case 15: thisdelay=80; bgbri = 32; bgclr=96; thishue=random8(); break;
- case 20: thishue=random8(); huerot=1; break;
- case 25: break;
- }
- }
- EVERY_N_MILLISECONDS(100) {
- uint8_t maxChanges = 24;
- nblendPaletteTowardPalette(currentPalette, targetPalette, maxChanges); // AWESOME palette blending capability.
- }
- EVERY_N_MILLISECONDS(thisdelay) {
- // matrix();
- if (huerot) thishue++;
- if (random16(90) > 80) {
- if (thisdir == 0) leds[0] = ColorFromPalette(currentPalette, thishue, 255, currentBlending); else leds[NUM_LEDS-1] = ColorFromPalette( currentPalette, thishue, 255, currentBlending);
- }
- else {
- if (thisdir ==0) leds[0] = CHSV(bgclr, 255, bgbri); else leds[NUM_LEDS-1] = CHSV(bgclr, 255, bgbri);
- }
- if (thisdir == 0) {
- for (int i = NUM_LEDS-1; i >0 ; i-- ) leds[i] = leds[i-1];
- } else {
- for (int i = 0; i < NUM_LEDS ; i++ ) leds[i] = leds[i+1];
- }
- }
- show_at_max_brightness_for_power();
- } // loop()
- void stripes()
- {
- static bool init = 1;
- if (init) {
- uint8_t stripe1=random8();
- uint8_t stripe2=stripe1+100;
- CRGB A = CHSV(stripe1,255,255);
- CRGB AB = CHSV(stripe1+30,230,220);
- CRGB B = CHSV(stripe2,255,255);
- CRGB BA = CHSV(stripe2-30,230,220);
- currentPalette = CRGBPalette16(
- A, A, A, A, AB, AB, B, B, B, B, B, B, B, B, BA, BA
- // A, A, A, A, A, A, AB, AB, B, B, B, B, B, B, BA, BA
- );
- init = 0;
- }
- EVERY_N_MILLISECONDS(100) {
- nblendPaletteTowardPalette(currentPalette, targetPalette, 24); // AWESOME palette blending capability.
- }
- EVERY_N_MILLISECONDS( 5000 )
- {
- uint8_t stripe1=random8();
- uint8_t stripe2=stripe1+100;
- CRGB A = CHSV(stripe1,255,255);
- CRGB AB = CHSV(stripe1+30,230,220);
- CRGB B = CHSV(stripe2,255,255);
- CRGB BA = CHSV(stripe2-30,230,220);
- targetPalette = CRGBPalette16(
- A, A, A, A, AB, AB, B, B, B, B, B, B, B, B, BA, BA
- // A, A, A, A, A, A, AB, AB, B, B, B, B, B, B, BA, BA
- );
- //setupStripedPalette( CHSV(stripe1,255,255), CHSV(stripe1+25,255,255), CHSV(stripe2,255,255), CHSV(stripe2-25,255,255));
- }
- static uint8_t startIndex = 0;
- startIndex = startIndex + 2; /* higher = faster motion */
- fill_palette( leds, NUM_LEDS,
- startIndex, 6, /* higher = narrower stripes */
- currentPalette, 255, LINEARBLEND);
- delay_at_max_brightness_for_power(1000/FRAMES_PER_SECOND);
- }
- /////global variables for MSGEQ7 sound reactive code///
- uint8_t MSGhue; //hue change variable that doesn't rely on every_n_milliseconds to change hue.
- int bands[7]; // there are 7 LEDs and 7 freq bands. This variable holds the smoothed data for use in the light dispalay
- int left[7]; //for raw (unsmoothed) audio data
- ////////////////
- void equalize(){
- digitalWrite(msg7RESET, HIGH); // reset the MSGEQ7's counter
- delay(5);
- digitalWrite(msg7RESET, LOW);
- for (int x = 0; x < 7; x++){
- digitalWrite(msg7Strobe, LOW); // output each DC value for each freq band
- delayMicroseconds(35); // to allow the output to settle
- int spectrumRead = analogRead(A0);//returns a value from 0 to 1023
- int MSGval = map(spectrumRead, 0, 512, 0, 255); // scale analogRead's value to Write's 255 max
- if (MSGval < 50) MSGval = MSGval / 2; // bit of a noise filter, so the LEDs turn off at low levels
- leds[x]=CHSV(MSGhue,255,MSGval);
- if (millis() - ticker > 100){
- MSGhue++; //increases hue vale every 100 milliseconds
- ticker = millis();
- }
- digitalWrite(msg7Strobe, HIGH);
- Serial.println(MSGval);
- }
- }
- void getBands(){
- digitalWrite(msg7RESET, HIGH); // reset the MSGEQ7's counter
- delay(5);
- digitalWrite(msg7RESET, LOW);
- for (int x = 0; x < 7; x++){
- digitalWrite(msg7Strobe, LOW); // output each DC value for each freq band
- delayMicroseconds(35); // to allow the output to settle
- int spectrumRead = analogRead(A0);
- left[x] = map(spectrumRead, 0, 512, 0, 255); // scale analogRead's value to Write's 255 max
- if (left[x] < 50)
- left[x] = left[x] / 2; // bit of a noise filter, so the LEDs turn off at low levels
- digitalWrite(msg7Strobe, HIGH);
- }
- }
- void animation22(){
- leds[3] = CRGB(bands[6], bands[5] / 3, bands[1] / 2);
- leds[3].fadeToBlackBy(bands[3] / 12);
- //move to the right
- //Idea for long strips: Have the sound impulse accelerate as it moves outward in each direction.
- for (int i = NUM_LEDS - 1; i > 3; i--) {
- leds[i] = leds[i - 1];
- }
- // move to the left
- for (int i = 0; i < 3; i++) {
- leds[i] = leds[i + 1];
- }
- FastLED.show();
- fade_down(72);
- }
- void sound_2_light(){
- getBands();
- soften_spectrum_data();
- animation22();
- }
- void fade_down(uint8_t value){
- for (int i = 0; i < NUM_LEDS; i++) leds[i].fadeToBlackBy(value);
- }
- void soften_spectrum_data(){
- for (byte i = 0; i < 7; i++)
- {
- uint8_t old = bands[i];
- uint16_t data = left[i] + old; //this is for a mono MSGEQ7 setup. Petrick wrote his animation for a stereo setup
- data = data / 2;
- bands[i] = data;
- }
- }
- void loop() {
- webserver();
- if (ledMode != 999) {
- switch (ledMode) {
- case 1: all_off(); break;
- case 2: fast_circ(); break;
- case 3: rainbowWithGlitter(); break;
- case 4: confetti(); break;
- case 5: sinelon(); break;
- case 6: juggle(); break;
- case 7: bpm(); break;
- case 8: Fire2012(); break;
- case 9: lightning(); break;
- case 10: matrix(); break;
- case 11: stripes(); break;
- case 12: equalize(); break;
- case 13: sound_2_light(); break;
- }
- }
- // show_at_max_brightness_for_power();
- // delay_at_max_brightness_for_power(1000/FRAMES_PER_SECOND);
- FastLED.show();
- if(ledMode != 12) FastLED.delay(1000/FRAMES_PER_SECOND);
- EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow
- } // end of loop ************************************************************************************************************
Advertisement
Add Comment
Please, Sign In to add comment