Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Adafruit_NeoPixel.h>
- // Pattern types supported:
- enum pattern { NONE, COLOR_WIPE, STROBE};
- // 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
- void Update()
- {
- if((millis() - lastUpdate) > Interval) // time to update
- {
- lastUpdate = millis();
- switch(ActivePattern)
- {
- case COLOR_WIPE:
- ColorWipeUpdate();
- break;
- default:
- break;
- }
- }
- }
- // 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;
- }
- // 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 ColorWipe
- void ColorWipe(uint32_t color, uint8_t interval, direction dir = FORWARD)
- {
- ActivePattern = COLOR_WIPE;
- Interval = interval;
- TotalSteps = numPixels();
- Color1 = color;
- Index = 0;
- Direction = dir;
- }
- // Update the Color Wipe Pattern
- void ColorWipeUpdate()
- {
- setPixelColor(Index, Color1);
- show();
- Increment();
- }
- void ColorSet(uint32_t color)
- {
- for (int i = 0; i < numPixels(); i++)
- {
- setPixelColor(i, color);
- }
- show();
- }
- // Returns the Red component of a 32-bit color
- uint8_t Red(uint32_t color)
- {
- return (color >> 16) & 0xFF;
- }
- // Returns the Green component of a 32-bit color
- uint8_t Green(uint32_t color)
- {
- return (color >> 8) & 0xFF;
- }
- // Returns the Blue component of a 32-bit color
- uint8_t Blue(uint32_t color)
- {
- return color & 0xFF;
- }
- // Input a value 0 to 255 to get a color value.
- // The colours are a transition r - g - b - back to r.
- uint32_t Wheel(byte WheelPos)
- {
- WheelPos = 255 - WheelPos;
- if(WheelPos < 85)
- {
- return Color(255 - WheelPos * 3, 0, WheelPos * 3);
- }
- else if(WheelPos < 170)
- {
- WheelPos -= 85;
- return Color(0, WheelPos * 3, 255 - WheelPos * 3);
- }
- else
- {
- WheelPos -= 170;
- return Color(WheelPos * 3, 255 - WheelPos * 3, 0);
- }
- }
- };
- void strip1Complete();
- void strip2Complete();
- // Define some NeoPatterns for the two strips
- // as well as some completion routines
- NeoPatterns strip1(300, 6, NEO_GRBW + NEO_KHZ800, &strip1Complete);
- NeoPatterns strip2(150, 5, NEO_GRB + NEO_KHZ800, &strip1Complete);
- // Initialize everything and prepare to start
- void setup()
- {
- // Initialize all the pixelStrips
- strip1.begin();
- strip2.begin();
- }
- unsigned long next = 1;
- boolean phase1_done = false;
- boolean phase2_done = false;
- unsigned long msec = 5; // next trigger time
- // Main loop
- void loop()
- {
- // Update the strips.
- strip1.Update();
- strip2.Update();
- if ((millis() - next) >= !phase1_done)
- {
- strip1.ActivePattern = COLOR_WIPE;
- strip2.ActivePattern = COLOR_WIPE;
- strip2.TotalSteps = strip2.numPixels();
- // lock out this phase
- phase1_done = true;
- phase2_done = true;
- msec = 99; // next phase
- }
- }
- //------------------------------------------------------------
- //Completion Routines - get called on completion of a pattern
- //------------------------------------------------------------
- // Strip1 Completion Callback
- void strip1Complete()
- {
- strip1.Color1 = strip1.Wheel(random(255));
- strip1.Interval = 20;
- }
- // Strip 2 Completion Callback
- void Strip2Complete()
- {
- strip2.Color1 = strip2.Wheel(random(255));
- strip2.Interval = 20;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement