I am new to the Adafruit NeoPixel library and have a question about the “class NeoPatterns : public Adafruit_NeoPixel” class. Up to now I have just used the FastLED library and I don’t have any experience with C++ classes (I just programmed with python) .
I am using the latest version of the NeoPixel library and I got most of the code from this tutorial
(I just removed the button code and added a serial connection to a python skript).
So my class looks like:
// Pattern types supported:
enum pattern { NONE, Off, RAINBOW_CYCLE, THEATER_CHASE, COLOR_WIPE, SCANNER, FADE };
// Patern directions supported:
enum direction { FORWARD, REVERSE };
// NeoPattern Class - derived from the Adafruit_NeoPixel class
class NeoPatterns : public Adafruit_NeoPixel
{
public:
// Member Variables:
pattern ActivePattern; // which pattern is running
direction Direction; // direction to run the pattern
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
void (*OnComplete)(); // Callback on completion of pattern
// Constructor - calls base-class constructor to initialize strip
NeoPatterns(uint16_t pixels, uint8_t pin, uint8_t type, void (*callback)())
:Adafruit_NeoPixel(pixels, pin, type)
{
OnComplete = callback;
}
// Update the pattern
void Update()
{
if((millis() - lastUpdate) > Interval) // time to update
{
lastUpdate = millis();
switch(ActivePattern)
{
case RAINBOW_CYCLE:
RainbowCycleUpdate();
break;
case THEATER_CHASE:
TheaterChaseUpdate();
break;
case COLOR_WIPE:
ColorWipeUpdate();
break;
case SCANNER:
ScannerUpdate();
break;
case FADE:
FadeUpdate();
break;
default:
break;
}
}
}
// Increment the Index and reset at the end
void Increment()
{
if (Direction == FORWARD)
{
Index++;
if (Index >= TotalSteps)
{
Index = 0;
if (OnComplete != NULL)
{
OnComplete(); // call the comlpetion callback
}
}
}
else // Direction == REVERSE
{
--Index;
if (Index <= 0)
{
Index = TotalSteps-1;
if (OnComplete != NULL)
{
OnComplete(); // call the comlpetion callback
}
}
}
}
// Reverse pattern direction
void Reverse()
{
if (Direction == FORWARD)
{
Direction = REVERSE;
Index = TotalSteps-1;
}
else
{
Direction = FORWARD;
Index = 0;
}
}
// Initialize for a RainbowCycle
void RainbowCycle(uint8_t interval, direction dir = FORWARD)
{
ActivePattern = RAINBOW_CYCLE;
Interval = interval;
TotalSteps = 255;
Index = 0;
Direction = dir;
}
// Update the Rainbow Cycle Pattern
void RainbowCycleUpdate()
{
for(int i=0; i< numPixels(); i++)
{
setPixelColor(i, Wheel(((i * 256 / numPixels()) + Index) & 255));
}
show();
Increment();
}
(I have also added some other effects like the mentioned tutorial)
I have 3 LED strips added:
NeoPatterns Strip2(9, 5, NEO_GRB + NEO_KHZ800, &Strip2Complete);
NeoPatterns Strip3(9, 6, NEO_GRB + NEO_KHZ800, &Strip3Complete);
NeoPatterns Strip1(24, 3, NEO_GRB + NEO_KHZ800, &Strip1Complete);
And change the colors with this main loop:
void loop()
{
Strip1.Update();
Strip2.Update();
Strip3.Update();
if (Serial.available() > 0)
{
achievedata();
}
if (current_strip == 0)
{
if (mode == "1"){
Strip1.ActivePattern = RAINBOW_CYCLE;
Strip1.TotalSteps = 255;
Strip1.Interval = min(10, Strip3.Interval);
}
else if (mode == "2"){
Strip1.ActivePattern = THEATER_CHASE;
Strip1.Interval = 100;
}
else if (mode == "3"){
Strip1.ActivePattern = COLOR_WIPE;
Strip1.TotalSteps = Strip3.numPixels();
}
else if (mode == "4"){
Strip1.ActivePattern = SCANNER;
}
else if (mode == "5"){
Strip1.ActivePattern = FADE;
Strip1.Interval = 20;
}
Strip1.Interval = 100;
Strip1.show();
}
else if (current_strip == 1)
{
if (mode == "1"){
Strip2.ActivePattern = RAINBOW_CYCLE;
Strip2.TotalSteps = 255;
Strip2.Interval = min(10, Strip3.Interval);
}
else if (mode == "2"){
Strip2.ActivePattern = THEATER_CHASE;
Strip2.Interval = 100;
}
else if (mode == "3"){
Strip2.ActivePattern = COLOR_WIPE;
Strip2.TotalSteps = Strip3.numPixels();
}
else if (mode == "4"){
Strip2.ActivePattern = SCANNER;
}
else if (mode == "5"){
Strip2.ActivePattern = FADE;
Strip2.Interval = 20;
}
Strip2.TotalSteps = 255;
Strip2.Interval = min(10, Strip3.Interval);
Strip2.show();
}
else if (current_strip == 2)
{
if (mode == "1"){
Strip3.ActivePattern = RAINBOW_CYCLE;
Strip3.TotalSteps = 255;
Strip3.Interval = min(10, Strip3.Interval);
}
else if (mode == "2"){
Strip3.ActivePattern = THEATER_CHASE;
Strip3.Interval = 100;
}
else if (mode == "3"){
Strip3.ActivePattern = COLOR_WIPE;
Strip3.TotalSteps = Strip3.numPixels();
}
else if (mode == "4"){
Strip3.ActivePattern = SCANNER;
}
else if (mode == "5"){
Strip3.ActivePattern = FADE;
Strip3.Interval = 20;
}
Strip3.TotalSteps = 255;
Strip3.Interval = min(10, Strip3.Interval);
Strip3.show();
}
else // Back to normal operation
{
// Restore all pattern parameters to normal values
Strip1.ActivePattern = THEATER_CHASE;
Strip1.Interval = 100;
Strip2.ActivePattern = RAINBOW_CYCLE;
Strip2.TotalSteps = 255;
Strip2.Interval = min(10, Strip3.Interval);
}
}
(the variables “mode” and “current_strip” are from the function “achievedata”, which gets data from my python script)
But now my question: How can I add other modes (e.g. all LED’s off or in one color…) - and is it possible to add those to my class or do I need to solve my problem with an other class or function?
Thank you for your help