Advertisement
atuline

NPB_FastLED.ino

Mar 21st, 2021 (edited)
942
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.10 KB | None | 0 0
  1. /* FastLED and NeoPixelbus for ESP8266
  2.  *
  3.  * By: Andrew Tuline
  4.  *
  5.  * Date: March, 2021
  6.  *
  7.  * Use FastLED for the rich API and NeoPixelBus to drive the LED's on an ESP8266. This is how
  8.  * I believe that WLED drives the LED's on an ESP8266. It uses what is called the NPB's UART mode
  9.  * and is on pin D4 of the ESP8266. You need to use pin D4 here and to change it, you'll need
  10.  * to read up on the NeoPixelBus documentation at:
  11.  *
  12.  * https://github.com/Makuna/NeoPixelBus/wiki/ESP8266-NeoMethods
  13.  *
  14.  * The display routine uses pseudo random numbers to fade in and out a strand of LED's. Feel free
  15.  * to replace it.
  16.  *
  17.  */
  18.  
  19. #include <NeoPixelBus.h>
  20. #include <FastLED.h>
  21.  
  22. #define NUM_LEDS 30                           // No less than 4.
  23.  
  24. const uint8_t PixelPin = D4;                  // This line is actually ignored and is forced by the method chosen below.
  25. NeoPixelBus<NeoGrbFeature, NeoEsp8266Uart1800KbpsMethod> strip(NUM_LEDS, PixelPin);
  26.  
  27. uint32_t ledData[NUM_LEDS];                   // FastLED array, so we can refer to leds[i].
  28. CRGB *leds = (CRGB*) ledData;
  29.  
  30. CRGBPalette16 currentPalette = PartyColors_p; // FastLED palette.
  31.  
  32.    
  33. void setup() {
  34.     Serial.begin(115200);
  35.     delay(1000);
  36.     strip.Begin();                            // Reset all the WS2812's to an off state.
  37.     strip.Show();
  38. } // setup()
  39.  
  40.  
  41. void setPixels(CRGB* leds) {                                                // Display FastLED array onto NeoPixelBus.
  42.   for (int i=0; i<NUM_LEDS; i++) {                                          // We're just copying the array over.
  43.     RgbColor color = RgbColor(leds[i].red, leds[i].green, leds[i].blue);
  44.     strip.SetPixelColor(i, color);
  45.   }
  46.   strip.Show();                                                             // The actual NPB method to show the LED's.
  47. }  // setPixels()
  48.  
  49.  
  50. void loop() {
  51.   twinkleup();                                                              // Throw your own (preferably non-blocking) routine in here.
  52.   setPixels(leds);                                                          // This replaces FastLED.show();
  53. } // loop()
  54.  
  55.  
  56. void twinkleup() {
  57.   uint8_t intensity = 128;                                                  // 8-bit percentage of LED's lit.
  58.   random16_set_seed(535);                                                   // The randomizer needs to be re-set each time through the loop.
  59.   for (int i=0; i<NUM_LEDS; i++) {
  60.     uint8_t startVal = random8();                                           // The starting value (aka brightness) for each pixel.
  61.     uint8_t freq = random8(64, 192);                                        // The frequency of our sine wave for each LED. By Andrew Tuline.
  62.     uint8_t pixBri = sin8(startVal + 16 * millis()/(255-freq));             // Combined starting value with speed to come up with unique sine wave for each LED.
  63.     if (random8() > intensity) pixBri = 0;                                  // Reduce number of LED's based on intensity.
  64.     leds[i] = ColorFromPalette(currentPalette, random8()+millis()/100, pixBri, LINEARBLEND);   // Use a random colour that slowly changes over time.
  65.   }
  66. } // twinkleup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement