SHARE
TWEET

LightEffects

a guest Dec 9th, 2019 92 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Strobe
  2. void Strobe(int FlashDelay, byte red, byte green, byte blue, int StrobeCount, int EndPause) {
  3.  
  4.   int timing = (2 * FlashDelay * StrobeCount) + EndPause;
  5.  
  6.   for (int i = 0; i < 30000 / timing; i++) {
  7.     for (int j = 0; j < StrobeCount; j++) {
  8.       setAll(red, green, blue);
  9.       if (showStrip()) {
  10.         return;
  11.       };
  12.       delay(FlashDelay);
  13.       setAll(0, 0, 0);
  14.       if (showStrip()) {
  15.         return;
  16.       }
  17.       delay(FlashDelay);
  18.     }
  19.  
  20.     delay(EndPause);
  21.   }
  22. }
  23.  
  24. //Cylon
  25.  
  26. void CylonBounce(int SpeedDelay, byte red, byte green, byte blue, int EyeSize, int ReturnDelay, double pixelMultiplier) {
  27.  
  28.   int timing = ((int(NUM_LEDS * pixelMultiplier) - 2 + EyeSize) * SpeedDelay + ReturnDelay) * 2;
  29.   for (int h = 0; h < 30000 / timing; h++) {
  30.     for (int i = -EyeSize; i < int(NUM_LEDS * pixelMultiplier) - 2; i += 2) {
  31.       setAll(0, 0, 0);
  32.  
  33.       setPixel(i, red / 10, green / 10, blue / 10);
  34.       setPixel(i + 1, red / 10, green / 10, blue / 10);
  35.       for (int j = 1; j <= EyeSize; j++) {
  36.         setPixel(i + j, red, green, blue);
  37.         setPixel(i + j + 1, red, green, blue);
  38.       }
  39.       setPixel(i + EyeSize + 1, red / 10, green / 10, blue / 10);
  40.       if (showStrip()) {
  41.         return;
  42.       }
  43.       delay(SpeedDelay);
  44.     }
  45.  
  46.     delay(ReturnDelay);
  47.  
  48.     for (int i = NUM_LEDS * pixelMultiplier - 2; i > -EyeSize; i -= 2) {
  49.       setAll(0, 0, 0);
  50.       setPixel(i, red / 10, green / 10, blue / 10);
  51.       setPixel(i - 1, red / 10, green / 10, blue / 10);
  52.  
  53.       for (int j = 1; j <= EyeSize; j++) {
  54.         setPixel(i + j, red, green, blue);
  55.         setPixel(i + j - 1, red, green, blue);
  56.  
  57.       }
  58.       setPixel(i + EyeSize + 1, red / 10, green / 10, blue / 10);
  59.       if (showStrip()) {
  60.         return;
  61.       };
  62.       delay(SpeedDelay);
  63.     }
  64.  
  65.     delay(ReturnDelay);
  66.   }
  67. }
  68.  
  69. //Fire
  70. void Fire(int SpeedDelay, int Cooling, int Sparking) {
  71.   byte heat[NUM_LEDS];
  72.   int cooldown;
  73.  
  74.   // Step 1.  Cool down every cell a little
  75.   for ( int i = 0; i < NUM_LEDS; i++) {
  76.     cooldown = random(0, ((Cooling * 10) / NUM_LEDS) + 2);
  77.  
  78.     if (cooldown > heat[i]) {
  79.       heat[i] = 0;
  80.     } else {
  81.       heat[i] = heat[i] - cooldown;
  82.     }
  83.   }
  84.  
  85.   // Step 2.  Heat from each cell drifts 'up' and diffuses a little
  86.   for ( int k = NUM_LEDS - 1; k >= 2; k--) {
  87.     heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2]) / 3;
  88.   }
  89.  
  90.   // Step 3.  Randomly ignite new 'sparks' near the bottom
  91.   if ( random(255) < Sparking ) {
  92.     int y = random(7);
  93.     heat[y] = heat[y] + random(160, 255);
  94.     //heat[y] = random(160,255);
  95.   }
  96.  
  97.   // Step 4.  Convert heat to LED colors
  98.   for ( int j = 0; j < NUM_LEDS; j++) {
  99.     setPixelHeatColor(j, heat[j] );
  100.   }
  101.  
  102.   showStrip();
  103.   delay(SpeedDelay);
  104. }
  105.  
  106. void setPixelHeatColor (int Pixel, byte temperature) {
  107.   // Scale 'heat' down from 0-255 to 0-191
  108.   byte t192 = round((temperature / 255.0) * 191);
  109.  
  110.   // calculate ramp up from
  111.   byte heatramp = t192 & 0x3F; // 0..63
  112.   heatramp <<= 2; // scale up to 0..252
  113.  
  114.   // figure out which third of the spectrum we're in:
  115.   if ( t192 > 0x80) {                    // hottest
  116.     setPixel(Pixel, 255, 255, heatramp);
  117.   } else if ( t192 > 0x40 ) {            // middle
  118.     setPixel(Pixel, 255, heatramp, 0);
  119.   } else {                               // coolest
  120.     setPixel(Pixel, heatramp, 0, 0);
  121.   }
  122. }
  123.  
  124. //Color Wipe
  125.  
  126. void colorWipe(int SpeedDelay, byte red, byte green, byte blue) {
  127.   for (uint16_t i = 0; i < NUM_LEDS; i++) {
  128.     setPixel(i, red, green, blue);
  129.     if (showStrip()) {
  130.       return;
  131.     };
  132.     delay(SpeedDelay);
  133.   }
  134. }
  135.  
  136.  
  137. void FadeInOut(int speedIn, double speedOut, byte red, byte green, byte blue) {
  138.   float r, g, b;
  139.  
  140.   for (int k = 0; k < int(256 * speedIn); k = k + 1) {
  141.     r = (k / 256.0 / speedIn) * red;
  142.     g = (k / 256.0 / speedIn) * green;
  143.     b = (k / 256.0 / speedIn) * blue;
  144.     setAll(r, g, b);
  145.     if (showStrip()) {
  146.       return;
  147.     };
  148.   }
  149.  
  150.   for (int k = 255 * speedOut; k >= 0; k = k - 1) {
  151.     r = (k / 256.0 / speedOut) * red;
  152.     g = (k / 256.0 / speedOut) * green;
  153.     b = (k / 256.0 / speedOut) * blue;
  154.     setAll(r, g, b);
  155.     if (showStrip()) {
  156.       return;
  157.     };
  158.   }
  159. }
  160.  
  161. //Halloween Eyes
  162. void HalloweenEyes(int FadeDelay, byte red, byte green, byte blue,
  163.                    int EyeWidth, int EyeSpace,
  164.                    boolean Fade, int Steps,
  165.                    int EndPause) {
  166.   randomSeed(analogRead(0));
  167.  
  168.   if (FadeDelay < 0) {
  169.     FadeDelay = random(50, 150);
  170.   }
  171.  
  172.   if (Steps < 0) {
  173.     Steps = random(5, 50);
  174.   }
  175.  
  176.   if (EndPause < 0) {
  177.     EndPause = random(0, 3000);
  178.   }
  179.   int i;
  180.   int StartPoint  = random( 0, NUM_LEDS - (2 * EyeWidth) - EyeSpace );
  181.   int Start2ndEye = StartPoint + EyeWidth + EyeSpace;
  182.  
  183.   for (i = 0; i < EyeWidth; i++) {
  184.     setPixel(StartPoint + i, red, green, blue);
  185.     setPixel(Start2ndEye + i, red, green, blue);
  186.   }
  187.  
  188.   if (showStrip()) {
  189.     return;
  190.   };
  191.  
  192.   if (Fade == true) {
  193.     float r, g, b;
  194.  
  195.     for (int j = Steps; j >= 0; j--) {
  196.       r = j * (red / Steps);
  197.       g = j * (green / Steps);
  198.       b = j * (blue / Steps);
  199.  
  200.       for (i = 0; i < EyeWidth; i++) {
  201.         setPixel(StartPoint + i, r, g, b);
  202.         setPixel(Start2ndEye + i, r, g, b);
  203.       }
  204.  
  205.       if (showStrip()) {
  206.         return;
  207.       };
  208.       delay(FadeDelay);
  209.     }
  210.   }
  211.  
  212.   setAll(0, 0, 0); // Set all black
  213.  
  214.   delay(EndPause);
  215. }
  216.  
  217. //New KITT
  218. void NewKITT(int SpeedDelay, byte red, byte green, byte blue, int EyeSize, int ReturnDelay) {
  219.   for (int i = 0; i < 2; i++) {
  220.     if (RightToLeft(red, green, blue, EyeSize, SpeedDelay, ReturnDelay)) {
  221.       return;
  222.     }
  223.     if (LeftToRight(red, green, blue, EyeSize, SpeedDelay, ReturnDelay)) {
  224.       return;
  225.     }
  226.  
  227.     if (OutsideToCenter(red, green, blue, EyeSize, SpeedDelay, ReturnDelay)) {
  228.       return;
  229.     }
  230.  
  231.     if (CenterToOutside(red, green, blue, EyeSize, SpeedDelay, ReturnDelay)) {
  232.       return;
  233.     }
  234.  
  235.     //    if (LeftToRight(red, green, blue, EyeSize, SpeedDelay, ReturnDelay)) {
  236.     //      return;
  237.     //    }
  238.     //
  239.     //    if (RightToLeft(red, green, blue, EyeSize, SpeedDelay, ReturnDelay)) {
  240.     //      return;
  241.     //    }
  242.     //
  243.     //    if (OutsideToCenter(red, green, blue, EyeSize, SpeedDelay, ReturnDelay)) {
  244.     //      return;
  245.     //    }
  246.     //
  247.     //    if (CenterToOutside(red, green, blue, EyeSize, SpeedDelay, ReturnDelay)) {
  248.     //      return;
  249.     //    }
  250.  
  251.   }
  252. }
  253.  
  254. bool CenterToOutside(byte red, byte green, byte blue, int EyeSize, int SpeedDelay, int ReturnDelay) {
  255.   for (int i = ((NUM_LEDS - EyeSize) / 2); i >= -EyeSize - 2; i--) {
  256.     setAll(0, 0, 0);
  257.  
  258.     setPixel(i, red / 10, green / 10, blue / 10);
  259.     for (int j = 1; j <= EyeSize; j++) {
  260.       setPixel(i + j, red, green, blue);
  261.     }
  262.     setPixel(i + EyeSize + 1, red / 10, green / 10, blue / 10);
  263.  
  264.     setPixel(NUM_LEDS - i, red / 10, green / 10, blue / 10);
  265.     for (int j = 1; j <= EyeSize; j++) {
  266.       setPixel(NUM_LEDS - i - j, red, green, blue);
  267.     }
  268.     setPixel(NUM_LEDS - i - EyeSize - 1, red / 10, green / 10, blue / 10);
  269.  
  270.     if (showStrip()) {
  271.       return true;
  272.     };
  273.     delay(SpeedDelay);
  274.   }
  275.   delay(ReturnDelay);
  276.   return false;
  277. }
  278.  
  279. bool OutsideToCenter(byte red, byte green, byte blue, int EyeSize, int SpeedDelay, int ReturnDelay) {
  280.   for (int i = -EyeSize - 2; i <= ((NUM_LEDS - EyeSize) / 2); i++) {
  281.     setAll(0, 0, 0);
  282.  
  283.     setPixel(i, red / 10, green / 10, blue / 10);
  284.     for (int j = 1; j <= EyeSize; j++) {
  285.       setPixel(i + j, red, green, blue);
  286.     }
  287.     setPixel(i + EyeSize + 1, red / 10, green / 10, blue / 10);
  288.  
  289.     setPixel(NUM_LEDS - i, red / 10, green / 10, blue / 10);
  290.     for (int j = 1; j <= EyeSize; j++) {
  291.       setPixel(NUM_LEDS - i - j, red, green, blue);
  292.     }
  293.     setPixel(NUM_LEDS - i - EyeSize - 1, red / 10, green / 10, blue / 10);
  294.  
  295.     if (showStrip()) {
  296.       return true;
  297.     };
  298.     delay(SpeedDelay);
  299.   }
  300.   delay(ReturnDelay);
  301.   return false;
  302. }
  303.  
  304. bool LeftToRight(byte red, byte green, byte blue, int EyeSize, int SpeedDelay, int ReturnDelay) {
  305.   for (int i = -EyeSize - 2; i < NUM_LEDS; i++) {
  306.     setAll(0, 0, 0);
  307.     setPixel(i, red / 10, green / 10, blue / 10);
  308.     for (int j = 1; j <= EyeSize; j++) {
  309.       setPixel(i + j, red, green, blue);
  310.     }
  311.     setPixel(i + EyeSize + 1, red / 10, green / 10, blue / 10);
  312.     if (showStrip()) {
  313.       return true;
  314.     };
  315.     delay(SpeedDelay);
  316.   }
  317.   delay(ReturnDelay);
  318.   return false;
  319. }
  320.  
  321. bool RightToLeft(byte red, byte green, byte blue, int EyeSize, int SpeedDelay, int ReturnDelay) {
  322.   for (int i = NUM_LEDS; i > -EyeSize - 2; i--) {
  323.     setAll(0, 0, 0);
  324.     setPixel(i, red / 10, green / 10, blue / 10);
  325.     for (int j = 1; j <= EyeSize; j++) {
  326.       setPixel(i + j, red, green, blue);
  327.     }
  328.     setPixel(i + EyeSize + 1, red / 10, green / 10, blue / 10);
  329.     if (showStrip()) {
  330.       //      Serial.println("Show Strip True");
  331.       return true;
  332.     };
  333.     delay(SpeedDelay);
  334.   }
  335.   delay(ReturnDelay);
  336.   return false;
  337. }
  338.  
  339. int cycleCount = 4;
  340. //Rainbow Cycle
  341. void rainbowCycle(int SpeedDelay) {
  342.   byte *c;
  343.   uint16_t i, j;
  344.  
  345.   for (j = 0; j < 256 * cycleCount; j++) { // 5 cycles of all colors on wheel
  346.     for (i = 0; i < NUM_LEDS; i++) {
  347.       c = Wheel(((i * 256 / NUM_LEDS) + j) & 255);
  348.       setPixel(i, *c, *(c + 1), *(c + 2));
  349.     }
  350.     if (showStrip()) {
  351.       return;
  352.     };
  353.     delay(SpeedDelay);
  354.   }
  355. }
  356.  
  357.  
  358. //Cool Blue Cycle
  359. void coolCycle(int SpeedDelay, int amountOfPurple) {
  360.   FadeColorScheme(SpeedDelay, 2, amountOfPurple);
  361. }
  362.  
  363. //Warm cycle
  364. void warmCycle(int SpeedDelay, int amountOfGreen) {
  365.   FadeColorScheme(SpeedDelay, 1, amountOfGreen);
  366. }
  367.  
  368. //Nature Cycle
  369. void natureCycle(int SpeedDelay, int amountOfBlue) {
  370.   FadeColorScheme(SpeedDelay, 3, amountOfBlue);
  371.  
  372. }
  373.  
  374. void candyCycle(int SpeedDelay, int amountOfSpecial) {
  375.   christmasCycle(SpeedDelay, amountOfSpecial);
  376. }
  377.  
  378. void christmasCycle(int SpeedDelay) {
  379.   christmasCycle(SpeedDelay, 5);
  380. }
  381.  
  382. void christmasCycle(int SpeedDelay, int amountOfChristmas) {
  383.   byte *c;
  384.   uint16_t i, j;
  385.  
  386.   for (j = 0; j < 256 * cycleCount; j++) { // 5 cycles of all colors on wheel
  387.     for (i = 0; i < NUM_LEDS; i++) {
  388.       c = Wheel(((i * 256 / NUM_LEDS) + j) & 255);
  389.       setPixel(i, 255 - *c, *c, amountOfChristmas);
  390.     }
  391.     if (showStrip()) {
  392.       return;
  393.     };
  394.     delay(SpeedDelay);
  395.   }
  396.  
  397. }
  398. //1 = Warm
  399. //2 = Cool
  400. //3 = Green Bleh
  401. void FadeColorScheme(int SpeedDelay, int setOfColors, int amountOfColor) {
  402.   byte *c;
  403.   uint16_t i, j;
  404.  
  405.   for (j = 0; j < 256 * cycleCount; j++) { // 5 cycles of all colors on wheel
  406.     for (i = 0; i < NUM_LEDS; i++) {
  407.       c = Wheel(((i * 256 / NUM_LEDS) + j) & 255);
  408.       //  Serial.println(*c);
  409.       int colorMod = amountOfColor;
  410.       //70 is optimal
  411.  
  412.       switch (setOfColors) {
  413.         case 1:
  414.           if (*c < colorMod) {
  415.             setPixel(i, 255, colorMod - *c, 0);
  416.           } else {
  417.             setPixel(i, 255, 0, *c - colorMod);
  418.           }
  419.           break;
  420.         case 2:
  421.           if (*c < colorMod) {
  422.             setPixel(i, (colorMod - *c) * 2, 0, 255);
  423.           } else {
  424.             setPixel(i, 0, (*c - colorMod), 255);
  425.           }
  426.           break;
  427.         case 3:
  428.           if (*c < colorMod) {
  429.             setPixel(i, 0, 255, colorMod - *c);
  430.           } else {
  431.             setPixel(i, *c - colorMod, 255, 0);
  432.           }
  433.  
  434.           break;
  435.       }
  436.  
  437.     }
  438.     if (showStrip()) {
  439.       return;
  440.     };
  441.     delay(SpeedDelay);
  442.   }
  443. }
  444.  
  445. byte * Wheel(byte WheelPos) {
  446.   static byte c[3];
  447.  
  448.   if (WheelPos < 85) {
  449.     c[0] = WheelPos * 3;
  450.     c[1] = 255 - WheelPos * 3;
  451.     c[2] = 0;
  452.   } else if (WheelPos < 170) {
  453.     WheelPos -= 85;
  454.     c[0] = 255 - WheelPos * 3;
  455.     c[1] = 0;
  456.     c[2] = WheelPos * 3;
  457.   } else {
  458.     WheelPos -= 170;
  459.     c[0] = 0;
  460.     c[1] = WheelPos * 3;
  461.     c[2] = 255 - WheelPos * 3;
  462.   }
  463.  
  464.   return c;
  465. }
  466.  
  467.  
  468.  
  469.  
  470. //Twinkle
  471. void Twinkle(int SpeedDelay, byte red, byte green, byte blue, int Count, boolean OnlyOne, boolean ifClear, int randomClear) {
  472.   int timing = SpeedDelay * (Count + 1);
  473.   for (int a = 0; a < 30000 / timing; a++) {
  474.     if (ifClear) {
  475.       setAll(0, 0, 0);
  476.     }
  477.  
  478.     for (int i = 0; i < Count; i++) {
  479.       setPixel(random(NUM_LEDS), red, green, blue);
  480.       for (int j = 0; j < randomClear; j++) {
  481.         setPixel(random(NUM_LEDS), 0, 0, 0);
  482.       }
  483.       if (showStrip()) {
  484.         return;
  485.       };
  486.       delay(SpeedDelay);
  487.       if (OnlyOne) {
  488.         setAll(0, 0, 0);
  489.       }
  490.     }
  491.  
  492.     delay(SpeedDelay);
  493.   }
  494. }
  495.  
  496.  
  497.  
  498.  
  499. //Random Twinkle
  500. void TwinkleRandom(int Count, int SpeedDelay, boolean OnlyOne, boolean ifClear, int randomClear) {
  501.   int timing = SpeedDelay * (Count + 1);
  502.   for (int a = 0; a < 30000 / timing; a++) {
  503.     if (ifClear) {
  504.       setAll(0, 0, 0);
  505.     }
  506.  
  507.     for (int i = 0; i < Count; i++) {
  508.       setPixel(random(NUM_LEDS), random(0, 255), random(0, 255), random(0, 255));
  509.       for (int j = 0; j < randomClear; j++) {
  510.         setPixel(random(NUM_LEDS), 0, 0, 0);
  511.       }
  512.  
  513.       if (showStrip()) {
  514.         return;
  515.       };
  516.       delay(SpeedDelay);
  517.       if (OnlyOne) {
  518.         setAll(0, 0, 0);
  519.       }
  520.     }
  521.  
  522.     delay(SpeedDelay);
  523.   }
  524. }
  525.  
  526. //Running Lights
  527. void RunningLights(int WaveDelay, byte red, byte green, byte blue) {
  528.   int Position = 0;
  529.  
  530.   for (int i = 0; i < NUM_LEDS * 2; i++)
  531.   {
  532.     Position++; // = 0; //Position + Rate;
  533.     for (int i = 0; i < NUM_LEDS; i++) {
  534.       // sine wave, 3 offset waves make a rainbow!
  535.       //float level = sin(i+Position) * 127 + 128;
  536.       //setPixel(i,level,0,0);
  537.       //float level = sin(i+Position) * 127 + 128;
  538.       setPixel(i, ((sin(i + Position) * 127 + 128) / 255)*red,
  539.                ((sin(i + Position) * 127 + 128) / 255)*green,
  540.                ((sin(i + Position) * 127 + 128) / 255)*blue);
  541.     }
  542.  
  543.     if (showStrip()) {
  544.       return;
  545.     };
  546.     delay(WaveDelay);
  547.   }
  548. }
  549.  
  550.  
  551. //Snow Sparkle
  552.  
  553. void SnowSparkle(int SpeedDelay, byte red, byte green, byte blue, int SparkleDelay, int sparkleNum) {
  554.   int timing = SparkleDelay + 300;
  555.   for ( int k = 0; k < 30000 / timing * 2; k++) {
  556.     setAll(red, green, blue);
  557.     int pixels[sparkleNum];
  558.     for (int i = 0; i < sparkleNum; i++) {
  559.       int Pixel = random(NUM_LEDS);
  560.       setPixel(Pixel, 0xff, 0xff, 0xff);
  561.       pixels[i] = Pixel;
  562.     }
  563.  
  564.     if (showStrip()) {
  565.       return;
  566.     };
  567.     delay(SparkleDelay);
  568.     for (int i = 0; i < sparkleNum; i++) {
  569.       setPixel(pixels[i], red, green, blue);
  570.  
  571.     }
  572.     showStrip();
  573.     delay(SpeedDelay);
  574.   }
  575. }
  576.  
  577.  
  578. //Bouncing Balls
  579.  
  580. void BouncingBalls(int BallCount, byte red, byte green, byte blue) {
  581.   float Gravity = -9.81;
  582.   int StartHeight = 1;
  583.  
  584.   float Height[BallCount];
  585.   float ImpactVelocityStart = sqrt( -2 * Gravity * StartHeight );
  586.   float ImpactVelocity[BallCount];
  587.   float TimeSinceLastBounce[BallCount];
  588.   int   Position[BallCount];
  589.   long  ClockTimeSinceLastBounce[BallCount];
  590.   float Dampening[BallCount];
  591.  
  592.   for (int i = 0 ; i < BallCount ; i++) {
  593.     ClockTimeSinceLastBounce[i] = millis();
  594.     Height[i] = StartHeight;
  595.     Position[i] = 0;
  596.     ImpactVelocity[i] = ImpactVelocityStart;
  597.     TimeSinceLastBounce[i] = 0;
  598.     Dampening[i] = 0.90 - float(i) / pow(BallCount, 2);
  599.   }
  600.  
  601.   while (true) {
  602.     for (int i = 0 ; i < BallCount ; i++) {
  603.       TimeSinceLastBounce[i] =  millis() - ClockTimeSinceLastBounce[i];
  604.       Height[i] = 0.5 * Gravity * pow( TimeSinceLastBounce[i] / 1000 , 2.0 ) + ImpactVelocity[i] * TimeSinceLastBounce[i] / 1000;
  605.  
  606.       if ( Height[i] < 0 ) {
  607.         Height[i] = 0;
  608.         ImpactVelocity[i] = Dampening[i] * ImpactVelocity[i];
  609.         ClockTimeSinceLastBounce[i] = millis();
  610.  
  611.         if ( ImpactVelocity[i] < 0.01 ) {
  612.           ImpactVelocity[i] = ImpactVelocityStart;
  613.         }
  614.       }
  615.       Position[i] = round( Height[i] * (NUM_LEDS - 1) / StartHeight);
  616.     }
  617.  
  618.     for (int i = 0 ; i < BallCount ; i++) {
  619.       setPixel(Position[i], red, green, blue);
  620.     }
  621.  
  622.     if (showStrip()) {
  623.       return;
  624.     };
  625.     setAll(0, 0, 0);
  626.   }
  627. }
  628.  
  629. //Multi Colored Bouncing Balls
  630. void BouncingColoredBalls(int BallCount, byte colors[][3]) {
  631.   float Gravity = -9.81;
  632.   int StartHeight = 1;
  633.  
  634.   float Height[BallCount];
  635.   float ImpactVelocityStart = sqrt( -2 * Gravity * StartHeight );
  636.   float ImpactVelocity[BallCount];
  637.   float TimeSinceLastBounce[BallCount];
  638.   int   Position[BallCount];
  639.   long  ClockTimeSinceLastBounce[BallCount];
  640.   float Dampening[BallCount];
  641.  
  642.   for (int i = 0 ; i < BallCount ; i++) {
  643.     ClockTimeSinceLastBounce[i] = millis();
  644.     Height[i] = StartHeight;
  645.     Position[i] = 0;
  646.     ImpactVelocity[i] = ImpactVelocityStart;
  647.     TimeSinceLastBounce[i] = 0;
  648.     Dampening[i] = 0.90 - float(i) / pow(BallCount, 2);
  649.   }
  650.  
  651.   while (true) {
  652.     for (int i = 0 ; i < BallCount ; i++) {
  653.       TimeSinceLastBounce[i] =  millis() - ClockTimeSinceLastBounce[i];
  654.       Height[i] = 0.5 * Gravity * pow( TimeSinceLastBounce[i] / 1000 , 2.0 ) + ImpactVelocity[i] * TimeSinceLastBounce[i] / 1000;
  655.  
  656.       if ( Height[i] < 0 ) {
  657.         Height[i] = 0;
  658.         ImpactVelocity[i] = Dampening[i] * ImpactVelocity[i];
  659.         ClockTimeSinceLastBounce[i] = millis();
  660.  
  661.         if ( ImpactVelocity[i] < 0.01 ) {
  662.           ImpactVelocity[i] = ImpactVelocityStart;
  663.         }
  664.       }
  665.       Position[i] = round( Height[i] * (NUM_LEDS - 1) / StartHeight);
  666.     }
  667.  
  668.     for (int i = 0 ; i < BallCount ; i++) {
  669.       setPixel(Position[i], colors[i][0], colors[i][1], colors[i][2]);
  670.     }
  671.  
  672.     if (showStrip()) {
  673.       return;
  674.     };
  675.     setAll(0, 0, 0);
  676.   }
  677. }
  678.  
  679.  
  680.  
  681. //Sparkle
  682. void Sparkle(int SpeedDelay, byte red, byte green, byte blue) {
  683.   for (int i = 0; i < 30000 / SpeedDelay / 1.2; i++) {
  684.  
  685.     int Pixel = random(NUM_LEDS);
  686.     setPixel(Pixel, red, green, blue);
  687.     if (showStrip()) {
  688.       return;
  689.     };
  690.     delay(SpeedDelay);
  691.     setPixel(Pixel, 0, 0, 0);
  692.   }
  693. }
  694.  
  695.  
  696.  
  697. //Theater Chase
  698. void theaterChase(int SpeedDelay, byte red, byte green, byte blue) {
  699.   int timing = SpeedDelay * 30;
  700.   for (int k = 0; k < 30000 / timing; k++) {
  701.     for (int j = 0; j < 10; j++) { //do 10 cycles of chasing
  702.       for (int q = 0; q < 3; q++) {
  703.         for (int i = 0; i < NUM_LEDS; i = i + 3) {
  704.           setPixel(i + q, red, green, blue);  //turn every third pixel on
  705.         }
  706.         if (showStrip()) {
  707.           return;
  708.         };
  709.  
  710.         delay(SpeedDelay);
  711.  
  712.         for (int i = 0; i < NUM_LEDS; i = i + 3) {
  713.           setPixel(i + q, 0, 0, 0);    //turn every third pixel off
  714.         }
  715.       }
  716.     }
  717.   }
  718. }
  719.  
  720.  
  721. //Theater Chase Rainbow
  722.  
  723. void theaterChaseRainbow(int SpeedDelay, int colorSpeedChange) {
  724.   byte *c;
  725.   int timing = SpeedDelay * 3 * 83;
  726.   for (int k = 0; k < 30000 / timing; k++) {
  727.     for (int j = 0; j < 256; j += colorSpeedChange) {  // cycle all 256 colors in the wheel
  728.       for (int q = 0; q < 3; q++) {
  729.         for (int i = 0; i < NUM_LEDS; i = i + 3) {
  730.           c = Wheel( (i + j) % 255);
  731.           setPixel(i + q, *c, *(c + 1), *(c + 2)); //turn every third pixel on
  732.         }
  733.         if (showStrip()) {
  734.           return;
  735.         };
  736.  
  737.         delay(SpeedDelay);
  738.  
  739.         for (int i = 0; i < NUM_LEDS; i = i + 3) {
  740.           setPixel(i + q, 0, 0, 0);    //turn every third pixel off
  741.         }
  742.       }
  743.     }
  744.   }
  745. }
  746.  
  747. void candyCane(int SpeedDelay, int setOfLightsNum) {
  748.   for (int h = 0; h < 40; h++) {
  749.     for (int j = -setOfLightsNum * 2; j < 0; j++) {
  750.       for (int i = j; i < NUM_LEDS; i += setOfLightsNum * 2) {
  751.         for (int k = 0; k < setOfLightsNum; k++) {
  752.           setPixel(i + k, 255, 255, 255);
  753.         }
  754.  
  755.         for (int k = setOfLightsNum; k < setOfLightsNum * 2; k++) {
  756.           setPixel(i + k, 255, 0, 0);
  757.         }
  758.       }
  759.       if (showStrip()) {
  760.         return;
  761.       }
  762.  
  763.       delay(SpeedDelay);
  764.     }
  765.   }
  766. }
  767.  
  768.  
  769.  
  770.  
  771.  
  772.  
  773.  
  774.  
  775.  
  776.  
  777.  
  778.  
  779.  
  780.  
  781.  
  782.  
  783.  
  784. bool showStrip() {
  785. #ifdef ADAFRUIT_NEOPIXEL_H
  786.   // NeoPixel
  787.   strip.show();
  788. #endif
  789.   // #ifndef ADAFRUIT_NEOPIXEL_H
  790.   //   // FastLED
  791.   //   FastLED.show();
  792.   // #endif
  793.  
  794.   if (Serial.available() > 0) {
  795.  
  796.     int newCase = Serial.parseInt(); // read the incoming byte:
  797.     Serial.println(newCase);
  798.     currentCase = newCase;
  799.  
  800.   }
  801.  
  802.  
  803.  
  804.  
  805.   // return checkWifi();
  806.   //Serial.println("Server Loop");
  807.   //Serial.println("Before loop received" + String(wemoManager.receivedNetworkRequest));
  808.   wemoManager.serverLoop();
  809.   // Serial.println("After loop received Returning" + String(wemoManager.receivedNetworkRequest));
  810.  
  811.  
  812.   return oldCase != currentCase;
  813.   // return false;
  814.  
  815. }
  816.  
  817.  
  818. void setPixel(int Pixel, byte red, byte green, byte blue) {
  819. #ifdef ADAFRUIT_NEOPIXEL_H
  820.   // NeoPixel
  821.   strip.setPixelColor(Pixel, strip.Color(red, green, blue));
  822. #endif
  823.   // #ifndef ADAFRUIT_NEOPIXEL_H
  824.   //   // FastLED
  825.   //   leds[Pixel].r = red;
  826.   //   leds[Pixel].g = green;
  827.   //   leds[Pixel].b = blue;
  828.   // #endif
  829. }
  830.  
  831. void setIntPixel(int Pixel, int red, int green, int blue) {
  832.   // NeoPixel
  833.   strip.setPixelColor(Pixel, strip.Color(red, green, blue));
  834. }
  835.  
  836. void clearStrip() {
  837.   setAll(0, 0, 0);
  838. }
  839.  
  840. void clearStrip(int numLeds) {
  841.   for (int i = 0; i < numLeds; i++ ) {
  842.     setPixel(i, 0, 0, 0);
  843.   }
  844.   //  Serial.println("Setall");
  845.   if (showStrip()) {
  846.     return;
  847.   };
  848. }
  849.  
  850. void setAll(byte red, byte green, byte blue) {
  851.   for (int i = 0; i < NUM_LEDS; i++ ) {
  852.     setPixel(i, red, green, blue);
  853.   }
  854.   //  Serial.println("Setall");
  855.   if (showStrip()) {
  856.     return;
  857.   };
  858. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top