Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void lightning2() {
- // The first "flash" in a bolt of lightning is the "leader." The leader
- // is usually duller and has a longer delay until the next flash. Subsequent
- // flashes, the "strokes," are brighter and happen at shorter intervals.
- FastLED.clear();
- ledstart = random8(NUM_LEDS); // Determine starting location of flash
- ledlen = random8(NUM_LEDS-ledstart); // Determine length of flash (not to go beyond NUM_LEDS-1)
- const uint8_t frequency = 50; // controls the interval between strikes
- const uint8_t flashes = 12; // the upper limit of flashes per strike
- static uint8_t dimmer = 1;
- static uint8_t flashCount = flashes;
- static uint8_t flashCounter = 0;
- static uint32_t delayMillis = 0;
- static uint32_t delayStart = 0;
- static bool flashing = true;
- // bail, if we haven't waited long enough
- if (millis() < delayMillis + delayStart)
- return;
- flashing = !flashing;
- if (flashCounter >= flashCount) // if we've finished the current set of flashes, clear the display and wait a bit
- {
- flashCounter = 0;
- fill_solid(leds+ledstart,ledlen, CRGB::Black); // clear the display
- delayMillis = random8(frequency) * 130;
- delayStart = millis();
- return;
- }
- if (flashCounter == 0) dimmer = 5; // the brightness of the leader is scaled down by a factor of 5
- else dimmer = random8(1, 3); // return strokes are brighter than the leader
- if (flashing)
- {
- fill_solid(leds+ledstart, ledlen, CHSV(255, 0, 255 / dimmer));
- delayMillis = random8(4, 10);
- delayStart = millis();
- }
- else
- {
- fill_solid(leds+ledstart, ledlen, CRGB::Black); // clear the display
- delayMillis = 50 + random8(100);
- if (flashCount == 0) delayMillis += 150; // longer delay until next flash after the leader
- }
- flashCounter++;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement