Skip to content

Instantly share code, notes, and snippets.

@hsiboy
Last active October 25, 2023 10:08
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hsiboy/851404307232801cb79e to your computer and use it in GitHub Desktop.
Save hsiboy/851404307232801cb79e to your computer and use it in GitHub Desktop.
WS2811 - Lightning effect - FastLed
// stolen from https://github.com/fibonacci162
#include <FastLED.h>
#define LED_PIN 13 // hardware SPI pin SCK
#define NUM_LEDS 250
#define COLOR_ORDER RGB
#define LED_TYPE WS2811
#define MAX_BRIGHTNESS 255 // watch the power!
#define FPS 50
#define FLASHES 8
#define FREQUENCY 2 // delay between strikes
struct CRGB leds[NUM_LEDS];
unsigned int dimmer = 1;
void setup() {
delay(3000);
LEDS.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.setBrightness(MAX_BRIGHTNESS);
}
// 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.
void loop()
{
for (int flashCounter = 0; flashCounter < random8(3,FLASHES); flashCounter++)
{
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
FastLED.showColor(CHSV(255, 0, 255/dimmer));
delay(random8(4,10)); // each flash only lasts 4-10 milliseconds
FastLED.showColor(CHSV(255, 0, 0));
if (flashCounter == 0) delay (150); // longer delay until next flash after the leader
delay(50+random8(100)); // shorter delay between strikes
}
delay(random8(FREQUENCY)*100); // delay between strikes
}
@LakeLife
Copy link

Frequency not defined

@hsiboy
Copy link
Author

hsiboy commented Oct 20, 2019

You need to define FREQUENCY at the top of the file and set it to a value that produces the effect you want.

#define FREQUENCY 2

For example.

@Mrlonch
Copy link

Mrlonch commented Feb 4, 2021

Hi there,

Even if I define FREQUENCY it stills does not compile. Here is what I have:

#include <FastLED.h>

#define LED_PIN 4 // hardware SPI pin SCK
#define NUM_LEDS 5
#define COLOR_ORDER RGB
#define LED_TYPE WS2811
#define MAX_BRIGHTNESS 200 // watch the power!
#define FPS 50;
#define FLASHES 8;
#define FREQUENCY 2;

struct CRGB leds[NUM_LEDS];

unsigned int dimmer = 1;

void setup() {
delay(3000);
LEDS.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.setBrightness(MAX_BRIGHTNESS);
}

// 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.

void loop()
{
for (int flashCounter = 0; flashCounter < random8(3,FLASHES); flashCounter++)
{
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

FastLED.showColor(CHSV(255, 0, 255/dimmer));
delay(random8(4,10));                 // each flash only lasts 4-10 milliseconds
FastLED.showColor(CHSV(255, 0, 0));

if (flashCounter == 0) delay (150);   // longer delay until next flash after the leader
delay(50+random8(100));               // shorter delay between strokes  

}
delay(random8(FREQUENCY) * 100); // delay between strikes
}

Thanks in advance!

@hsiboy
Copy link
Author

hsiboy commented Feb 5, 2021

Hi, what is the error message at compile time?

@Stone6142
Copy link

Can i get a wiring diagram, I would love to do this and put it in my home be hided some cotton balls

@hsiboy
Copy link
Author

hsiboy commented Mar 29, 2023

Can i get a wiring diagram, I would love to do this and put it in my home be hided some cotton balls

As the code is written, it outputs the data on pin 13 of the Arduino.

So connect the data line of a string or tape of WS2811 LEDs to pin 13, connect the ground to the ground on the Arduino. Connect the positive of the LEDs to a suitable power supply and connect the Arduino to its own power supply.

Lots of tutorials for wiring "neo pixels" will show you how.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment