Advertisement
Guest User

FastLED-MultiStringArrayIssue

a guest
Jun 22nd, 2015
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.27 KB | None | 0 0
  1. #include "FastLED.h"
  2.  
  3. // Pattern types supported:
  4. enum  pattern { NONE, RAINBOW_CYCLE, THEATER_CHASE, COLOR_WIPE, COLOR_BOUNCE, SCANNER, FADE };
  5. // Patern directions supported:
  6. enum  direction { FORWARD, REVERSE };
  7.  
  8. #define NUM_LEDS 160
  9. #define NUM_ROWS 8
  10. #define NUM_LEDSPERROW 20
  11. CRGB leds[NUM_ROWS][NUM_LEDSPERROW];
  12.  
  13.  
  14. // NeoPattern Class - derived from the Adafruit_NeoPixel class
  15. class TreePatterns
  16. {
  17.     public:
  18.  
  19.     // Member Variables:
  20.     pattern  ActivePattern;  // which pattern is running
  21.     direction Direction;     // direction to run the pattern
  22.  
  23.     uint16_t Row;    // current row/branch to work on
  24.     uint16_t RowSize;   //totan length of row
  25.  
  26.     unsigned long Interval;   // milliseconds between updates
  27.     unsigned long lastUpdate; // last update of position
  28.  
  29.     uint32_t Color1, Color2;  // What colors are in use
  30.     uint16_t TotalSteps;  // total number of steps in the pattern
  31.     uint16_t Index;  // current step within the pattern
  32.  
  33.  
  34.     // Constructor - calls base-class constructor to initialize strip
  35.     TreePatterns(uint16_t row, uint16_t rownumleds)
  36.     {
  37.         Row = row-1;
  38.         RowSize = rownumleds;
  39.     }
  40.  
  41.  
  42.     // Update the pattern
  43.     void Update()
  44.     {
  45.         if((millis() - lastUpdate) > Interval) // time to update
  46.         {
  47.             lastUpdate = millis();
  48.             switch(ActivePattern)
  49.             {
  50.                 case RAINBOW_CYCLE:
  51.                     //RainbowCycleUpdate();
  52.                     break;
  53.                 case THEATER_CHASE:
  54.                     //TheaterChaseUpdate();
  55.                     break;
  56.                 case COLOR_WIPE:
  57.                     ColorWipeUpdate();
  58.                     break;
  59.                 case COLOR_BOUNCE:
  60.                     ColorBounceUpdate();
  61.                     break;
  62.                 case FADE:
  63.                     //FadeUpdate();
  64.                     break;
  65.                 default:
  66.                     break;
  67.             }
  68.             //ColorWipeUpdate();
  69.             //ColorBounceUpdate();
  70.             //FastLED.show();
  71.         }
  72.     }
  73.  
  74.     // Increment the Index and reset at the end
  75.     void Increment()
  76.     {
  77.         if (Direction == FORWARD)
  78.         {
  79.            Index++;
  80.            if (Index >= TotalSteps)
  81.             {
  82.                 Index = 0;
  83.             }
  84.         }
  85.         else // Direction == REVERSE
  86.         {
  87.             --Index;
  88.             if (Index <= 0)
  89.             {
  90.                 Index = TotalSteps-1;
  91.             }
  92.         }
  93.     }
  94.  
  95.     // Reverse pattern direction
  96.     void Reverse()
  97.     {
  98.         if (Direction == FORWARD)
  99.         {
  100.             Direction = REVERSE;
  101.             Index = TotalSteps-1;
  102.         }
  103.         else
  104.         {
  105.             Direction = FORWARD;
  106.             Index = 0;
  107.         }
  108.     }
  109.     void Reflect()
  110.     {
  111.       if (Direction == FORWARD)
  112.       {
  113.          Index++;
  114.          if (Index >= TotalSteps-1)
  115.           {
  116.               Direction = REVERSE;
  117.           }
  118.       }
  119.       else
  120.         {
  121.         Index--;
  122.           if (Index == 0)
  123.            {
  124.                Direction = FORWARD;
  125.            }
  126.         }
  127.     }
  128.  
  129.     // Initialize for a ColorWipe
  130.     void ColorWipe(uint32_t color, uint16_t interval,direction dir = FORWARD)
  131.     {
  132.         ActivePattern = COLOR_WIPE;
  133.         Interval = interval;
  134.         TotalSteps = RowSize;
  135.         Color1 = color;
  136.         Index = 0;
  137.         Direction = dir;
  138.     }
  139.  
  140.     // Update the Color Wipe Pattern
  141.     void ColorWipeUpdate()
  142.     {
  143.         leds[Row][Index] = CRGB::Red;
  144.         //FastLED.show();
  145.         Increment();
  146.     }
  147.     // Initialize for a ColorWipe
  148.     void ColorBounce(uint16_t interval)
  149.     {
  150.         ActivePattern = COLOR_BOUNCE;
  151.         Interval = interval;
  152.         TotalSteps = RowSize;
  153.         Index = 0;
  154.     }
  155.  
  156.     // Update the Color Wipe Pattern
  157.     void ColorBounceUpdate()
  158.     {
  159.       for(int i = 0; i <= TotalSteps; i++ ) {
  160.         if (i == Index) {leds[Row][i] = CHSV(100, 200, 255);}
  161.         else {leds[Row][i] = CHSV(0, 0, 0);}
  162.       }
  163.         //FastLED.show();
  164.         Reflect();
  165.     }
  166.  
  167.  
  168. };
  169.  
  170.  
  171. TreePatterns Branch1(1, 10);
  172. TreePatterns Branch2(2, 8);
  173. TreePatterns Branch3(3, 21);
  174. //TreePatterns Branch4(4, 4);
  175. //TreePatterns Branch5(5, 5);
  176. //TreePatterns Branch6(6, 6);
  177.  
  178. // Initialize everything and prepare to start
  179. void setup()
  180. {
  181.   Serial.begin(115200);
  182.   FastLED.addLeds<WS2812B, 2,  GRB>(leds[0], NUM_LEDSPERROW);
  183.   FastLED.addLeds<WS2812B, 14, GRB>(leds[1], NUM_LEDSPERROW);
  184.   FastLED.addLeds<WS2812B, 7,  GRB>(leds[2], NUM_LEDSPERROW);
  185.   FastLED.addLeds<WS2812B, 20, GRB>(leds[3], NUM_LEDSPERROW);
  186.   FastLED.addLeds<WS2812B, 6,  GRB>(leds[4], NUM_LEDSPERROW);
  187.   FastLED.addLeds<WS2812B, 9,  GRB>(leds[5], NUM_LEDSPERROW);
  188.   FastLED.addLeds<WS2812B, 21, GRB>(leds[6], NUM_LEDSPERROW);
  189.   FastLED.addLeds<WS2812B, 5,  GRB>(leds[7], NUM_LEDSPERROW);
  190.  
  191.  
  192.  
  193.     // Kick off a pattern
  194.     Branch1.ColorWipe(255, 100);
  195.     Branch2.ColorBounce(100);
  196.     Branch3.ColorWipe(255, 200);
  197.     //Branch4.ColorWipe(255, 1000);
  198.     //Branch5.ColorBounce(500);
  199.     //Branch6.ColorWipe(255, 100);
  200.  
  201. }
  202.  
  203. // Main loop
  204. void loop()
  205. {
  206.     // Update the rings.
  207. Branch1.Update();
  208. Branch2.Update();
  209. Branch3.Update();
  210. //Branch4.Update();
  211. //Branch5.Update();
  212. //Branch6.Update();
  213.  
  214. FastLED.show();
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement