Created
March 5, 2021 21:38
-
-
Save barafael/ef51322535b3f1a14b31b8fb1e3e869a to your computer and use it in GitHub Desktop.
Random Blink
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <FastLED.h> | |
// How many leds are in the strip? | |
#define NUM_LEDS 144 | |
#define DATA_PIN_1 2 | |
#define DATA_PIN_2 4 | |
// This is an array of leds. One item for each led in your strip. | |
CRGB leds_1[NUM_LEDS]; | |
CRGB leds_2[NUM_LEDS]; | |
class State { | |
public: | |
size_t index = 0; | |
uint8_t brightness = 0; | |
uint8_t step_size = 10; | |
bool rising = true; | |
CHSV color; | |
void randomize() { | |
index = random8(NUM_LEDS); | |
brightness = random8(128); | |
step_size = random8(2, 12); | |
rising = true; | |
color = CHSV(random8(), random8(128, 255), random8()); | |
} | |
void advance() { | |
if ((uint32_t)brightness + step_size > 255) { | |
rising = false; | |
} | |
if (!rising && brightness < 30) { | |
leds_1[index] = CRGB::Black; | |
randomize(); | |
} | |
if (rising) { | |
brightness += step_size; | |
} else { | |
brightness -= step_size; | |
} | |
color.v = brightness; | |
leds_1[index] = color; | |
} | |
}; | |
#define NUM_STATES 8 | |
State states[NUM_STATES]; | |
// This function sets up the ledsand tells the controller about them | |
void setup() { | |
Serial.begin(9600); | |
// sanity check delay - allows reprogramming if accidently blowing power w/leds | |
delay(1000); | |
FastLED.addLeds<WS2811, DATA_PIN_1, GRB>(leds_1, NUM_LEDS); | |
FastLED.addLeds<WS2811, DATA_PIN_2, GRB>(leds_2, NUM_LEDS); | |
for (size_t i = 0; i < NUM_STATES; i++) { | |
states[i].randomize(); | |
} | |
} | |
void loop() { | |
EVERY_N_MILLISECONDS(20) { | |
FastLED.setBrightness(128); | |
for (size_t i = 0; i < NUM_STATES; i++) { | |
states[i].advance(); | |
} | |
FastLED.show(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment