Skip to content

Instantly share code, notes, and snippets.

Created June 21, 2015 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/14d6a36014656ec5b78a to your computer and use it in GitHub Desktop.
Save anonymous/14d6a36014656ec5b78a to your computer and use it in GitHub Desktop.
FastLED Multiple Strips ClassPattern Example
#include "FastLED.h"
// Pattern types supported:
enum pattern { NONE, RAINBOW_CYCLE, THEATER_CHASE, COLOR_WIPE, SCANNER, FADE };
// Patern directions supported:
enum direction { FORWARD, REVERSE };
#define NUM_LEDS 48
#define NUM_ROWS 8
#define NUM_LEDSPERROW 6
CRGB leds[NUM_ROWS][NUM_LEDSPERROW];
// NeoPattern Class - derived from the Adafruit_NeoPixel class
class TreePatterns
{
public:
// Member Variables:
pattern ActivePattern; // which pattern is running
direction Direction; // direction to run the pattern
uint16_t Row; // current row/branch to work on
uint16_t RowSize; //totan length of row
unsigned long Interval; // milliseconds between updates
unsigned long lastUpdate; // last update of position
uint32_t Color1, Color2; // What colors are in use
uint16_t TotalSteps; // total number of steps in the pattern
uint16_t Index; // current step within the pattern
// Constructor - calls base-class constructor to initialize strip
TreePatterns(uint16_t row, uint16_t rownumleds)
{
Row = row-1;
RowSize = rownumleds;
}
// Update the pattern
void Update()
{
if((millis() - lastUpdate) > Interval) // time to update
{
lastUpdate = millis();
ColorWipeUpdate();
}
}
// Increment the Index and reset at the end
void Increment()
{
if (Direction == FORWARD)
{
Index++;
if (Index >= TotalSteps)
{
Index = 0;
}
}
else // Direction == REVERSE
{
--Index;
if (Index <= 0)
{
Index = TotalSteps-1;
}
}
}
// Reverse pattern direction
void Reverse()
{
if (Direction == FORWARD)
{
Direction = REVERSE;
Index = TotalSteps-1;
}
else
{
Direction = FORWARD;
Index = 0;
}
}
// Initialize for a ColorWipe
void ColorWipe(uint32_t color, uint16_t interval, direction dir = FORWARD)
{
ActivePattern = COLOR_WIPE;
Interval = interval;
TotalSteps = RowSize;
Color1 = color;
Index = 0;
Direction = dir;
}
// Update the Color Wipe Pattern
void ColorWipeUpdate()
{
leds[Row][Index] = CRGB::Red;
FastLED.show();
Increment();
}
};
TreePatterns Branch1(1, 1);
TreePatterns Branch2(2, 2);
TreePatterns Branch3(3, 3);
TreePatterns Branch4(4, 4);
TreePatterns Branch5(5, 5);
TreePatterns Branch6(6, 6);
// Initialize everything and prepare to start
void setup()
{
Serial.begin(115200);
FastLED.addLeds<WS2812B, 2, GRB>(leds[0], NUM_LEDSPERROW);
FastLED.addLeds<WS2812B, 14, GRB>(leds[1], NUM_LEDSPERROW);
FastLED.addLeds<WS2812B, 7, GRB>(leds[2], NUM_LEDSPERROW);
FastLED.addLeds<WS2812B, 20, GRB>(leds[3], NUM_LEDSPERROW);
FastLED.addLeds<WS2812B, 6, GRB>(leds[4], NUM_LEDSPERROW);
FastLED.addLeds<WS2812B, 9, GRB>(leds[5], NUM_LEDSPERROW);
FastLED.addLeds<WS2812B, 21, GRB>(leds[6], NUM_LEDSPERROW);
FastLED.addLeds<WS2812B, 5, GRB>(leds[7], NUM_LEDSPERROW);
// Kick off a pattern
Branch1.ColorWipe(255, 10000);
Branch2.ColorWipe(255, 5000);
Branch3.ColorWipe(255, 2000);
Branch4.ColorWipe(255, 1000);
Branch5.ColorWipe(255, 500);
Branch6.ColorWipe(255, 100);
}
// Main loop
void loop()
{
// Update the rings.
Branch1.Update();
Branch2.Update();
Branch3.Update();
Branch4.Update();
Branch5.Update();
Branch6.Update();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment