Skip to content

Instantly share code, notes, and snippets.

@jwhendy
Created November 4, 2015 04:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jwhendy/9febd1f4144179fa84b4 to your computer and use it in GitHub Desktop.
Save jwhendy/9febd1f4144179fa84b4 to your computer and use it in GitHub Desktop.
#include "FastLED.h"
// must use fastLED 3.1: https://github.com/FastLED/FastLED/tree/FastLED3.1
// fast led constants and initialize
#define DATA_PIN 9
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
#define NUM_LEDS 48
CRGB leds[NUM_LEDS];
int min_bright = 80;
int max_bright = 160;
int bright = 0;
int bpm = 6;
int hue = 0;
int sat = 0;
int delta = 0;
int lead_pixel;
int first_run;
int order[NUM_LEDS];
void setup()
{
delay(2000);
Serial.begin(9600);
// tell FastLED about the LED strip configuration
// FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip).setDither(bright < 255);
FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.setBrightness(max_bright);
first_run = 1;
}
void loop()
{
chase();
}
void chase()
{
// initialize a vector to store the LED numbers 0..NUM_LEDS - 1
if(first_run == 1)
{
for(int i = 0; i < NUM_LEDS; i++)
{
order[i] = i;
first_run = 0;
}
}
// oscillate the base hue, saturation, and hue delta for fill_gradient()
hue = beatsin8(bpm, 100, 200);
delta = beatsin8(5, 20, 80);
sat = beatsin8(7, 50, 200);
// the start color for fill_gradient will feature the values from beatsin8() above
CHSV start_col = CHSV(hue, sat, min_bright);
// the end color is shifted from the start color by some delta and brighter
CHSV end_col = CHSV(hue + delta, sat, max_bright);
// populate the strips by using fill_gradiet starting from the first pixel
// and transitioning to the end color halfway down the strip
// then transition backwards from the end color back to the start trip from
// halfway down the strip to the end
// the goal of this is to create a symmetrical pattern with the bright
// segmenet always at the center
fill_gradient(leds, 0, start_col, NUM_LEDS / 2, end_col, FORWARD_HUES);
fill_gradient(leds, NUM_LEDS / 2, end_col, NUM_LEDS - 1, start_col, BACKWARD_HUES);
EVERY_N_MILLISECONDS(20)
{
// I save the last array entry for the order array stored on first_run
int order_last = order[NUM_LEDS - 1];
// here I shift the order by one
for(int i = NUM_LEDS - 1; i > 0; i--)
{
order[i] = order[i - 1];
}
// the loop above stops after i = 1. order[0] needs to get the value
// of whatever the last value, order[NUM_LEDS - 1], hence storing it
// above
order[0] = order_last;
// Similarly, we save the value of the pixel named by the last entry in the order array
CRGB last_val = leds[order[NUM_LEDS - 1]];
// now, we take our baselin gradient and "shift" it according to the pixel order
// in our order array
// we just take the ith pixel, and give it the values of the pixel in order[i]
for(int i = NUM_LEDS - 1; i > 0; i--)
{
leds[i] = leds[order[i]] ;
}
// we call back the CRGB value we stored from the order[NUM_LEDS - 1]th pixel
leds[0] = last_val;
}
FastLED.show();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment