Advertisement
Guest User

Problem with FastLED.show()

a guest
Jul 13th, 2018
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 27.70 KB | None | 0 0
  1. #define USE_OCTOWS2811
  2. #include<OctoWS2811.h>
  3. #include<FastLED.h>
  4. #include <SLIPEncodedSerial.h>
  5. #include <OSCMessage.h>
  6.  
  7. String readString;
  8.  
  9. // FastLED provides these pre-conigured incandescent color profiles:
  10. //     Candle, Tungsten40W, Tungsten100W, Halogen, CarbonArc,
  11. //     HighNoonSun, DirectSunlight, OvercastSky, ClearBlueSky,
  12. // FastLED provides these pre-configured gaseous-light color profiles:
  13. //     WarmFluorescent, StandardFluorescent, CoolWhiteFluorescent,
  14. //     FullSpectrumFluorescent, GrowLightFluorescent, BlackLightFluorescent,
  15. //     MercuryVapor, SodiumVapor, MetalHalide, HighPressureSodium,
  16. // FastLED also provides an "Uncorrected temperature" profile
  17. //    UncorrectedTemperature;
  18.  
  19. #define TEMPERATURE UncorrectedTemperature
  20.  
  21.  
  22. DEFINE_GRADIENT_PALETTE( pink_gp ) {
  23.    0,     0,   0,   0,    //Black
  24.    25,  255, 255, 255,    //White
  25.   127,  255,  0,  255,    //Pink
  26.   230,  128,  0,  128,   //Purple
  27.   255,    0,  0,    0};    //Black
  28.  
  29. DEFINE_GRADIENT_PALETTE( heatmap_gp ) {
  30.   0,     0,  0,  0,   //black
  31. 85,   255,  0,  0,   //red
  32. 170,   255,255,  0,   //bright yellow
  33. 255,     0,  0,  0 };  //black
  34.  
  35. DEFINE_GRADIENT_PALETTE( blink_purple_gp ) {
  36.   0,     0,  0,  0,     //black
  37. 100,   255,  0,  255,   //Purple
  38. 255,     0,  0,  0};   //Black
  39.  
  40. DEFINE_GRADIENT_PALETTE( blink_orange_gp ) {
  41.   0,     0,  0,  0,     //black
  42. 100,   255,  100,  0,   //orange
  43. 255,     0,  0,  0};   //black
  44.  
  45. DEFINE_GRADIENT_PALETTE( bg_orange_gp ) {
  46.   0,     0,  0,  0,     //black
  47.   50,     0,  0,  0,     //black
  48. 100,   255,  100,  0,   //orange
  49. 205,     0,  0,  0,   //black
  50. 255,     0,  0,  0};   //black
  51.  
  52. DEFINE_GRADIENT_PALETTE( blink_bluetones_gp ) {
  53.   0,     0,  0,  0,     //black
  54. 64,   33,  180,  226,   //Dark blue
  55. 128,   109,  197,  224, //Light blue
  56. 192,   33,  180,  226,  //Dark blue
  57. 255,     0,  0,  0};   //black
  58.  
  59.  
  60.  
  61. CRGBPalette16 heatcolorPalette = heatmap_gp;
  62. CRGBPalette16 pinkPalette = pink_gp;
  63. CRGBPalette16 blinkPalettePurple = blink_purple_gp;
  64. CRGBPalette16 blinkPaletteOrange = blink_orange_gp;
  65. CRGBPalette16 blinkPaletteBluetones = blink_bluetones_gp;
  66. CRGBPalette16 bgPaletteOrange = bg_orange_gp;
  67. CRGBPalette16 rainbowPalette = RainbowColors_p;
  68.  
  69. CRGBPalette16 * paletteArray[] = { &blinkPalettePurple, &blinkPaletteOrange, &blinkPaletteBluetones,&heatcolorPalette,&pinkPalette,&bgPaletteOrange, &rainbowPalette};
  70.  
  71. #define NUM_LEDS_PER_STRIP 203
  72. #define NUM_STRIPS 8
  73. #define BRIGHTNESS 255
  74. CRGB leds[NUM_STRIPS * NUM_LEDS_PER_STRIP];
  75.  
  76. SLIPEncodedSerial SLIPSerial(Serial1);
  77. enum pattern { NONE, BLOOMING, ENOGEN, TWINKLING, BLOOMING_RAINBOW, RAINBOW_THEATER_CHASE};
  78. enum mode { BLOMST, VU_METER, STREAM };
  79. enum direction { FORWARD, REVERSE };
  80.  
  81. //LED-strip order
  82. const uint8_t blomst1BunnHullStart = 0;
  83. const uint8_t blomst1ToppHullStart = 1;
  84. const uint8_t blomst1BunnOmrissStart = 2;
  85. const uint8_t blomst1ToppOmrissStart = 3;
  86.  
  87. const uint8_t blomst2BunnHullStart = 4;
  88. const uint8_t blomst2ToppHullStart = 5;
  89. const uint8_t blomst2BunnOmrissStart = 6;
  90. const uint8_t blomst2ToppOmrissStart = 7;
  91.  
  92. //Forklaring paa nivaaer i arrayene
  93. //0 = Faktisk index i leds[] arrayen
  94. //1 = Rekkefolge i blooming-animasjonen
  95. //2 = Stilk eller blomst
  96. //3 = Next random blink time
  97. //4 = Index pr LED for hvor den er i random blinkanimasjon
  98. float blomst1Omriss [5][377];
  99. float blomst1Hull [5][290];
  100. float blomst2Omriss [5][377];
  101. float blomst2Hull [5][290];
  102.  
  103. //Distanse mellom pixler i blomstene
  104. float distanse1 = 1.5;
  105. //startpixel for venstre blomst, bunn
  106. float vb_b = 31.0;
  107. //startpixel for hoyre blomst, bunn
  108. float hb_b = 35.0;
  109. float bunnHullRekkefolge [] = { 0.0,1.0,2.0,3.0,4.0,4.0,5.0,5.0,6.0,6.0,7.0,7.0,8.0,8.0,9.0,9.0,10.0,11.0,10.0,11.0,12.0,13.0,12.5,13.5,14.0,16.0,15.0,17.0,18.0,19.0,20.0,21.0,22.0,23.0,24.0,25.0,26.0,27.0, //Stilk
  110.                     vb_b+(2*distanse1),vb_b+(2*distanse1),vb_b+(3*distanse1),vb_b+(2*distanse1),vb_b+(3*distanse1),vb_b+(2*distanse1),vb_b+(3*distanse1),vb_b+(3*distanse1),vb_b+(1*distanse1),vb_b+(1*distanse1),vb_b+(1*distanse1),vb_b+(3*distanse1), //Blomst venstre
  111.                     28.0,28.0, //Stilk
  112.                     vb_b+(2*distanse1),vb_b+(1*distanse1),vb_b,vb_b+(1*distanse1),vb_b+(1*distanse1),vb_b+(2*distanse1),vb_b+(3*distanse1),vb_b+(2*distanse1),vb_b+(3*distanse1),vb_b+(3*distanse1), //Blomst venstre
  113.                     29.0,29.0,30.0,31.0,32.0,33.0, //Stilk
  114.                     hb_b+(2*distanse1), //Blomst hoyre
  115.                     32.0, //Stilk
  116.                     hb_b+(2*distanse1),hb_b+(2*distanse1),hb_b+(1*distanse1),hb_b+(1*distanse1), //Blomst hoyre
  117.                     33.0, //Stilk
  118.                     hb_b+(2*distanse1),hb_b+(2*distanse1),hb_b+(1*distanse1),hb_b+(1*distanse1),hb_b+(2*distanse1),hb_b+(2*distanse1),hb_b+(1*distanse1),hb_b,hb_b+(1*distanse1),hb_b+(2*distanse1),hb_b+(2*distanse1),hb_b+(2*distanse1) //Blomst hoyre
  119.                     };
  120.  
  121. //Distanse mellom pixler i blomst
  122. float distanse2 = 1.5;
  123. //startpixel for blomst1, topp
  124. float b1_t = 37.0;
  125. //startpixel for blomst2, topp
  126. float b2_t = 56.0;
  127. //startpixel for blomst3, topp
  128. float b3_t = 60.5;
  129. //startpixel for blomst4, topp
  130. float b4_t = 61.5;
  131.  
  132. float toppHullRekkefolge [] = {
  133.   //Stilk
  134.   34,34,
  135.   //Blomst1
  136.   b1_t+(5.5*distanse2),b1_t+(5.5*distanse2),b1_t+(5.5*distanse2),b1_t+(5.0*distanse2),b1_t+(3.0*distanse2),b1_t+(4.0*distanse2),b1_t+(5.0*distanse2),b1_t+(5.5*distanse2),b1_t+(5.5*distanse2),
  137.   b1_t+(5.5*distanse2),b1_t+(5.5*distanse2),b1_t+(5.0*distanse2),b1_t+(4.0*distanse2),b1_t+(5.0*distanse2),b1_t+(5.5*distanse2),b1_t+(5.5*distanse2),b1_t+(5.5*distanse2),b1_t+(5.5*distanse2),
  138.   b1_t+(5.0*distanse2),b1_t+(5.5*distanse2),b1_t+(5.0*distanse2),b1_t+(4.0*distanse2),b1_t+(3.0*distanse2),b1_t+(2.0*distanse2),b1_t+(2.0*distanse2),b1_t+(2.0*distanse2),b1_t+(2.0*distanse2),
  139.   b1_t+(2.0*distanse2),b1_t+(2.0*distanse2),b1_t+(2.0*distanse2),b1_t+(2.0*distanse2),b1_t+(3.0*distanse2),b1_t+(5.5*distanse2),b1_t+(5.0*distanse2),b1_t+(4.0*distanse2),b1_t+(5.0*distanse2),
  140.   b1_t+(5.5*distanse2),b1_t+(5.5*distanse2),b1_t+(5.5*distanse2),b1_t+(5.5*distanse2),b1_t+(5.0*distanse2),b1_t+(3.0*distanse2),b1_t+(4.0*distanse2),b1_t+(5.0*distanse2),b1_t+(5.5*distanse2),
  141.   b1_t+(5.5*distanse2),b1_t+(5.5*distanse2),b1_t+(5.0*distanse2),b1_t+(3.0*distanse2),b1_t+(2.0*distanse2),b1_t+(1.0*distanse2),b1_t+(1.0*distanse2),b1_t+(1.0*distanse2),b1_t,
  142.   b1_t+(1.0*distanse2),b1_t+(1.0*distanse2),b1_t+(1.0*distanse2),b1_t+(2.0*distanse2),b1_t+(2.0*distanse2),b1_t+(2.0*distanse2),b1_t+(4.0*distanse2),b1_t+(5.0*distanse2),b1_t+(5.5*distanse2),
  143.   b1_t+(5.5*distanse2),b1_t+(5.5*distanse2),
  144.   //Stilk
  145.   40,40.5,41.5,42.5,43,44,44.5,45.5,45.5,46.5,46.5,47.5,47.5,48.5,48.5,49.5,49.5,50.5,50.5,51,51.5,52,53,
  146.   //Blomst2
  147.   b2_t+(2.5*distanse1),b2_t+(2.5*distanse1),b2_t+(2.0*distanse1),b2_t+(2.5*distanse1),b2_t+(2.5*distanse1),b2_t+(2.0*distanse1),b2_t+(2.5*distanse1),b2_t+(1.0*distanse1),b2_t+(1.0*distanse1),
  148.   b2_t+(1.0*distanse1),b2_t,b2_t+(1.0*distanse1),b2_t+(1.0*distanse1),b2_t+(2.0*distanse1),b2_t+(2.5*distanse1),b2_t+(2.5*distanse1),b2_t+(2.0*distanse1),b2_t+(1.0*distanse1),
  149.   b2_t+(2.5*distanse1),b2_t+(2.5*distanse1),b2_t+(2.0*distanse1),b2_t+(2.5*distanse1),b2_t+(2.5*distanse1),
  150.   //Stilk
  151.   54,53,53,52.5,53.5,53.5,54.5,54.5,55.5,55.5,55.5,56.5,57.5,
  152.   //Blomst 3
  153.   b3_t+(2.5*distanse1),b3_t+(2.5*distanse1),b3_t+(2.5*distanse1),b3_t+(2.5*distanse1),b3_t+(2.5*distanse1),b3_t+(2.5*distanse1),b3_t+(2.0*distanse1),b3_t+(2.5*distanse1),b3_t+(1.0*distanse1),
  154.   b3_t+(1.0*distanse1),b3_t+(2.5*distanse1),b3_t+(2.0*distanse1),b3_t+(1.0*distanse1),b3_t,b3_t+(1.0*distanse1),b3_t+(1.0*distanse1),b3_t+(1.0*distanse1),b3_t+(2.5*distanse1),
  155.   b3_t+(2.5*distanse1),b3_t+(2.0*distanse1),b3_t+(2.5*distanse1),b3_t+(2.5*distanse1),b3_t+(2.0*distanse1),b3_t+(2.5*distanse1),b3_t+(2.5*distanse1),b3_t+(2.0*distanse1),
  156.   //Stilk
  157.   58.5,57.5,56.5,56.5,56.5,57.5,58.5,
  158.   //Blomst4
  159.   b4_t,b4_t+(1.0*distanse2),
  160.   //Stilk
  161.   59.5,
  162.   //Blomst4
  163.   b4_t+(2.0*distanse2),b4_t+(1.0*distanse2),b4_t+(3.0*distanse2),b4_t+(2.5*distanse2),b4_t+(4.0*distanse2),b4_t+(5.0*distanse2),b4_t+(3.0*distanse2),b4_t+(3.5*distanse2),b4_t+(5.0*distanse2),
  164.   b4_t+(5.5*distanse2),b4_t+(5.5*distanse2),b4_t+(5.5*distanse2),b4_t+(5.0*distanse2),b4_t+(5.5*distanse2),b4_t+(6.0*distanse2),b4_t+(6.5*distanse2),b4_t+(7.0*distanse2),b4_t+(7.0*distanse2),
  165.   b4_t+(6.5*distanse2),b4_t+(6.0*distanse2),b4_t+(5.5*distanse2),b4_t+(4.5*distanse2),b4_t+(2.5*distanse2),b4_t+(0.5*distanse2),b4_t+(1.5*distanse2),b4_t+(3.5*distanse2),b4_t+(5.5*distanse2),
  166.   b4_t+(5.7*distanse2),b4_t+(6.0*distanse2),b4_t+(6.5*distanse2),b4_t+(6.5*distanse2),b4_t+(6.0*distanse2),b4_t+(5.0*distanse2),b4_t+(4.5*distanse2),b4_t+(4.0*distanse2),b4_t+(3.0*distanse2),
  167.   //Stilk
  168.   60.5,59.5,58.5,58.0,57
  169. };
  170.  
  171.  
  172. class LedsMultitask
  173. {
  174.   public:
  175.  
  176.     pattern ActivePattern1;
  177.     mode ActiveMode;
  178.     direction Direction;
  179.     bool EndOfPattern;
  180.  
  181.     unsigned long Interval1;
  182.     unsigned long Interval2;
  183.     unsigned long lastUpdate1;
  184.     unsigned long lastUpdate2;
  185.  
  186.     uint8_t Hue1;
  187.     uint8_t Value1;
  188.     uint8_t Saturation1;
  189.     uint8_t Hue2;
  190.     uint8_t Value2;
  191.     uint8_t Saturation2;
  192.  
  193.     uint8_t IncrementSat;
  194.     uint8_t IncrementVal;
  195.  
  196.     float Delta;
  197.     float Index1;
  198.     float Increment_by1;
  199.     float Index2;
  200.     float Increment_by2;
  201.  
  202.     uint16_t TotalSteps1;
  203.     uint16_t TotalSteps2;
  204.  
  205.     uint8_t SegmentColorRed;
  206.     uint8_t SegmentColorGreen;
  207.     uint8_t SegmentColorBlue;
  208.     uint8_t NumberOfStrips;
  209.     uint8_t LedsPerStrip;
  210.     uint16_t Pixels;
  211.     boolean UpdatePattern;
  212.     boolean Flash;
  213.     boolean Dim;
  214.     uint8_t PixelDistance;
  215.     uint8_t PixelWidth;
  216.     uint8_t TailLength;
  217.  
  218.     uint32_t BlinkRandomTime;
  219.     uint32_t NextBlink_LEDi[NUM_LEDS_PER_STRIP * NUM_STRIPS];
  220.     CRGBPalette16 * ActivePalette1;
  221.     CRGBPalette16 * ActivePalette2;
  222.  
  223.  
  224.     LedsMultitask(mode displaymode)
  225.     {
  226.         Pixels = NUM_LEDS_PER_STRIP * NUM_STRIPS;
  227.         ActiveMode = displaymode;
  228.     }
  229.  
  230.     // Update the pattern (hull)
  231.     void Update1()
  232.     {
  233.       Serial.println("Update1");
  234.       if ((millis() - lastUpdate1) > Interval1) // time to update
  235.       {
  236.         lastUpdate1 = millis();
  237.         switch (ActivePattern1)
  238.         {
  239.           case BLOOMING:
  240.             BloomingUpdate();
  241.             break;
  242.           case TWINKLING:
  243.               TwinklingUpdate();
  244.               break;
  245.           case BLOOMING_RAINBOW:
  246.               BloomingRainbowUpdate();
  247.               break;
  248.           case RAINBOW_THEATER_CHASE:
  249.               RainbowTheaterChaseUpdate();
  250.               break;
  251.           default:
  252.             break;
  253.         }
  254.       }
  255.     }
  256.  
  257.     // Update the pattern (omriss)
  258.     void Update2()
  259.     {
  260.       if ((millis() - lastUpdate2) > Interval2) // time to update
  261.       {
  262.         lastUpdate2 = millis();
  263.         switch (ActivePattern1)
  264.         {
  265.           case BLOOMING:
  266.           case TWINKLING:
  267.           case BLOOMING_RAINBOW:
  268.             OmrissUpdate();
  269.             break;
  270.           default:
  271.             break;
  272.         }
  273.       }
  274.     }
  275.  
  276.     void ResetHull () {
  277.       for (int i = 0; i < 290; i++) {
  278.         leds[(int)blomst1Hull[0][i]] = CRGB::Black;
  279.         leds[(int)blomst2Hull[0][i]] = CRGB::Black;
  280.       }
  281.     }
  282.  
  283.     void ResetAllLEDs () {
  284.       for (int i = 0; i < NUM_STRIPS*NUM_LEDS_PER_STRIP; i++) {
  285.         leds[i] = CRGB::Black;
  286.       }
  287.     }
  288.  
  289.  
  290.  
  291.     // Increment the Index and reset at the end
  292.     void Increment(float *index, uint16_t *total_steps, float *increment_by)
  293.     {
  294.       *index = *index + *increment_by;
  295.  
  296.       if (*index >= *total_steps)
  297.       {
  298.         *index = 0;
  299.       }
  300.     }
  301.  
  302.     void RainbowTheaterChase(direction dir = FORWARD)
  303.     {
  304.       ActivePattern1 = RAINBOW_THEATER_CHASE;
  305.       TotalSteps1 = 290;
  306.       TotalSteps2 = 100;
  307.       Increment_by1 = 1;
  308.       Increment_by2 = 0.5;
  309.       Interval1 = 25;
  310.       Hue1 = 15;
  311.       Hue2 = 160;
  312.       Saturation1 = 255;
  313.       Saturation2 = 255;
  314.       Value1 = 255;
  315.       Value2 = 0;
  316.       Index1 = 0;
  317.       Index2 = 0;
  318.       PixelDistance = 5;
  319.       PixelWidth = 1;
  320.       Direction = FORWARD;
  321.     }
  322.  
  323.     void RainbowTheaterChaseUpdate()
  324.     {
  325.     uint8_t tail = 0;
  326.     float dist;
  327.  
  328.  
  329.       for(int i=0; i< TotalSteps1; i++)
  330.       {
  331.         dist = abs(((blomst1Hull[1][i]) - Index2));
  332.         if (dist < Delta)
  333.         {
  334.           if ((i + (int)Index1) % PixelDistance == 0)
  335.           {
  336.             leds[(int)blomst1Hull[0][i]] = CHSV((i*10)%255, Saturation1, (Delta-dist)*(255/Delta));
  337.             leds[(int)blomst2Hull[0][i]] = CHSV((i*10)%255, Saturation1, (Delta-dist)*(255/Delta));
  338.             tail = PixelWidth;
  339.           }
  340.           else if (tail != 0)
  341.           {
  342.             leds[(int)blomst1Hull[0][i]] = CHSV((((i*10)-(PixelWidth-(tail*3)+1)))%255, Saturation1, (Delta-dist)*(255/Delta));
  343.             leds[(int)blomst2Hull[0][i]] = CHSV((((i*10)-(PixelWidth-(tail*3)+1)))%255, Saturation1, (Delta-dist)*(255/Delta));
  344.             //Serial.println(tail);
  345.             tail--;
  346.  
  347.           }
  348.           else
  349.           {
  350.             leds[(int)blomst1Hull[0][i]] = CHSV(Hue2, Saturation2, Value2);
  351.             leds[(int)blomst2Hull[0][i]] = CHSV(Hue2, Saturation2, Value2);
  352.           }
  353.         }
  354.         else {
  355.           leds[(int)blomst1Hull[0][i]] = CRGB::Black;
  356.           leds[(int)blomst2Hull[0][i]] = CRGB::Black;
  357.         }
  358.       }
  359.       FastLED.show();
  360.       Increment(&Index1, &TotalSteps1, &Increment_by1);
  361.       Increment(&Index2, &TotalSteps2, &Increment_by2);
  362.     }
  363.  
  364.     void Blooming()
  365.     {
  366.  
  367.       ActiveMode = BLOMST;
  368.       ActivePattern1 = BLOOMING;
  369.       //Hull
  370.       Delta = 4;
  371.       Interval1 = 1;
  372.       Index1 = 0;
  373.       Increment_by1 = 0.08;
  374.       TotalSteps1 = 100;
  375.       Hue1 = 50;
  376.       Hue2 = 100;
  377.       Saturation1 = 255;
  378.       //Omriss
  379.       Interval2 = 50;
  380.       Index2 = 0;
  381.       TotalSteps2 = 255;
  382.       Increment_by2 = 1;
  383.       ActivePalette2 = &bgPaletteOrange;
  384.  
  385.       //Zero all LEDs
  386.       ResetAllLEDs();
  387.  
  388.       LEDS.show();
  389.       Serial.println("Blooming");
  390.  
  391.     }
  392.  
  393.     void BloomingUpdate()
  394.     {
  395.       Serial.println("BloomingUpdate");
  396.       //Hull
  397.       float dist;
  398.       for (int x = 0; x < 290; x++)
  399.       {
  400.         dist = abs(((blomst1Hull[1][x]) - Index1));
  401.         if (dist < Delta)
  402.         {
  403.           if ((int)blomst1Hull[2][x]==0) {
  404.             leds[(int)blomst1Hull[0][x]] = CHSV(Hue1,Saturation1,(Delta-dist)*(255/Delta));
  405.             leds[(int)blomst2Hull[0][x]] = CHSV(Hue1,Saturation1,(Delta-dist)*(255/Delta));
  406.           }
  407.           else {
  408.             leds[(int)blomst1Hull[0][x]] = CHSV(Hue2,Saturation1,(Delta-dist)*(255/Delta));
  409.             leds[(int)blomst2Hull[0][x]] = CHSV(Hue2,Saturation1,(Delta-dist)*(255/Delta));
  410.           }
  411.         }
  412.         else {
  413.           leds[(int)blomst1Hull[0][x]] = CRGB::Black;
  414.           leds[(int)blomst2Hull[0][x]] = CRGB::Black;
  415.         }
  416.       }
  417.       Increment(&Index1, &TotalSteps1, &Increment_by1);
  418.       FastLED.show();
  419.     }
  420.  
  421.     void OmrissUpdate()
  422.     {
  423.       for (int i = 0; i < 377; i++) {
  424.         uint8_t palette_index = (int)Index2+i;
  425.         //leds[(int)blomst1Omriss[0][i]] = CHSV(hue,255,255);
  426.         leds[(int)blomst1Omriss[0][i]] = ColorFromPalette(*ActivePalette2,palette_index);
  427.         leds[(int)blomst2Omriss[0][i]] = ColorFromPalette(*ActivePalette2,palette_index);
  428.       }
  429.       Increment(&Index2, &TotalSteps2, &Increment_by2);
  430.       FastLED.show();
  431.     }
  432.  
  433.     void Twinkling()
  434.     {
  435.  
  436.       ActivePattern1 = TWINKLING;
  437.       //Hull
  438.       Interval1 = 1;
  439.       Index1 = 0;
  440.       Increment_by1 = 1;
  441.       TotalSteps1 = 255;
  442.       Hue2 = 100;
  443.       BlinkRandomTime = 10000;
  444.       Value1 = 100;
  445.       Saturation2 = 255;
  446.       ActivePalette1 = &blinkPaletteOrange;
  447.       //Omriss
  448.       Interval2 = 50;
  449.       Index2 = 0;
  450.       TotalSteps2 = 255;
  451.       Increment_by2 = 1;
  452.       ActivePalette2 = &bgPaletteOrange;
  453.  
  454.  
  455.       for (int i = 0; i < 290; i++) {
  456.         //Nullstiller index for alle LEDs
  457.         blomst1Hull[4][i] = 0;
  458.         blomst2Hull[4][i] = 0;
  459.         //Gir random startverdier til pixels i blomsterhodene
  460.         NextBlink_LEDi[(int)blomst1Hull[0][i]] = millis()+(random16()%BlinkRandomTime);
  461.         NextBlink_LEDi[(int)blomst2Hull[0][i]] = millis()+(random16()%BlinkRandomTime);
  462.       }
  463.  
  464.       //Zero all LEDs
  465.       ResetAllLEDs();
  466.       FastLED.show();
  467.       Serial.println("Twinkling");
  468.     }
  469.  
  470.     void TwinklingUpdate()
  471.     {
  472.       for (int i = 0; i < 290; i++)
  473.       {
  474.  
  475.         if (blomst1Hull[2][i] == 0 ) //Hvis blomst
  476.         {
  477.           if (millis() >= NextBlink_LEDi[(int)blomst1Hull[0][i]])
  478.           {
  479.  
  480.             leds[(int)blomst1Hull[0][i]] = ColorFromPalette( *ActivePalette1, (int)blomst1Hull[4][i]);
  481.             leds[(int)blomst2Hull[0][i]] = ColorFromPalette( *ActivePalette1, (int)blomst1Hull[4][i]); //Sets color of LED according to position in ActivePalette
  482.             blomst1Hull[4][i]++; //Increases Index by 1
  483.             blomst2Hull[4][i]++;
  484.  
  485.             //Checks if we have run through the entire color palette
  486.             if (blomst1Hull[4][i] >= TotalSteps1)
  487.             {
  488.               blomst1Hull[4][i] = 0;
  489.               blomst2Hull[4][i] = 0;                                            //Resets the index to 0
  490.               NextBlink_LEDi[(int)blomst1Hull[0][i]] = (millis()+(random16()%BlinkRandomTime));      //Generates a new time for the next blink
  491.               NextBlink_LEDi[(int)blomst2Hull[0][i]] = (millis()+(random16()%BlinkRandomTime));
  492.             }
  493.           }
  494.         }
  495.         else
  496.         {  //Hvis stilk
  497.           leds[(int)blomst1Hull[0][i]] = CHSV(Hue2,Saturation2,Value1);
  498.           leds[(int)blomst2Hull[0][i]] = CHSV(Hue2,Saturation2,Value1);
  499.         }
  500.       }
  501.       FastLED.show();
  502.     }
  503.  
  504.     void BloomingRainbow()
  505.     {
  506.       //Hull
  507.       Interval1 = 30;
  508.       ActivePattern1 = BLOOMING_RAINBOW;
  509.       Value1 = 255;
  510.       Saturation1 = 255;
  511.       Saturation2 = 0;
  512.       TotalSteps1 = 100;
  513.       Index1 = 0;
  514.       Increment_by1 = 1;
  515.       Delta = 4;
  516.  
  517.       //Omriss
  518.       ActivePalette2 = &rainbowPalette;
  519.  
  520.       //Zero all LEDs
  521.       //ResetAllLEDs();
  522.       //FastLED.show();
  523.       Serial.println("BloomingRainbow");
  524.     }
  525.  
  526.     void BloomingRainbowUpdate()
  527.     {
  528.       //Hull
  529.       float dist;
  530.       for (int x = 0; x < 290; x++)
  531.       {
  532.         dist = abs(((blomst1Hull[1][x]) - Index1));
  533.         if (dist < Delta) {
  534.           if ((int)blomst1Hull[2][x]==0) {
  535.             leds[(int)blomst1Hull[0][x]] = CHSV((int)(Index1+x)%255,Saturation1,(Delta-dist)*(255/Delta));
  536.             leds[(int)blomst2Hull[0][x]] = CHSV((int)(Index1+x)%255,Saturation1,(Delta-dist)*(255/Delta));
  537.           }
  538.           else {
  539.             leds[(int)blomst1Hull[0][x]] = CHSV((int)(Index1+x)%255,Saturation1,(Delta-dist)*(255/Delta));
  540.             leds[(int)blomst2Hull[0][x]] = CHSV((int)(Index1+x)%255,Saturation1,(Delta-dist)*(255/Delta));
  541.           }
  542.         }
  543.         else {
  544.           leds[(int)blomst1Hull[0][x]] = CRGB::Black;
  545.           leds[(int)blomst2Hull[0][x]] = CRGB::Black;
  546.         }
  547.       }
  548.       Increment(&Index1, &TotalSteps1, &Increment_by1);
  549.       FastLED.show();
  550.       Serial.println("BloomingRainbowUpdate");
  551.     }
  552.  
  553.  
  554.     void updateInterval(unsigned long interval)
  555.     {
  556.       Interval1 = interval;
  557.     }
  558.  
  559.     void updateValue1(uint8_t value)
  560.     {
  561.       Value1 = value;
  562.     }
  563.     void updateValue2(uint8_t value)
  564.     {
  565.       Value2 = value;
  566.     }
  567.  
  568.     void updateHue1(uint8_t hue)
  569.     {
  570.       Hue1 = hue;
  571.     }
  572.     void updateHue2(uint8_t hue)
  573.     {
  574.       Hue2 = hue;
  575.     }
  576.  
  577.     void updateSaturation1(uint8_t sat)
  578.     {
  579.       Saturation1 = sat;
  580.     }
  581.     void updateSaturation2(uint8_t sat)
  582.     {
  583.       Saturation2 = sat;
  584.     }
  585.  
  586.     void updatePatternFunction()
  587.     {
  588.       UpdatePattern = 1;
  589.     }
  590.  
  591.     void updateColor1(uint8_t x, uint8_t y)
  592.     {
  593.       Saturation1 = x;
  594.       Hue1 = y;
  595.     }
  596.     void updateColor2(uint8_t x, uint8_t y)
  597.     {
  598.       Saturation2 = x;
  599.       Hue2 = y;
  600.     }
  601.     void changeActiveMode(int x)
  602.     {
  603.       switch (x)
  604.       {
  605.         case 1:
  606.           ActiveMode = BLOMST;
  607.           break;
  608.         case 2:
  609.           ActiveMode = VU_METER;
  610.           break;
  611.         case 3:
  612.           ActiveMode = STREAM;
  613.           break;
  614.         default:
  615.           break;
  616.       }
  617.     }
  618.  
  619.     void changeDirection(uint8_t x)
  620.     {
  621.       switch (x)
  622.       {
  623.         case 1:
  624.           Direction = FORWARD;
  625.           break;
  626.         case 2:
  627.           Direction = REVERSE;
  628.           break;
  629.         default:
  630.           break;
  631.       }
  632.     }
  633.  
  634.     void updatePixelDistance(uint8_t x)
  635.     {
  636.       PixelDistance = x;
  637.     }
  638.  
  639.     void updatePixelWidth(uint8_t x)
  640.     {
  641.       PixelWidth = x;
  642.     }
  643.  
  644.     void updateTailLength(uint8_t x)
  645.     {
  646.       TailLength = x;
  647.     }
  648.  
  649.     void updateBlinkRandomTime(uint32_t x)
  650.     {
  651.       BlinkRandomTime = x;
  652.     }
  653.  
  654.     void SetActivePalette1(int x) {
  655.       ActivePalette1 = paletteArray[x];
  656.       ResetHull();
  657.     }
  658.     void SetActivePalette2(int x) {
  659.       ActivePalette2 = paletteArray[x];
  660.     }
  661.  
  662. };
  663.  
  664. LedsMultitask Ledstrip(BLOMST);
  665.  
  666. void setup() {
  667.   Serial.begin(9600);         //Teensy <=> Computer
  668.   SLIPSerial.begin(115200);   //Teensy <=> ESP
  669.   delay(1000);
  670.   Serial.println("Started");
  671.  
  672.   FastLED.addLeds<OCTOWS2811>(leds, NUM_LEDS_PER_STRIP).setCorrection( TypicalSMD5050 );
  673.   FastLED.setTemperature( TEMPERATURE );
  674.   LEDS.setBrightness(BRIGHTNESS);
  675.   LedArrayMaker();
  676.   Ledstrip.BloomingRainbow();
  677.  
  678.   //Reset all LEDs
  679.   for (int i = 0; i < NUM_LEDS_PER_STRIP*NUM_STRIPS;i++) {
  680.     leds[i] = CRGB::Black;
  681.   }
  682.   FastLED.show();
  683.  
  684. }
  685.  
  686. void loop() {
  687.   Serial.println("loop");
  688.   //OSCMsgReceive();
  689.   //Ledstrip.Update1();
  690.   //Ledstrip.Update2();
  691.  
  692.  
  693.  
  694.   /*
  695.   while (Serial.available())
  696.   {
  697.     Serial.println("Reading serial");
  698.     char c = Serial.read();  //gets one byte from serial buffer
  699.     readString += c; //makes the string readString
  700.     delay(2);  //slow looping to allow buffer to fill with next character
  701.   }
  702.  
  703.   if (readString.length() >0)
  704.   {
  705.     Serial.println(readString);  //so you can see the captured string
  706.     int n = readString.toInt();  //convert readString into a number
  707.     Serial.print("Lyser opp LED nr: ");
  708.     Serial.println(n);
  709.  
  710.     for (int i = 0; i < 290; i++)
  711.     {
  712.       if (i == n) {
  713.         leds[(int)blomst1Hull[0][i]] = CRGB::White;
  714.       }
  715.       else {
  716.         leds[(int)blomst1Hull[0][i]] = CRGB::Black;
  717.       }
  718.     }
  719.     FastLED.show();
  720.   }
  721.  
  722.   readString=""; //empty for next input
  723. */
  724.  
  725. }
  726.  
  727. void OSCMsgReceive()
  728. {
  729.  
  730.   OSCMessage msgIN;
  731.   int size;
  732.  
  733.   static unsigned long microsTime = 0;
  734.   while (!SLIPSerial.endofPacket())
  735.   {
  736.     if ((size = SLIPSerial.available()) > 0)
  737.     {
  738.       microsTime = micros();
  739.       while (size--)  //this needs a byte limit
  740.       {
  741.         msgIN.fill(SLIPSerial.read());
  742.       }
  743.     }
  744.     if ((micros() - microsTime) > 10000) break; //Timeout for no eoP()
  745.   }
  746.  
  747.  
  748.   if(!msgIN.hasError())
  749.   {
  750.     //Serial.println("Routing OSC message");
  751.     msgIN.route("/Mode",changeLEDMode);
  752.     msgIN.route("/Fader",funcValue);
  753.   }
  754.  
  755. }
  756.  
  757. void changeLEDMode(OSCMessage &msg, int addrOffset ){
  758.  
  759.   //Serial.println("Changing LED mode");
  760.  
  761.   //PROGRAMS
  762.   if(msg.fullMatch("/Mode/blooming"))
  763.   {
  764.     Ledstrip.Blooming();
  765.     FastLED.delay(20);
  766.   }
  767.  
  768.   if(msg.fullMatch("/Mode/twinkling"))
  769.   {
  770.     Ledstrip.Twinkling();
  771.     FastLED.delay(20);
  772.   }
  773.  
  774.   if(msg.fullMatch("/Mode/blooming_rainbow"))
  775.   {
  776.     Ledstrip.BloomingRainbow();
  777.     FastLED.delay(20);
  778.   }
  779.  
  780.   if(msg.fullMatch("/Mode/rainbow_theater"))
  781.   {
  782.     Ledstrip.RainbowTheaterChase();
  783.     FastLED.delay(20);
  784.   }
  785.  
  786.   //PARAMETERS
  787.   if(msg.fullMatch("/Mode/direction"))
  788.   {
  789.     float value = msg.getFloat(0);
  790.     Ledstrip.changeDirection((int)value);
  791.   }
  792.  
  793.   //DISPLAY MODES
  794.   if(msg.fullMatch("/Mode/displaymode/blomst"))
  795.   {
  796.     Ledstrip.changeActiveMode(1);
  797.   }
  798.   if(msg.fullMatch("/Mode/displaymode/vu_meter"))
  799.   {
  800.     Ledstrip.changeActiveMode(2);
  801.   }
  802.   if(msg.fullMatch("/Mode/displaymode/stream"))
  803.   {
  804.     Ledstrip.changeActiveMode(3);
  805.   }
  806. }
  807.  
  808. void funcValue(OSCMessage &msg, int addrOffset ){
  809.  
  810.   //OSCMessage msgOUT("/Fader");
  811.   Ledstrip.updatePatternFunction();
  812.  
  813.   // INTERVAL
  814.   if (msg.fullMatch("/Fader/interval"))
  815.   {
  816.     float value = msg.getFloat(0);
  817.     Ledstrip.updateInterval((int)value);
  818.   }
  819.  
  820.   // Value (brightness)
  821.   if (msg.fullMatch("/Fader/value1"))
  822.   {
  823.     float value = msg.getFloat(0);
  824.     Ledstrip.updateValue1((int)value);
  825.   }
  826.   if (msg.fullMatch("/Fader/value2"))
  827.   {
  828.     float value = msg.getFloat(0);
  829.     Ledstrip.updateValue2((int)value);
  830.   }
  831.  
  832.   // HUE
  833.   if (msg.fullMatch("/Fader/hue1"))
  834.   {
  835.     float value = msg.getFloat(0);
  836.     Ledstrip.updateHue1((int)value);
  837.   }
  838.   if (msg.fullMatch("/Fader/hue2"))
  839.   {
  840.     float value = msg.getFloat(0);
  841.     Ledstrip.updateHue2((int)value);
  842.   }
  843.  
  844.   // SATURATION
  845.   if (msg.fullMatch("/Fader/saturation1"))
  846.   {
  847.     float value = msg.getFloat(0);
  848.     Ledstrip.updateSaturation1((int)value);
  849.   }
  850.   if (msg.fullMatch("/Fader/saturation2"))
  851.   {
  852.     float value = msg.getFloat(0);
  853.     Ledstrip.updateSaturation2((int)value);
  854.   }
  855.  
  856.   // COLOR (will replace hue and saturation)
  857.   if (msg.fullMatch("/Fader/color1"))
  858.   {
  859.     float x = msg.getFloat(0);
  860.     float y = msg.getFloat(1);
  861.     Ledstrip.updateColor1((uint8_t)x, (uint8_t)y);
  862.   }
  863.   if (msg.fullMatch("/Fader/color2"))
  864.   {
  865.     float x = msg.getFloat(0);
  866.     float y = msg.getFloat(1);
  867.     Ledstrip.updateColor2((uint8_t)x, (uint8_t)y);
  868.   }
  869.  
  870.   if (msg.fullMatch("/Fader/palette1"))
  871.   {
  872.     int x = (int)msg.getFloat(0);
  873.     if (x != -1) {
  874.       Ledstrip.SetActivePalette1(x);
  875.     }
  876.   }
  877.  
  878.   if (msg.fullMatch("/Fader/palette2"))
  879.   {
  880.     int x = (int)msg.getFloat(0);
  881.     if (x != -1) {
  882.       Ledstrip.SetActivePalette2(x);
  883.     }
  884.   }
  885. }
  886.  
  887.  
  888. void LedArrayMaker()
  889. {
  890.  
  891.  
  892.   //Hull bunn, begge blomster
  893.   for (int i = 0; i < 87; i++) {
  894.     blomst1Hull[0][i] = (blomst1BunnHullStart*NUM_LEDS_PER_STRIP)+i;
  895.     blomst2Hull[0][i] = (blomst2BunnHullStart*NUM_LEDS_PER_STRIP)+i;
  896.  
  897.     blomst1Hull[1][i] = bunnHullRekkefolge[i];
  898.     blomst2Hull[1][i] = bunnHullRekkefolge[i];
  899.  
  900.     //Definerer hva som er blomster
  901.     if ( ((i > 37) && (i < 50)) || ((i > 51) && (i < 62)) ) {
  902.       blomst1Hull[2][i] = 0;
  903.       blomst2Hull[2][i] = 0;
  904.     }
  905.     //Definerer hva som er blomster
  906.     else if ( (i==68) || ((i>69) && (i<74)) || ((i>74)&&(i<87)) ) {
  907.       blomst1Hull[2][i] = 0;
  908.       blomst2Hull[2][i] = 0;
  909.     }
  910.     //Definerer hva som er stilk
  911.     else {
  912.       blomst1Hull[2][i] = 1;
  913.       blomst2Hull[2][i] = 1;
  914.     }
  915.   }
  916.   //Hull topp, begge blomster
  917.   for (int i = 0; i < 203; i++) {
  918.     int x = 87 + i;
  919.  
  920.     blomst1Hull[0][x] = (blomst1ToppHullStart*NUM_LEDS_PER_STRIP)+i;
  921.     blomst2Hull[0][x] = (blomst2ToppHullStart*NUM_LEDS_PER_STRIP)+i;
  922.  
  923.     blomst1Hull[1][x] = toppHullRekkefolge[i];
  924.     blomst2Hull[1][x] = toppHullRekkefolge[i];
  925.  
  926.     //Definerer hva som er blomster
  927.     if ( ((x>88) && (x<154)) || ((x>176) && (x<200)) || ((x>212) && (x<239)) || ((x>245) && (x<248)) || ((x>248) && (x<285)) ) {
  928.       blomst1Hull[2][x] = 0;
  929.       blomst2Hull[2][x] = 0;
  930.     }
  931.     //Definerer hva som er stilk
  932.     else {
  933.       blomst1Hull[2][x] = 1;
  934.       blomst2Hull[2][x] = 1;
  935.     }
  936.   }
  937.  
  938.   //Omriss blomst1 bunn
  939.   for (int i = 0; i < 187; i++) {
  940.     blomst1Omriss[0][i] = (blomst1BunnOmrissStart*NUM_LEDS_PER_STRIP)+i;
  941.   }
  942.   //Omriss blomst1 topp
  943.   for (int i = 0; i < 189; i++) {
  944.     blomst1Omriss[0][i+188] = (blomst1ToppOmrissStart*NUM_LEDS_PER_STRIP)+i;
  945.   }
  946.   //Omriss blomst2 bunn
  947.   for (int i = 0; i < 187; i++) {
  948.     blomst2Omriss[0][i] = (blomst2BunnOmrissStart*NUM_LEDS_PER_STRIP)+i;
  949.   }
  950.   //Omriss blomst2 topp
  951.   for (int i = 0; i < 187; i++) {
  952.     blomst2Omriss[0][i+188] = (blomst2ToppOmrissStart*NUM_LEDS_PER_STRIP)+i;
  953.   }
  954. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement