Advertisement
Guest User

FastLed

a guest
Dec 14th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <FastLED.h>
  2.  
  3. FASTLED_USING_NAMESPACE
  4.  
  5. #include <ESP8266WiFi.h>
  6. #include <BlynkSimpleEsp8266.h>
  7.  
  8. // You should get Auth Token in the Blynk App.
  9. // Go to the Project Settings (nut icon).
  10. char auth[] = "3OfEectOBlaFy7KjFN885S3MeUcHHg5C";
  11.  
  12. // Your WiFi credentials.
  13. // Set password to "" for open networks.
  14. char ssid[] = "JAN";
  15. char pass[] = "janusz2610567";
  16. bool rot = true;
  17. int patN=0;
  18. // This function will be called every time Slider Widget
  19. // in Blynk app writes values to the Virtual Pin 1
  20. BLYNK_WRITE(V1)
  21. {
  22.   int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  23.   if (pinValue!=1) {
  24.     rot = false;
  25.     patN=pinValue-1;
  26.   }
  27.   else {
  28.     rot = true;
  29.   }
  30.   // You can also use:
  31.   // String i = param.asStr();
  32.   // double d = param.asDouble();
  33.   //Serial.print("V1 Slider value is: ");
  34.   //Serial.println(pinValue);
  35. }
  36.  
  37. BLYNK_WRITE(V2)
  38. {
  39.   int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  40.   FastLED.setBrightness(pinValue);
  41.   // You can also use:
  42.   // String i = param.asStr();
  43.   // double d = param.asDouble();
  44.   //Serial.print("V1 Slider value is: ");
  45.   //Serial.println(pinValue);
  46. }
  47. #if defined(FASTLED_VERSION) && (FASTLED_VERSION < 3001000)
  48.   #warning "Requires FastLED 3.1 or later; check github for latest code."
  49. #endif
  50.  
  51.  
  52.   //#define CLK_PIN   4
  53. #define LED_TYPE    WS2811
  54. #define COLOR_ORDER GRB
  55. #define NUM_LEDS    50
  56.   CRGB leds[2][NUM_LEDS];
  57.  
  58. #define BRIGHTNESS          100
  59. #define FRAMES_PER_SECOND  60
  60.  
  61. void setup() {
  62.   delay(3000); // 3 second delay for recovery
  63.  
  64.   // tell FastLED about the LED strip configuration
  65.   FastLED.addLeds<LED_TYPE, D5, COLOR_ORDER>(leds[0], NUM_LEDS).setCorrection(TypicalLEDStrip);
  66.   FastLED.addLeds<LED_TYPE, D6, COLOR_ORDER>(leds[1], NUM_LEDS).setCorrection(TypicalLEDStrip);
  67.   FastLED.setMaxRefreshRate( FRAMES_PER_SECOND );
  68.   // set master brightness control
  69.   FastLED.setBrightness(BRIGHTNESS);
  70.   //Serial.begin(9600);
  71.   Blynk.begin(auth, ssid, pass, IPAddress(192,168,55,106), 8080);
  72. }
  73.  
  74.  
  75. // List of patterns to cycle through.  Each is defined as a separate function below.
  76. typedef void(*SimplePatternList[])(int x);
  77. SimplePatternList gPatterns = { confettiTwoVer, breathing, sinelon, snakeTwo, snake, rainbow, rainbowWithGlitter, confetti, juggle, bpm};
  78.  
  79. uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
  80. uint8_t gHue = 0; // rotating "base color" used by many of the patterns
  81. uint8_t snakeN = 0;
  82.  
  83. void loop()
  84. {
  85.   if(rot){
  86.     gPatterns[gCurrentPatternNumber](0);
  87.     gPatterns[gCurrentPatternNumber](1);
  88.   } else
  89.   {
  90.     gPatterns[patN](0);
  91.     gPatterns[patN](1);
  92.   }
  93.  
  94.   // send the 'leds' array out to the actual LED strip
  95.   FastLED.show();
  96.   // insert a delay to keep the framerate modest
  97.   //FastLED.delay(1000 / FRAMES_PER_SECOND);
  98.  
  99.   // do some periodic updates
  100.   EVERY_N_MILLISECONDS(20) { gHue++;  
  101. } // slowly cycle the "base color" through the rainbow
  102.   EVERY_N_SECONDS(20) { nextPattern(); } // change patterns periodically
  103.   EVERY_N_MILLISECONDS(300) {  Blynk.run();}
  104. }
  105.  
  106. #define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
  107.  
  108. void nextPattern()
  109. {
  110.   // add one to the current pattern number, and wrap around at the end
  111.   gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE(gPatterns);
  112. }
  113.  
  114. void snake(int x) {
  115.   snakeN = beatsin16(15, 0, NUM_LEDS-1);
  116.   for (int i = 1; i <= snakeN; i++) {
  117.     leds[x][i] = CHSV(gHue, 255, 255);
  118.   }
  119.  
  120.   /*Serial.print("Pasek ");
  121.   Serial.print(x + 1);
  122.   Serial.print(" pos: ");
  123.   Serial.print(snakeN);
  124.   Serial.print("\n"); */
  125. }
  126.  
  127. void snakeTwo(int x) {
  128.   snakeN = beatsin16(15, 0, NUM_LEDS - 1);
  129.   for (int i = 0; i <= NUM_LEDS-1; i++) {
  130.     if (i < snakeN) {
  131.       leds[x][i] = CHSV(gHue, 255, 255);
  132.     }
  133.     else {
  134.       leds[x][i] = CRGB::Black;
  135.     }
  136.   }
  137. }
  138.  
  139. void confettiTwoVer(int x) {
  140.   fadeToBlackBy(leds[x], NUM_LEDS, 20);
  141.   int pos = (beatsin16(15, 0, NUM_LEDS-1) + (random16(16)-8));
  142.   if (pos >= NUM_LEDS-1) {
  143.     pos = NUM_LEDS-1;
  144.   }
  145.   if (pos < 0) {
  146.     pos = 0;
  147.   }
  148.  
  149.   leds[x][pos] += CHSV(gHue + random8(64), 200, 255);
  150. }
  151.  
  152. void breathing(int x) {
  153.   for (int i = 0; i < NUM_LEDS; i++) {
  154.     leds[x][i] = CHSV(gHue, 255, 255);
  155.     Serial.println(i);
  156.   }
  157.  
  158.   //fill_solid(leds[x], NUM_LEDS-1, gHue);
  159.   fadeLightBy(leds[x], NUM_LEDS, beatsin8(15, 0, 255));
  160.   /*Serial.print("Pasek ");
  161.   Serial.print(x + 1);
  162.   Serial.print(" pos: ");
  163.   Serial.print(snakeN);
  164.   Serial.print("\n");*/
  165. }
  166.  
  167. void rainbow(int x)
  168. {
  169.   // FastLED's built-in rainbow generator
  170.   fill_rainbow(leds[x], NUM_LEDS, gHue, 7);
  171. }
  172.  
  173. void rainbowWithGlitter(int x)
  174. {
  175.   // built-in FastLED rainbow, plus some random sparkly glitter
  176.   rainbow(x);
  177.   addGlitter(80, x);
  178. }
  179.  
  180. void addGlitter(fract8 chanceOfGlitter, int x)
  181. {
  182.   if (random8() < chanceOfGlitter) {
  183.     leds[x][random16(NUM_LEDS)] += CRGB::White;
  184.   }
  185. }
  186.  
  187. void confetti(int x)
  188. {
  189.   // random colored speckles that blink in and fade smoothly
  190.   fadeToBlackBy(leds[x], NUM_LEDS, 10);
  191.   int pos = random16(NUM_LEDS);
  192.   leds[x][pos] += CHSV(gHue + random8(64), 200, 255);
  193. }
  194.  
  195. void sinelon(int x)
  196. {
  197.   // a colored dot sweeping back and forth, with fading trails
  198.   fadeToBlackBy(leds[x], NUM_LEDS, 20);
  199.   int pos = beatsin16(7, 0, NUM_LEDS - 1);
  200.   leds[x][pos] += CHSV(gHue, 255, 192);
  201.   /*if (pos == 0) {
  202.     leds[x][0] += CRGB::White;
  203.   }*/
  204. }
  205.  
  206. void bpm(int x)
  207. {
  208.   // colored stripes pulsing at a defined Beats-Per-Minute (BPM)
  209.   uint8_t BeatsPerMinute = 62;
  210.   CRGBPalette16 palette = PartyColors_p;
  211.   uint8_t beat = beatsin8(BeatsPerMinute, 64, 255);
  212.   for (int i = 0; i < NUM_LEDS; i++) { //9948
  213.     leds[x][i] = ColorFromPalette(palette, gHue + (i * 2), beat - gHue + (i * 10));
  214.   }
  215. }
  216.  
  217. void juggle(int x) {
  218.   // eight colored dots, weaving in and out of sync with each other
  219.   fadeToBlackBy(leds[x], NUM_LEDS, 20);
  220.   byte dothue = 0;
  221.   for (int i = 0; i < 8; i++) {
  222.     leds[x][beatsin16(i + 7, 0, NUM_LEDS - 1)] |= CHSV(dothue, 200, 255);
  223.     dothue += 32;
  224.   }
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement