Advertisement
Guest User

Untitled

a guest
Aug 16th, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.53 KB | None | 0 0
  1. #include <FastLED.h>
  2. #include <ESP8266WiFi.h>
  3. //#include <ArduinoOTA.h>
  4.  
  5. FASTLED_USING_NAMESPACE
  6.  
  7.  
  8. #if defined(FASTLED_VERSION) && (FASTLED_VERSION < 3001000)
  9. #warning "Requires FastLED 3.1 or later; check github for latest code."
  10. #endif
  11.  
  12. #define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
  13.  
  14. #define DATA_PIN    2
  15. //#define CLK_PIN   4
  16. #define LED_TYPE    WS2812B
  17. #define COLOR_ORDER GRB
  18. #define NUM_LEDS    96
  19. CRGB leds[NUM_LEDS];
  20.  
  21. int BRIGHTNESS = 0;
  22. #define FRAMES_PER_SECOND  120
  23.  
  24. const char *ssid = "Limettenporsche";
  25. const char *pass = "W!r3l355";
  26.  
  27. WiFiServer server(80);
  28.  
  29. void rainbow();
  30. void stat();
  31. void rainbowWithGlitter();
  32. void confetti();
  33. void sinelon();
  34. void juggle();
  35. void bpm();
  36. void nope();
  37. void noperb();
  38. void die();
  39. // List of patterns to cycle through.  Each is defined as a separate function below.
  40. typedef void (*SimplePatternList[])();
  41. SimplePatternList gPatterns = { rainbow, stat, rainbowWithGlitter, confetti, sinelon, juggle, bpm, nope, noperb, die};
  42.  
  43. uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
  44. uint8_t gHue = 0; // rotating "base color" used by many of the patterns
  45. int oneColor = 0;
  46.  
  47. void setup() {
  48.   delay(3000); // 3 second delay for recovery
  49.  
  50.   Serial.begin(115200);
  51.   Serial.println();
  52.   Serial.println("Configuring access point...");
  53.  
  54.   WiFi.mode(WIFI_AP_STA);
  55.   WiFi.softAP(ssid, pass);
  56.   IPAddress myIP = WiFi.softAPIP();
  57.  
  58.   Serial.print("AP IP Address: ");
  59.   Serial.println(myIP);
  60.  
  61.   server.begin();
  62.  
  63. //    ArduinoOTA.onStart([]() {
  64. //    String type;
  65. //    if (ArduinoOTA.getCommand() == U_FLASH)
  66. //      type = "sketch";
  67. //    else // U_SPIFFS
  68. //      type = "filesystem";
  69. //
  70. //    // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
  71. //    Serial.println("Start updating " + type);
  72. //  });
  73. //  ArduinoOTA.onEnd([]() {
  74. //    Serial.println("\nEnd");
  75. //  });
  76. //  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
  77. //    Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  78. //  });
  79. //  ArduinoOTA.onError([](ota_error_t error) {
  80. //    Serial.printf("Error[%u]: ", error);
  81. //    if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
  82. //    else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
  83. //    else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
  84. //    else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
  85. //    else if (error == OTA_END_ERROR) Serial.println("End Failed");
  86. //  });
  87. //  ArduinoOTA.begin();
  88.  
  89.   // tell FastLED about the LED strip configuration
  90.   FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  91.   //FastLED.addLeds<LED_TYPE,DATA_PIN,CLK_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  92.  
  93.   // set master brightness control
  94.   FastLED.setBrightness(BRIGHTNESS);
  95. }
  96.  
  97. void loop()
  98. {
  99.    //ArduinoOTA.handle();
  100.   // Call the current pattern function once, updating the 'leds' array
  101.   gPatterns[gCurrentPatternNumber]();
  102.  
  103.   // send the 'leds' array out to the actual LED strip
  104.   FastLED.show();
  105.   // insert a delay to keep the framerate modest
  106.  
  107.   // do some periodic updates
  108.   if(!oneColor){ {
  109.       gHue++;  // slowly cycle the "base color" through the rainbow
  110.     }
  111.   }
  112.  
  113.   WiFiClient client = server.available();
  114.   if (!client) {
  115.     delay(20);
  116.     return;
  117.   }
  118.   int i = 0;
  119.   int bytes = client.available();
  120.   while (!bytes) {
  121.     bytes = client.available();
  122.     i++;
  123.     delay(1);
  124.     if (i > 100) return; //timeout after 100 ms
  125.   }
  126.  
  127.  
  128.   char c;
  129.   c = char(client.read());
  130.   String line = "";
  131.   String req = "";
  132.   bytes--;
  133.   if (c == 'P') { //it's probably a post request, process it
  134.     while (bytes) {
  135.       //we get the last line of the request
  136.       c = char(client.read());
  137.       line += c;
  138.       if (c == '\n') {
  139.         Serial.println("Line was");
  140.         Serial.println(line);
  141.         req = line;
  142.         line = "";
  143.       }
  144.       bytes--;
  145.     }
  146.   }
  147.   int command = line.toInt();
  148.   Serial.println("\nGot Data:");
  149.   Serial.println(req);
  150.   Serial.println("\nGot Command");
  151.   Serial.println(command);
  152.   client.flush();
  153.  
  154.   if (command) {
  155.     gCurrentPatternNumber = (command - 1) % ARRAY_SIZE( gPatterns ); //the modulo is just for safety
  156.   }
  157.   else{
  158.     switch(line[0]){
  159.       case 'a':
  160.         Serial.println("toggling color-rainbow mode");
  161.         oneColor = 1-oneColor;
  162.         break;
  163.       case 'r':
  164.         gHue = 0; //red
  165.         break;
  166.       case 'g':
  167.         gHue = 96; //green
  168.         break;
  169.       case 'b':
  170.         gHue = 160; //blue
  171.         break;
  172.       case 'y':
  173.         gHue = 64; //yellow
  174.         break;
  175.       case 'v':
  176.         gHue = 200; //violet
  177.         break;
  178.       case 'c':
  179.         gHue = 128; //cyan
  180.         break;
  181.       case 'h':
  182.       BRIGHTNESS = 255;
  183.           FastLED.setBrightness(BRIGHTNESS);
  184.         break;
  185.       case 'm':
  186.       BRIGHTNESS = 127;
  187.           FastLED.setBrightness(BRIGHTNESS);
  188.         break;
  189.        case 'l':
  190.        BRIGHTNESS = 0;
  191.           FastLED.setBrightness(BRIGHTNESS);
  192.         break;
  193.     }
  194.   }
  195.  
  196.   //prepare a response
  197.   String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\n";
  198.   s += "<form action = '' method = 'post'>";
  199.   s += "<input type='submit' name='r' value='red' /><br />\n";
  200.   s += "<input type='submit' name='g' value='green' /><br />\n";
  201.   s += "<input type='submit' name='b' value='blue' /><br />\n";
  202.   s += "<input type='submit' name='y' value='yellow' /><br />\n";
  203.   s += "<input type='submit' name='v' value='violet' /><br />\n";
  204.   s += "<input type='submit' name='c' value='cyan' /><br />\n";
  205.   s += "<input type='submit' name='a' value='Toggle Rainbow Colors' /><br />\n";
  206.   s += "<input type='submit' name='1' value='Solid/Rainbow'/><br />\n";
  207.   s += "<input type='submit' name='2' value='Solid/Rainbow with Glitter'/><br />\n";
  208.   s += "<input type='submit' name='3' value='Confetti'/><br />\n";
  209.   s += "<input type='submit' name='4' value='Sinelon'/><br />\n";
  210.   s += "<input type='submit' name='5' value='Juggle'/><br />\n";
  211.   s += "<input type='submit' name='6' value='BPM'/><br />\n";
  212.   s += "<input type='submit' name='7' value='kill me'/><br />\n";
  213.   s += "<input type='submit' name='8' value='kill me US edition'/><br />\n";
  214.   s += "<input type='submit' name='9' value='kill others'/><br />\n";
  215.   s += "<input type='submit' name='h' value='On'/><br />\n";
  216.   s += "<input type='submit' name='m' value='Half'/><br />\n";
  217.   s += "<input type='submit' name='l' value='Off'/><br />\n";
  218.   s += "</form>";
  219.   s += "<p>Last Command Was ";
  220.   s += line;
  221.   s += "</p>\n";
  222.   s += "</html>";
  223.  
  224.   client.print(s);
  225.   delay(1);
  226.  
  227.  
  228.   //EVERY_N_SECONDS( 10 ) {
  229.   //  nextPattern();  // change patterns periodically
  230.   //}
  231. }
  232.  
  233.  
  234.  
  235. void nextPattern()
  236. {
  237.   // add one to the current pattern number, and wrap around at the end
  238.   gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE( gPatterns);
  239. }
  240.  
  241. void rainbow()
  242. {
  243.   if(oneColor){
  244.     fill_solid( leds, NUM_LEDS, CHSV(gHue, 200, 255));
  245.   }
  246.   else{
  247.     // FastLED's built-in rainbow generator
  248.     fill_rainbow( leds, NUM_LEDS, gHue, 7);
  249.   }
  250. }
  251.  
  252.  
  253. void rainbowWithGlitter()
  254. {
  255.   // built-in FastLED rainbow, plus some random sparkly glitter
  256.   rainbow();
  257.   addGlitter(80);
  258. }
  259.  
  260. void addGlitter( fract8 chanceOfGlitter)
  261. {
  262.   if ( random8() < chanceOfGlitter) {
  263.     leds[ random16(NUM_LEDS) ] += CRGB::White;
  264.   }
  265. }
  266.  
  267. void confetti()
  268. {
  269.   // random colored speckles that blink in and fade smoothly
  270.   fadeToBlackBy( leds, NUM_LEDS, 10);
  271.   int pos = random16(NUM_LEDS);
  272.   if(!oneColor){
  273.     leds[pos] += CHSV( gHue + random8(64), 200, 255);
  274.   }
  275.   else{
  276.     leds[pos] += CHSV( gHue, 200, 255);
  277.   }
  278. }
  279.  
  280. void sinelon()
  281. {
  282.   // a colored dot sweeping back and forth, with fading trails
  283.   fadeToBlackBy( leds, NUM_LEDS, 50);
  284.   int pos = beatsin88( 2600, 0, NUM_LEDS - 1 );
  285.   leds[pos] += CHSV( gHue, 255, 192);
  286. }
  287.  
  288. void bpm()
  289. {
  290.   // colored stripes pulsing at a defined Beats-Per-Minute (BPM)
  291.   uint8_t BeatsPerMinute = 120;
  292.   CRGBPalette16 palette = PartyColors_p;
  293.   uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
  294.   for ( int i = 0; i < NUM_LEDS; i++) { //9948
  295.     leds[i] = ColorFromPalette(palette, gHue + (i * 2), beat - gHue + (i * 10));
  296.   }
  297. }
  298.  
  299. void juggle() {
  300.   // eight colored dots, weaving in and out of sync with each other
  301.   fadeToBlackBy( leds, NUM_LEDS, 20);
  302.   byte dothue = gHue;
  303.   for ( int i = 0; i < 8; i++) {
  304.     leds[beatsin16( i + 7, 0, NUM_LEDS - 1 )] |= CHSV(dothue, 200, 255);
  305.     if(!oneColor)
  306.     {
  307.       dothue += 32;
  308.     }
  309.   }
  310. }
  311.  
  312. void nope()
  313. {
  314.   allblue();
  315.   delay(20);
  316.   allblack();
  317.   delay(40);
  318.   allblue();
  319.   delay(20);
  320.   allblack();
  321.   delay(200);
  322.   allblue();
  323.   delay(20);
  324.   allblack();
  325.   delay(40);
  326.   allblue();
  327.   delay(20);
  328.   allblack();
  329.   delay(600);
  330. }
  331.  
  332. void noperb()
  333. {
  334.   allblue();
  335.   delay(20);
  336.   allblack();
  337.   delay(40);
  338.   allblue();
  339.   delay(20);
  340.   allblack();
  341.   delay(200);
  342.   allred();
  343.   delay(20);
  344.   allblack();
  345.   delay(40);
  346.   allred();
  347.   delay(20);
  348.   allblack();
  349.   delay(600);
  350. }
  351. void die()
  352. {
  353.   allred();
  354.   delay(40);
  355.   allblue();
  356.   delay(40);
  357. }
  358.  
  359. void allblack()
  360. {
  361.   for (int dot = 0; dot < NUM_LEDS; dot++)
  362.   {
  363.     leds[dot] = CRGB::Black;
  364.   }
  365.   FastLED.show();
  366. }
  367.  
  368. void allblue()
  369. {
  370.   for (int dot = 0; dot < NUM_LEDS; dot++)
  371.   {
  372.     leds[dot] = CRGB::Blue;
  373.   }
  374.   FastLED.show();
  375. }
  376.  
  377. void allred()
  378. {
  379.   for (int dot = 0; dot < NUM_LEDS; dot++)
  380.   {
  381.     leds[dot] = CRGB::Red;
  382.   }
  383.   FastLED.show();
  384. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement