Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Adafruit_NeoPixel.h>
- #ifdef __AVR__
- #include <avr/power.h>
- #endif
- #include <ESP8266WiFi.h>
- #include <WiFiClient.h>
- #include <ESP8266WebServer.h>
- //arduino
- #define LEDPIN 21
- #define RANDPIN 20
- //nodemcu
- #define LEDPIN 2
- #define RANDPIN A0
- #define LEDCOUNT 165
- #define ROWCOUNT 15
- #define DECAY 12
- const char *ssid = "lantern";
- const char *password = "lantern";
- uint8_t rgbs_base[3*LEDCOUNT+6];
- uint8_t *rgbs = &rgbs_base[3];
- int16_t sway = 0;
- uint8_t scale = 0;
- ESP8266WebServer server(80);
- // Parameter 1 = number of pixels in strip
- // Parameter 2 = Arduino pin number (most are valid)
- // Parameter 3 = pixel type flags, add together as needed:
- // NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
- // NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
- // NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
- // NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
- // NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
- Adafruit_NeoPixel strip = Adafruit_NeoPixel(LEDCOUNT, LEDPIN, NEO_GRB + NEO_KHZ800);
- // IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
- // pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
- // and minimize distance between Arduino and first pixel. Avoid connecting
- // on a live circuit...if you must, connect GND first.
- void setup()
- {
- // if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
- pinMode(LEDPIN, OUTPUT);
- strip.begin();
- strip.show();
- uint16_t i;
- for(i=0;i<3*LEDCOUNT;i++)
- {
- rgbs[i] = 32;
- }
- delay(1000);
- Serial.begin(9600);
- Serial.println();
- WiFi.softAP(ssid, password);
- IPAddress apip = WiFi.softAPIP();
- Serial.print("visit: \n");
- Serial.println(apip);
- server.on("/", handleRoot);
- server.on("/LED1", handleLed1);
- server.on("/LED2", handleLed2);
- server.on("/LED3", handleLed3);
- server.on("/LED4", handleLed4);
- server.begin();
- Serial.println("HTTP server begunnered");
- }
- void loop()
- {
- uint8_t randint;
- uint16_t i;
- for(i=0;i<3*(LEDCOUNT-ROWCOUNT);i++)
- {
- if(i % ROWCOUNT == 0)
- sway = ((analogRead(RANDPIN) ^ micros()) & 15) - 8;
- if(sway == 0)
- rgbs[i] = rgbs[i+3*ROWCOUNT] * DECAY / 16;
- if(sway < 0)
- rgbs[i] = ((rgbs[i+3*ROWCOUNT]*(16+sway)) + (rgbs[i+3*ROWCOUNT-3]*-sway))/16 * DECAY / 16;
- if(sway > 0)
- rgbs[i] = ((rgbs[i+3*ROWCOUNT]*(16-sway)) + (rgbs[i+3*ROWCOUNT+3]*sway))/16 * DECAY / 16;
- }
- if(scale == 0)
- {
- for(i=3*(LEDCOUNT-ROWCOUNT);i<3*LEDCOUNT;i++)
- rgbs[i] = 0;
- }
- else
- {
- for(i=3*(LEDCOUNT-ROWCOUNT);i<3*LEDCOUNT;i++)
- {
- randint = (analogRead(RANDPIN) ^ micros()) & 1;
- if(randint && rgbs[i] > 0)
- rgbs[i] = rgbs[i]*scale/16;
- else if(~randint && rgbs[i] < 80)
- rgbs[i] += scale;
- }
- for(i=(LEDCOUNT-ROWCOUNT);i<LEDCOUNT;i++)
- {
- // turn green yellow
- if(rgbs[i*3+1] > rgbs[i*3])
- rgbs[i*3+1] = rgbs[i*3];
- }
- }
- rgbs[-3] = rgbs[0];
- rgbs[-2] = rgbs[1];
- rgbs[-1] = rgbs[2];
- rgbs[3*LEDCOUNT] = rgbs[3*LEDCOUNT-3];
- rgbs[3*LEDCOUNT+1] = rgbs[3*LEDCOUNT-2];
- rgbs[3*LEDCOUNT+2] = rgbs[3*LEDCOUNT-1];
- // Serial.print("rand ");
- // Serial.println(randint);
- for(i=0;i<LEDCOUNT;i++)
- {
- uint8_t r,g,b;
- r = scalecolor(rgbs[i*3]);
- g = scalecolor(rgbs[i*3+1]);
- b = scalecolor(rgbs[i*3+2]>>3);
- strip.setPixelColor(i, strip.Color(r,g,b));
- }
- strip.show();
- server.handleClient();
- delay(40);
- }
- uint8_t scalecolor(uint8_t color)
- {
- if(color > 79) color = 79;
- if(color > 63) color = (color-48)*8;
- else if(color > 47) color = (color-32)*4;
- else if(color > 31) color = (color-16)*2;
- return color;
- }
- void handleRoot() {
- response();
- }
- void handleLed1() {
- scale = 13;
- //digitalWrite(LED_BUILTIN, stateLED);
- response();
- }
- void handleLed2() {
- scale = 10;
- //digitalWrite(LED_BUILTIN, stateLED);
- response();
- }
- void handleLed3() {
- scale = 4;
- //digitalWrite(LED_BUILTIN, stateLED);
- response();
- }
- void handleLed4() {
- scale = 0;
- //digitalWrite(LED_BUILTIN, stateLED);
- response();
- }
- const String HtmlHtml = "<html><head>"
- "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" /></head>";
- const String HtmlHtmlClose = "</html>";
- const String HtmlTitle = "<h4>Magic Lantern</h4>";
- const String HtmlButtons =
- "<a href=\"LED1\"><button style=\"display: block; width: 100%; height:90px;\">1</button></a><br/>"
- "<a href=\"LED2\"><button style=\"display: block; width: 100%; height:90px;\">2</button></a><br/>"
- "<a href=\"LED3\"><button style=\"display: block; width: 100%; height:90px;\">3</button></a><br/>"
- "<a href=\"LED4\"><button style=\"display: block; width: 100%; height:90px;\">4</button></a><br/>";
- void response()
- {
- String htmlRes = HtmlHtml;
- htmlRes += HtmlTitle;
- htmlRes += HtmlButtons;
- htmlRes += HtmlHtmlClose;
- server.send(200, "text/html", htmlRes);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement