/*********************************************************
* LED Strip Demo
* Written by Andy Weese
* March 17, 2023
*
* This code is for a 100 LED strip that uses the WS2812 protocol.
* It implements the following features:
* 1. Slide colors across the strip from LED 0
* 2. Slide colors across the strip from the last LED
* 3. Sparkle random LEDs
*********************************************************/
//add libraries for nonblockng timing and LED control
#include <elapsedMillis.h>
#include <FastLED.h>
//assign LED pin and number of LEDs
const int LED_PIN = 7;
const int NUM_LEDS = 100;
//set initial state of various LED patterns
int slidestate = 0;
int revslidestate = 0;
int sparklestate = 0;
//set amount of time diffrent patterns will be held
unsigned long time_sparkle = 100;
unsigned long time_slide = 0;
unsigned long time_revslide = 0;
CRGB leds[NUM_LEDS]; // tells LED library number of LEDs on strip
elapsedMillis LEDms; // sets up timer for use in code
void setup() {
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
}
void loop() {
FastLED.setBrightness(50); // sets/resets LED brightness each loop
//call the color slide function multiple times in various colors if no other
//functions are active then marks the color slide functions has been called
//by changing its variable to 1
if (slidestate == 0 && revslidestate == 0 && sparklestate == 0){
colorslide(255,0,0,100);
colorslide(255,255,0,100);
colorslide(0,255,0,100);
colorslide(0,255,255,100);
colorslide(0,0,255,100);
slidestate = 1;
}
//call the reverse color slide function multiple times in various colors if slide
//done and no other functions are active then marks the reverse slide function
//has been called by changing its variable to 1
if (slidestate ==1 && revslidestate ==0 && sparklestate == 0){
revslide(0,0,255,100);
revslide(0,255,255,75);
revslide(0,255,0,50);
revslide(255,255,0,25);
revslide(255,0,0,100);
revslidestate = 1;
}
//call the sparkle function if slide and reverse slide are done then marks
//the sparkle function has been called by changing its variable to 1
if (slidestate == 1 && revslidestate == 1 && sparklestate == 0){
sparkle(255,255,153,25);
sparklestate = 1;
}
//resets values for functions if they have all been called once
slidestate = 0;
revslidestate = 0;
sparklestate = 0;
}
// the function for the color slide forwards down the strip
void colorslide(int redval, int greenval, int blueval, int lednum){
// creates for loop that lights up LEDs one at a time then procedes
// to the next LED until the total # of LEDs on the strip is reached
for (int i = 0; i < lednum; i++){
LEDms = 0;
while (LEDms <= time_slide){
leds[i] = CRGB(redval, greenval, blueval);
FastLED.show();
}
// resets LED timer each time while loop is exited and in the for loop lights led # that i is currently at
}
}
// the function for the colorslide backwards down the strip
void revslide(int redval, int greenval, int blueval, int lednum){
// creates for loop that starts by lighting up all LEDs then turning
//off one in each loop until none are on
for (int i = NUM_LEDS; i >= (NUM_LEDS-lednum); i--){
LEDms = 0;
while (LEDms <= time_slide){
leds[i] = CRGB(redval, greenval, blueval);
FastLED.show();
}
// resets LED timer each time while loop is exited and in the for loop lights LED # that i is currently at
}
}
// the function to sparkle various LEDs
void sparkle(int redval, int greenval, int blueval, int lednum){
fill_solid(leds, NUM_LEDS, CRGB(0, 0, 0)); // turns all the LEDs on the strip off
FastLED.show();
for (int k = 0; k< lednum; k++){
LEDms = 0;
int j = random(0,NUM_LEDS); // pick a random LED
while (LEDms <=time_sparkle){
leds[j] = CRGB(redval, greenval, blueval);
FastLED.show();
}
leds[j] =CRGB(0,0,0);
FastLED.show();
}
}