Advertisement
atuline

wledlightning.ino

Mar 17th, 2021 (edited)
3,128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.11 KB | None | 0 0
  1. /* wledlightning - Non-blocking lightning.
  2.  *
  3.  * Originally from WLED by unknown author possibly AirCoookie.
  4.  *
  5.  * Translated to FastLED by: Andrew Tuline
  6.  *
  7.  * Date: March, 2021
  8.  *
  9.  * This non-blocking routine lets you make an LED strip look like a 1D cloud of lightning.
  10.  *
  11.  *
  12.  */
  13.  
  14. #include <FastLED.h>
  15.  
  16. #define LED_DT 12                                             // Data pin to connect to the strip.
  17. #define COLOR_ORDER GRB                                       // It's GRB for WS2812 and BGR for APA102.
  18. #define LED_TYPE WS2812                                       // Using APA102, WS2812, WS2801. Don't forget to change LEDS.addLeds.
  19. #define NUM_LEDS 30                                           // Number of LED's.
  20.  
  21. uint8_t max_bright = 255;                                      // Overall brightness definition. It can be changed on the fly.
  22.  
  23. struct CRGB leds[NUM_LEDS];                                   // Initialize our LED array.
  24.  
  25. uint8_t speedy = 192;                                         // Low value = greater delay, up to 255 for no delay.
  26. uint8_t intensity = 192;                                      // Higher value = more flashes per strike.
  27.  
  28.  
  29.  
  30. void setup() {
  31.   Serial.begin(115200);
  32.   delay(1000);
  33.   LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS);
  34.   FastLED.setBrightness(max_bright);  
  35. } // setup()
  36.  
  37.  
  38.  
  39. void loop() {
  40.   wledlightning();
  41.   FastLED.show();
  42. } // loop()
  43.  
  44.  
  45.  
  46. void wledlightning(){
  47.  
  48.   uint16_t ledstart = random16(NUM_LEDS);                     // Determine starting location of flash.
  49.   uint16_t ledlen = 1 + random16(NUM_LEDS - ledstart);        // Determine length of flash.
  50.   uint8_t bri = 255/random8(1, 3);                            // Brightness.
  51.   static long step;
  52.  
  53.   static uint16_t segDelay;                                   // Delay between strikes (was SEGMENT.aux0).
  54.   static uint16_t numFlash = 0;                               // Flash stepper (was SEGMENT.aux1).
  55.  
  56.   if (numFlash == 0)                                          // Initialize the leader flash.
  57.   {
  58.     numFlash = random8(4, 4 + intensity/20);                  // Number of flashes.
  59.     numFlash *= 2;
  60.     bri = 52;                                                 // Leader has lower brightness.
  61.     segDelay = 200;                                           // 200ms delay after leader.
  62.   }
  63.  
  64.   fill_solid(leds,NUM_LEDS,0);                                // Fill the led's with black
  65.  
  66.   if (numFlash > 3 && !(numFlash & 0x01))                     // Flash on even number > 2.
  67.   {                      
  68.     for (int i = ledstart; i < ledstart + ledlen; i++)
  69.     {
  70.       leds[i] = CRGB::Gray;                                   // Fill ledstart to ledstart+ledlen with gray.
  71.     }
  72.     numFlash--;
  73.     step = millis();
  74.   } else {
  75.     if (millis() - step > segDelay)
  76.     {
  77.       numFlash--;
  78.       if (numFlash < 2) numFlash = 0;
  79.       segDelay = (50 + random8(100));                               // Delay between flashes.
  80.       if (numFlash == 2) segDelay = (random8(255 - speedy) * 100);  // Delay between strikes.
  81.       step = millis();
  82.     }
  83.   }
  84. } // wledlightning()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement