Advertisement
infinitron

Jason Coon's modified lighning

May 21st, 2016
761
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1.  
  2. void lightning2() {
  3. // The first "flash" in a bolt of lightning is the "leader." The leader
  4. // is usually duller and has a longer delay until the next flash. Subsequent
  5. // flashes, the "strokes," are brighter and happen at shorter intervals.
  6.  
  7. FastLED.clear();
  8.  
  9. ledstart = random8(NUM_LEDS); // Determine starting location of flash
  10. ledlen = random8(NUM_LEDS-ledstart); // Determine length of flash (not to go beyond NUM_LEDS-1)
  11.  
  12. const uint8_t frequency = 50; // controls the interval between strikes
  13. const uint8_t flashes = 12; // the upper limit of flashes per strike
  14.  
  15. static uint8_t dimmer = 1;
  16. static uint8_t flashCount = flashes;
  17. static uint8_t flashCounter = 0;
  18.  
  19. static uint32_t delayMillis = 0;
  20. static uint32_t delayStart = 0;
  21.  
  22. static bool flashing = true;
  23.  
  24. // bail, if we haven't waited long enough
  25. if (millis() < delayMillis + delayStart)
  26. return;
  27.  
  28. flashing = !flashing;
  29.  
  30. if (flashCounter >= flashCount) // if we've finished the current set of flashes, clear the display and wait a bit
  31. {
  32. flashCounter = 0;
  33. fill_solid(leds+ledstart,ledlen, CRGB::Black); // clear the display
  34. delayMillis = random8(frequency) * 130;
  35. delayStart = millis();
  36. return;
  37. }
  38.  
  39. if (flashCounter == 0) dimmer = 5; // the brightness of the leader is scaled down by a factor of 5
  40. else dimmer = random8(1, 3); // return strokes are brighter than the leader
  41.  
  42. if (flashing)
  43. {
  44. fill_solid(leds+ledstart, ledlen, CHSV(255, 0, 255 / dimmer));
  45. delayMillis = random8(4, 10);
  46. delayStart = millis();
  47. }
  48. else
  49. {
  50. fill_solid(leds+ledstart, ledlen, CRGB::Black); // clear the display
  51. delayMillis = 50 + random8(100);
  52. if (flashCount == 0) delayMillis += 150; // longer delay until next flash after the leader
  53. }
  54.  
  55. flashCounter++;
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement