Advertisement
Guest User

TreeClassMessy

a guest
Jul 7th, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 15.56 KB | None | 0 0
  1. #include "FastLED.h"
  2.  
  3. // Pattern types supported:
  4. enum  pattern {
  5.   NONE,
  6.   BOUNCINGBALL,
  7.   RAINBOW_CYCLE,
  8.   FIREALL,
  9.   FIRESINGLE,
  10.   THEATER_CHASE,
  11.   COLOR_WIPE,
  12.   COLOR_BOUNCE,
  13.   SCANNER,
  14.   FADE };
  15. // Patern directions supported:
  16. enum  direction { FORWARD, REVERSE };
  17.  
  18. #define NUM_LEDS 1200
  19. #define NUM_ROWS 8
  20. #define MAXNUM_LEDSPERROW 150
  21. CRGB leds[1200];
  22.  
  23. //BouncingBalls
  24. #define GRAVITY           -9.81              // Downward (negative) acceleration of gravity in m/s^2
  25. #define h0                5                  // Starting height, in meters, of the ball (strip length)
  26. #define NUM_BALLS         8                  // Number of bouncing balls you want (recommend < 7, but 20 is fun in its own way)
  27.  
  28.  
  29. ///// Hard code length of the strips ////////
  30. /*
  31. #define STRING1SIZE 20
  32. #define STRING2SIZE 20
  33. #define STRING3SIZE 20
  34. #define STRING4SIZE 20
  35. #define STRING5SIZE 20
  36. #define STRING6SIZE 20
  37. #define STRING7SIZE 20
  38. #define STRING8SIZE 20
  39. */
  40. #define STRING1SIZE 150
  41. #define STRING2SIZE 150
  42. #define STRING3SIZE 150
  43. #define STRING4SIZE 150
  44. #define STRING5SIZE 150
  45. #define STRING6SIZE 150
  46. #define STRING7SIZE 150
  47. #define STRING8SIZE 150
  48.  
  49. // NeoPattern Class - derived from the Adafruit_NeoPixel class
  50. class TreePatterns
  51. {
  52.     public:
  53.  
  54.     // Member Variables:
  55.     pattern  ActivePattern;  // which pattern is running
  56.     direction Direction;     // direction to run the pattern
  57.  
  58.     uint16_t Row;    // current row/branch to work on
  59.     uint16_t RowSize;   //totan length of row
  60.     uint16_t NumRows;
  61.  
  62.     unsigned long Interval;   // milliseconds between updates
  63.     unsigned long lastUpdate; // last update of position
  64.  
  65.     uint32_t Color1, Color2;  // What colors are in use
  66.  
  67.     uint16_t TotalSteps;  // total number of steps in the pattern
  68.     uint16_t Index;  // current step within the pattern
  69.     uint16_t EndIndex; //Endindex per pattern
  70.     uint16_t LEDIndex;  //start index of a given row
  71.     uint16_t LEDEndIndex;  //End of index of a given row
  72.  
  73.  
  74.     //Fire2012 VARIABLES
  75.     uint16_t COOLING;
  76.     uint16_t SPARKING;
  77.     uint8_t heat[NUM_ROWS][MAXNUM_LEDSPERROW];
  78.     //BouncingBalls
  79.     float h[NUM_BALLS] ;                         // An array of heights
  80.     float vImpact0 = sqrt( -2 * GRAVITY * h0 );  // Impact velocity of the ball when it hits the ground if "dropped" from the top of the strip
  81.     float vImpact[NUM_BALLS] ;                   // As time goes on the impact velocity will change, so make an array to store those values
  82.     float tCycle[NUM_BALLS] ;                    // The time since the last time the ball struck the ground
  83.     int   pos[NUM_BALLS] ;                       // The integer position of the dot on the strip (LED index)
  84.     long  tLast[NUM_BALLS] ;                     // The clock time of the last ground strike
  85.     float COR[NUM_BALLS] ;                       // Coefficient of Restitution (bounce damping)
  86.  
  87.  
  88.     // Constructor - calls base-class constructor to initialize strip
  89.     TreePatterns(uint16_t row)
  90.     {
  91.         Row = row;
  92.         // FIND the size for the specific row/branch
  93.         switch(Row)
  94.         {
  95.           case 1:
  96.           RowSize = STRING1SIZE;
  97.           LEDIndex = 0;
  98.           LEDEndIndex = STRING1SIZE + LEDIndex -1 ;
  99.           break;
  100.           case 2:
  101.           RowSize = STRING2SIZE;
  102.           LEDIndex = MAXNUM_LEDSPERROW * (Row-1);
  103.           LEDEndIndex = STRING2SIZE + LEDIndex -1 ;
  104.           break;
  105.           case 3:
  106.           RowSize = STRING3SIZE;
  107.           LEDIndex = MAXNUM_LEDSPERROW * (Row-1);
  108.           LEDEndIndex = STRING3SIZE + LEDIndex-1 ;
  109.           break;
  110.           case 4:
  111.           RowSize = STRING4SIZE;
  112.           LEDIndex = MAXNUM_LEDSPERROW * (Row-1);
  113.           LEDEndIndex = STRING4SIZE + LEDIndex - 1 ;
  114.           break;
  115.           case 5:
  116.           RowSize = STRING5SIZE;
  117.           LEDIndex = MAXNUM_LEDSPERROW * (Row-1);
  118.           LEDEndIndex = STRING5SIZE + LEDIndex -1 ;
  119.           break;
  120.           case 6:
  121.           RowSize = STRING6SIZE;
  122.           LEDIndex = MAXNUM_LEDSPERROW * (Row-1);
  123.           LEDEndIndex = STRING6SIZE + LEDIndex -1 ;
  124.           break;
  125.           case 7:
  126.           RowSize = STRING7SIZE;
  127.           LEDIndex = MAXNUM_LEDSPERROW * (Row-1);
  128.           LEDEndIndex = STRING6SIZE + LEDIndex -1 ;
  129.           break;
  130.           case 8:
  131.           RowSize = STRING8SIZE;
  132.           LEDIndex = MAXNUM_LEDSPERROW * (Row-1);
  133.           LEDEndIndex = STRING8SIZE + LEDIndex -1 ;
  134.           break;
  135.           case 9:   //ALL STRINGS
  136.           RowSize = NUM_LEDS;
  137.           LEDIndex = 0;
  138.           LEDEndIndex = NUM_LEDS - 1;
  139.           NumRows = 8;
  140.           break;
  141.           default:
  142.           break;
  143.         }
  144.     }
  145.  
  146.  
  147.     // Update the pattern
  148.     void Update()
  149.     {
  150.         if((millis() - lastUpdate) > Interval) // time to update
  151.         {
  152.             lastUpdate = millis();
  153.             switch(ActivePattern)
  154.             {
  155.                 case BOUNCINGBALL:
  156.                     BouncingBallUpdate();
  157.                     break;
  158.                 case FIRESINGLE:
  159.                     Fire2012SingleUpdate();
  160.                     break;
  161.                 case COLOR_WIPE:
  162.                     ColorWipeUpdate();
  163.                     break;
  164.                 case COLOR_BOUNCE:
  165.                     ColorBounceUpdate();
  166.                     break;
  167.                 case FIREALL:
  168.                     Fire2012AllUpdate();
  169.                     break;
  170.                 default:
  171.                     break;
  172.             }
  173.         }
  174.     }
  175.  
  176.     // Increment the Index and reset at the end
  177.     void Increment()
  178.     {
  179.         if (Direction == FORWARD)
  180.         {
  181.            Index++;
  182.            if (Index >= LEDIndex + TotalSteps)
  183.             {
  184.                 Index = LEDIndex;
  185.             }
  186.         }
  187.         else // Direction == REVERSE
  188.         {
  189.             --Index;
  190.             if (Index <= LEDIndex)
  191.             {
  192.                 Index = LEDIndex + TotalSteps;
  193.             }
  194.         }
  195.     }
  196.  
  197.     // Reverse pattern direction
  198.     void Reverse()
  199.     {
  200.         if (Direction == FORWARD)
  201.         {
  202.             Direction = REVERSE;
  203.             Index = LEDIndex + TotalSteps;
  204.         }
  205.         else
  206.         {
  207.             Direction = FORWARD;
  208.             Index = LEDIndex;
  209.         }
  210.     }
  211.     void Reflect()
  212.     {
  213.       if (Direction == FORWARD)
  214.       {
  215.          //Index++;
  216.          Index = Index + 1;
  217.          if (Index >= LEDEndIndex)
  218.           {
  219.               Direction = REVERSE;
  220.               Index -1;
  221.           }
  222.  
  223.       }
  224.       else
  225.         {
  226.         //Index--;
  227.         Index = Index - 1;
  228.           if (Index <= LEDIndex)
  229.            {
  230.                Direction = FORWARD;
  231.            }
  232.         }
  233.     }
  234.  
  235.  
  236.     // Initialize for a ColorWipe
  237.     void ColorWipe(uint32_t color, uint16_t interval,direction dir = FORWARD)
  238.     {
  239.         ActivePattern = COLOR_WIPE;
  240.         Interval = interval;
  241.         TotalSteps = RowSize;
  242.         Color1 = color;
  243.         Index = LEDIndex;
  244.         EndIndex = LEDEndIndex;
  245.         Direction = dir;
  246.     }
  247.  
  248.     // Update the Color Wipe Pattern
  249.     void ColorWipeUpdate()
  250.     {
  251.         leds[Index] = CRGB::Red;
  252.         Increment();
  253.     }
  254.     // Initialize for a ColorWipe
  255.     void ColorBounce(uint16_t interval)
  256.     {
  257.         ActivePattern = COLOR_BOUNCE;
  258.         Interval = interval;
  259.         Index = LEDIndex;
  260.         EndIndex = LEDEndIndex;
  261.         TotalSteps = RowSize;
  262.     }
  263.  
  264.     // Update the Color Wipe Pattern
  265.     void ColorBounceUpdate()
  266.     {
  267.  
  268.       for(int i = LEDIndex; i <= LEDEndIndex; i++ )
  269.       {
  270.         Serial.print("forI is: ");
  271.         Serial.println(i);
  272.         Serial.print("forLEDIndex is: ");
  273.         Serial.println(LEDIndex);
  274.         //delay(500);
  275.         if (i == Index)
  276.          {
  277.           Serial.print("if I is Index: ");
  278.           Serial.print(i);
  279.           Serial.println(" colorgreen ");
  280.           leds[i] = CHSV(100, 100, 255);}
  281.         else {
  282.           Serial.print("elseI is: ");
  283.           Serial.println(i);
  284.         //  delay(500);
  285.           leds[i] = CHSV(0, 0, 0);}
  286.       }
  287.         //FastLED.show();
  288.         Reflect();
  289.         Serial.println("loop");
  290.         //  delay(2000);
  291.     }
  292.  
  293.     void Fire2012All(uint16_t cool, uint16_t spark,uint16_t interval)
  294.     {
  295.       ActivePattern = FIREALL;
  296.       Interval = interval;
  297.       Index = LEDIndex;
  298.       EndIndex = LEDEndIndex;
  299.       TotalSteps = RowSize;
  300.       COOLING = cool;
  301.       SPARKING = spark;
  302.     }
  303.     void Fire2012AllUpdate()
  304.     {
  305.       // Array of temperature readings at each simulation cell
  306.        // static byte heat[NUM_ROWS-1][NUM_LEDS];
  307.  
  308.       for (uint8_t row = 0; row < NUM_ROWS; row++) {
  309.         // Step 1.  Cool down every cell a little
  310.           for( int i = 0; i < MAXNUM_LEDSPERROW; i++) {
  311.             heat[row][i] = qsub8( heat[row][i],  random8(0, ((COOLING * 10) / MAXNUM_LEDSPERROW) + 2));
  312.           }
  313.  
  314.           // Step 2.  Heat from each cell drifts 'up' and diffuses a little
  315.           for( int k= MAXNUM_LEDSPERROW - 3; k > 0; k--) {
  316.             heat[row][k] = (heat[row][k - 1] + heat[row][k - 2] + heat[row][k - 2] ) / 3;
  317.           }
  318.  
  319.           // Step 3.  Randomly ignite new 'sparks' of heat near the bottom
  320.           if( random8() < SPARKING ) {
  321.             int y = random8(7);
  322.             heat[row][y] = qadd8( heat[row][y], random8(160,255) );
  323.           }
  324.  
  325.           // Step 4.  Map from heat cells to LED colors
  326.           for( int j = 0; j < MAXNUM_LEDSPERROW; j++) {
  327.               leds[XY(row,j)] = HeatColor( heat[row][j]);
  328.  
  329.           }
  330.       }
  331.     }
  332.  
  333.     void Fire2012Single(uint16_t cool, uint16_t spark,uint8_t row, uint16_t interval)
  334.     {
  335.       ActivePattern = FIRESINGLE;
  336.       Interval = interval;
  337.       Index = LEDIndex;
  338.       EndIndex = LEDEndIndex;
  339.       TotalSteps = RowSize;
  340.       COOLING = cool;
  341.       SPARKING = spark;
  342.       Row = row-1;
  343.     }
  344.     void Fire2012SingleUpdate()
  345.     {
  346.       // Array of temperature readings at each simulation cell
  347.        // static byte heat[NUM_ROWS-1][NUM_LEDS];
  348.  
  349.  
  350.         // Step 1.  Cool down every cell a little
  351.           for( int i = Index; i < MAXNUM_LEDSPERROW; i++) {
  352.             heat[Row][i] = qsub8( heat[Row][i],  random8(0, ((COOLING * 10) / MAXNUM_LEDSPERROW) + 2));
  353.           }
  354.  
  355.           // Step 2.  Heat from each cell drifts 'up' and diffuses a little
  356.           for( int k= MAXNUM_LEDSPERROW - 3; k > 0; k--) {
  357.             heat[Row][k] = (heat[Row][k - 1] + heat[Row][k - 2] + heat[Row][k - 2] ) / 3;
  358.           }
  359.  
  360.           // Step 3.  Randomly ignite new 'sparks' of heat near the bottom
  361.           if( random8() < SPARKING ) {
  362.             int y = random8(7);
  363.             heat[Row][y] = qadd8( heat[Row][y], random8(160,255) );
  364.           }
  365.  
  366.           // Step 4.  Map from heat cells to LED colors
  367.           for( int j = 0; j < MAXNUM_LEDSPERROW; j++) {
  368.               leds[XY(Row,j)] = HeatColor( heat[Row][j]);
  369.  
  370.           }
  371.     }
  372.  
  373.  
  374.  
  375.     // Initialize for a ColorWipe
  376.     void BouncingBall(uint16_t row)
  377.     {
  378.         ActivePattern = BOUNCINGBALL;
  379.         TotalSteps = RowSize;
  380.         Index = LEDIndex;
  381.         EndIndex = LEDEndIndex;
  382.         Row = row-1;
  383.         for (int i = 0 ; i < NUM_BALLS ; i++) {    // Initialize variables
  384.           tLast[i] = millis();
  385.           h[i] = h0;
  386.           pos[i] = 0;                              // Balls start on the ground
  387.           vImpact[i] = vImpact0;                   // And "pop" up at vImpact0
  388.           tCycle[i] = 0;
  389.           COR[i] = 0.90 - float(i)/pow(NUM_BALLS,2);
  390.         }
  391.     }
  392.  
  393.     // Update the Color Wipe Pattern
  394.     void BouncingBallUpdate()
  395.     {
  396.       for (int i = 0 ; i < NUM_BALLS ; i++) {
  397.         tCycle[i] =  millis() - tLast[i] ;     // Calculate the time since the last time the ball was on the ground
  398.  
  399.         // A little kinematics equation calculates positon as a function of time, acceleration (gravity) and intial velocity
  400.         h[i] = 0.5 * GRAVITY * pow( tCycle[i]/1000 , 2.0 ) + vImpact[i] * tCycle[i]/1000;
  401.  
  402.         if ( h[i] < 0 ) {
  403.           h[i] = 0;                            // If the ball crossed the threshold of the "ground," put it back on the ground
  404.           vImpact[i] = COR[i] * vImpact[i] ;   // and recalculate its new upward velocity as it's old velocity * COR
  405.           tLast[i] = millis();
  406.  
  407.           if ( vImpact[i] < 0.01 ) vImpact[i] = vImpact0;  // If the ball is barely moving, "pop" it back up at vImpact0
  408.         }
  409.         pos[i] = round( h[i] * (TotalSteps - 1) / h0);       // Map "h" to a "pos" integer index position on the LED strip
  410.         Serial.println("initial value is:  ");
  411.         Serial.print(Row);
  412.         Serial.print(" |  | ");
  413.         Serial.print(tCycle[i]);
  414.         Serial.print(" |  | ");
  415.         Serial.print(tLast[i]);
  416.         Serial.print(" |  | ");
  417.         Serial.print(h[i]);
  418.         Serial.print(" |  | ");
  419.         Serial.print(vImpact[i]);
  420.         Serial.print(" |  | ");
  421.         Serial.print(h0);
  422.         Serial.print(" |  | ");
  423.         Serial.print(pos[i]);
  424.         Serial.print(" |  | end");
  425.  
  426.       }
  427.  
  428.       //Choose color of LEDs, then the "pos" LED on
  429.       for (int i = 0 ; i < NUM_BALLS ; i++)
  430.       {
  431.         leds[XY(Row,pos[i])] = CHSV( uint8_t (i * 40) , 255, 255);
  432.         Serial.println("BALLRow value is:  ");
  433.         Serial.print(Row);
  434.         Serial.print("pos value is:  ");
  435.         Serial.print(pos[i]);
  436.         Serial.print("XY value is:  ");
  437.         Serial.print(XY(Row,pos[i]));
  438.       }
  439.       delay(50);
  440.       FastLED.show();
  441.       //Then off for the next loop around
  442.  
  443.       for (int i = 0 ; i < NUM_BALLS ; i++) {
  444.         leds[XY(Row,pos[i])] = CRGB::Black;
  445.         Serial.println("BLACKRow value is:  ");
  446.         Serial.print(Row);
  447.  
  448.         Serial.print("XY value is:  ");
  449.         Serial.print(XY(Row,pos[i]));
  450.       }
  451.  
  452.       //for (int i = LEDIndex ; i < TotalSteps ; i++) {
  453.           //leds[i].nscale8(195);
  454.       //    leds[XY(Row,i)].nscale8(195);
  455.     //  }
  456.  
  457.       /*
  458.       for (int i = 0 ; i < NUM_BALLS ; i++) {
  459.         leds[pos[i]] = CRGB::Black;
  460.       }
  461.       */
  462.     }
  463.  
  464. };
  465.  
  466.  
  467. TreePatterns Branch1(1);
  468. TreePatterns Branch2(2);
  469. TreePatterns Branch3(3);
  470. TreePatterns Branch4(4);
  471. TreePatterns Branch5(5);
  472. TreePatterns Branch6(6);
  473. TreePatterns AllBranches(9);
  474.  
  475. // Initialize everything and prepare to start
  476. void setup()
  477. {
  478.   delay(5000);
  479.  
  480.   Serial.begin(115200);
  481.  
  482.   FastLED.addLeds<WS2812B, 2,  GRB>(leds, MAXNUM_LEDSPERROW * 0, MAXNUM_LEDSPERROW);
  483.   FastLED.addLeds<WS2812B, 14, GRB>(leds, MAXNUM_LEDSPERROW * 1, MAXNUM_LEDSPERROW);
  484.   FastLED.addLeds<WS2812B, 7,  GRB>(leds, MAXNUM_LEDSPERROW * 2, MAXNUM_LEDSPERROW);
  485.   FastLED.addLeds<WS2812B, 8,  GRB>(leds, MAXNUM_LEDSPERROW * 3, MAXNUM_LEDSPERROW);
  486.   FastLED.addLeds<WS2812B, 6,  GRB>(leds, MAXNUM_LEDSPERROW * 4, MAXNUM_LEDSPERROW);
  487.   FastLED.addLeds<WS2812B, 20, GRB>(leds, MAXNUM_LEDSPERROW * 5, MAXNUM_LEDSPERROW);
  488.   FastLED.addLeds<WS2812B, 21, GRB>(leds, MAXNUM_LEDSPERROW * 6, MAXNUM_LEDSPERROW);
  489.   FastLED.addLeds<WS2812B, 5,  GRB>(leds, MAXNUM_LEDSPERROW * 7, MAXNUM_LEDSPERROW);
  490.   FastLED.setBrightness(30);
  491.  
  492.     // Kick off a pattern
  493.     //Branch1.ColorWipe(255, 1000);
  494.     //Branch2.ColorWipe(255, 150);
  495.     Branch2.ColorBounce(200);
  496.     //Branch3.ColorBounce(500);
  497.     Branch4.ColorWipe(255, 1000);
  498.     Branch5.ColorBounce(500);
  499.     Branch1.Fire2012Single(75, 140, 1, 50);
  500.     Branch3.BouncingBall(3);
  501.     AllBranches.Fire2012All(55, 140, 50);
  502. }
  503.  
  504. // Main loop
  505. void loop()
  506. {
  507.   random16_add_entropy( random());
  508.  
  509.     // Update the rings.
  510. //Fire2012();
  511. Branch1.Update();
  512. Branch2.Update();
  513. Branch3.Update();
  514. Branch4.Update();
  515. Branch5.Update();
  516. Branch6.Update();
  517. //AllBranches.Update();
  518.  
  519. FastLED.show();
  520. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement