CPP   38

code

Guest on 13th January 2022 12:10:28 AM

  1. #define FASTLED_INTERNAL
  2. #include <FastLED.h>
  3. #include <Adafruit_NeoPixel.h>
  4. //#include <EEPROM.h>
  5. #define PIN 5
  6. #define N_PIXELS  32
  7. #define BG 0
  8. #define COLOR_ORDER GRB  // Try mixing up the letters (RGB, GBR, BRG, etc) for a whole new world of color combinations
  9. #define BRIGHTNESS 64   // 0-255, higher number is brighter.
  10. #define LED_TYPE WS2812B
  11. #define MIC_PIN    A4  // Microphone is attached to this analog pin
  12. #define MIC_PIN_2  A5
  13. #define DC_OFFSET  0  // DC offset in mic signal - if unusure, leave 0
  14. #define NOISE     10  // Noise/hum/interference in mic signal
  15. #define SAMPLES   64  // Length of buffer for dynamic level adjustment
  16. #define SAMPLES2  64
  17. #define TOP       (N_PIXELS + 2) // Allow dot to go slightly off scale
  18. #define PEAK_FALL 20  // Rate of peak falling dot
  19. #define N_PIXELS_HALF (N_PIXELS / 2)
  20. #define N_PIXELS_QUARTER (N_PIXELS_HALF / 2)
  21. #define GRAVITY           -9.81              // Downward (negative) acceleration of gravity in m/s^2
  22. #define h0                1                  // Starting height, in meters, of the ball (strip length)
  23. #define NUM_BALLS         3                  // Number of bouncing balls you want (recommend < 7, but 20 is fun in its own way)
  24. #define SPEED .20       // Amount to increment RGB color by each cycle
  25. #define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
  26.  
  27. //config for balls
  28. float h[NUM_BALLS] ;                         // An array of heights
  29. float vImpact0 = sqrt( -2 * GRAVITY * h0 );  // Impact velocity of the ball when it hits the ground if "dropped" from the top of the strip
  30. float vImpact[NUM_BALLS] ;                   // As time goes on the impact velocity will change, so make an array to store those values
  31. float tCycle[NUM_BALLS] ;                    // The time since the last time the ball struck the ground
  32. int   pos[NUM_BALLS] ;                       // The integer position of the dot on the strip (LED index)
  33. long  tLast[NUM_BALLS] ;                     // The clock time of the last ground strike
  34. float COR[NUM_BALLS] ;                       // Coefficient of Restitution (bounce damping)
  35.  
  36. float
  37. greenOffset = 30,
  38. blueOffset = 150;
  39.  
  40. byte
  41. peakLeft      = 0,      // Used for falling dot
  42. peakRight     = 0,
  43. dotCountLeft  = 0,      // Frame counter for delaying dot-falling speed
  44. dotCountRight = 0,
  45. volCount  = 0;      // Frame counter for storing past volume data
  46. int
  47. vol[SAMPLES],       // Collection of prior volume samples
  48.     lvl       = 10,      // Current "dampened" audio level
  49.     minLvlAvg = 0,      // For dynamic adjustment of graph low & high
  50.     maxLvlAvg = 512;
  51.  
  52. uint8_t volCountLeft = 0;
  53. int volLeft[SAMPLES];
  54. int lvlLeft = 10;
  55. int minLvlAvgLeft = 0;
  56. int maxLvlAvgLeft = 512;
  57.  
  58. uint8_t volCountRight = 0;
  59. int volRight[SAMPLES2];
  60. int lvlRight = 10;
  61. int minLvlAvgRight = 0;
  62. int maxLvlAvgRight = 512;
  63.  
  64. int brightnessValue, prevBrightnessValue;
  65. int sensorDeviationBrightness = 1;
  66. int sensitivityValue = 128;                               // 0 - 255, initial value (value read from the potentiometer if useSensorValues = true)
  67. int maxSensitivity = 2 * 255;                             // let the 'volume' go up to 200%!
  68. int ledBrightness = 64;                                   // 0 - 255, initial value (value read from the potentiometer if useSensorValues = true)
  69. int nPatterns = 1;
  70. int Pattern = 1;
  71.  
  72. Adafruit_NeoPixel strip = Adafruit_NeoPixel(N_PIXELS, PIN, NEO_GRB + NEO_KHZ800);
  73.  
  74. // FOR SYLON ETC
  75. uint8_t thisbeat =  23;
  76. uint8_t thatbeat =  28;
  77. uint8_t thisfade =   2;                                     // How quickly does it fade? Lower = slower fade rate.
  78. uint8_t thissat = 255;                                     // The saturation, where 255 = brilliant colours.
  79. uint8_t thisbri = 64;
  80.  
  81. //FOR JUGGLE
  82. uint8_t numdots = 4;                                          // Number of dots in use.
  83. uint8_t faderate = 2;                                         // How long should the trails be. Very low value = longer trails.
  84. uint8_t hueinc = 16;                                          // Incremental change in hue between each dot.
  85. uint8_t thishue = 0;                                          // Starting hue.
  86. uint8_t curhue = 0;
  87. uint8_t thisbright = 64;                                     // How bright should the LED/display be.
  88. uint8_t basebeat = 5;
  89. uint8_t max_bright = 64;
  90.  
  91. // Twinkle
  92. float redStates[N_PIXELS];
  93. float blueStates[N_PIXELS];
  94. float greenStates[N_PIXELS];
  95. float Fade = 0.96;
  96.  
  97. // Vu meter 4
  98. const uint32_t Red = strip.Color(128, 0, 0);
  99. const uint32_t Yellow = strip.Color(128, 128, 0);
  100. const uint32_t Green = strip.Color(0, 128, 0);
  101. const uint32_t Blue = strip.Color(0, 0, 128);
  102. const uint32_t White = strip.Color(128, 128, 128);
  103. const uint32_t Dark = strip.Color(0, 0, 0);
  104. unsigned int sample;
  105.  
  106. CRGB leds[N_PIXELS];
  107.  
  108. int          myhue =   0;
  109. // Modes
  110. enum
  111. {
  112. } MODE;
  113. bool reverse = true;
  114.  
  115.  
  116.  
  117. //Ripple variables
  118. int color;
  119. int center = 0;
  120. int step = -1;
  121. int maxSteps = 8;
  122. float fadeRate = 0.80;
  123. int diff;
  124.  
  125. //background color
  126. uint32_t currentBg = random(256);
  127. uint32_t nextBg = currentBg;
  128.  
  129. byte peak = 16;
  130. byte dotCount = 0;
  131. byte dotHangCount = 0;
  132.  
  133.  
  134. //************************************************************************
  135. void setup() {
  136.   delay( 2000 ); // power-up safety delay
  137.   FastLED.addLeds<WS2812B, PIN, COLOR_ORDER>(leds, N_PIXELS).setCorrection( TypicalLEDStrip );
  138.   FastLED.setBrightness(  BRIGHTNESS );
  139.   //  analogReference(EXTERNAL);
  140.   memset(vol, 0, sizeof(vol));
  141.   LEDS.addLeds<LED_TYPE, PIN, COLOR_ORDER>(leds, N_PIXELS);
  142.   strip.setBrightness(ledBrightness);
  143.   strip.begin();
  144.   strip.clear();
  145.   strip.show(); // Initialize all pixels to 'off'
  146.   for (int i = 0 ; i < NUM_BALLS ; i++) {    // Initialize variables
  147.     tLast[i] = millis();
  148.     h[i] = h0;
  149.     pos[i] = 0;                              // Balls start on the ground
  150.     vImpact[i] = vImpact0;                   // And "pop" up at vImpact0
  151.     tCycle[i] = 0;
  152.     COR[i] = 0.90 - float(i) / pow(NUM_BALLS, 2);
  153.   }
  154. }
  155.  
  156. void loop() {
  157.  
  158.   //for mic
  159.   uint8_t  i;
  160.   uint16_t minLvlLeft, maxLvlLeft;
  161.   uint16_t minLvlRight, maxLvlRight;
  162.   int      n, n1, height;
  163.   // end mic
  164.  
  165.   Pattern = Pattern + 1;
  166.   if ( Pattern > nPatterns ) Pattern = 1;
  167.  
  168.  
  169.   switch (Pattern) {
  170.  
  171.     case 1:
  172.       All();
  173.       break;
  174.   }
  175. }
  176.  
  177. void colorWipe(uint32_t c, uint8_t wait) {
  178.   for (uint16_t i = 0; i < strip.numPixels(); i++) {
  179.     strip.setPixelColor(i, c);
  180.     strip.show();
  181.     if (Pattern != nPatterns)  // <------------- add this
  182.       return;         // <------------ and this
  183.     delay(wait);
  184.   }
  185. }
  186.  
  187. void Vu4() {
  188.  
  189.   uint8_t  i;
  190.   uint16_t minLvlLeft, maxLvlLeft;
  191.   uint16_t minLvlRight, maxLvlRight;
  192.   int      n, n1, height;
  193.  
  194.   n   = analogRead(MIC_PIN);                        // Raw reading from mic
  195.   n   = abs(n - 512 - DC_OFFSET); // Center on zero
  196.   n   = (n <= NOISE) ? 0 : (n - NOISE);             // Remove noise/hum
  197.   lvlLeft = ((lvlLeft * 7) + n) >> 3;    // "Dampened" reading (else looks twitchy)
  198.  
  199.   // Calculate bar height based on dynamic min/max levels (fixed point):
  200.   height = TOP * (lvlLeft - minLvlAvgLeft) / (long)(maxLvlAvgLeft - minLvlAvgLeft);
  201.  
  202.   if (height < 0L)       height = 0;     // Clip output
  203.   else if (height > TOP) height = TOP;
  204.   if (height > peakLeft)     peakLeft   = height; // Keep 'peak' dot at top
  205.   greenOffset += SPEED;
  206.   blueOffset += SPEED;
  207.   if (greenOffset >= 255) greenOffset = 0;
  208.   if (blueOffset >= 255) blueOffset = 0;
  209.  
  210.   // Color pixels based on rainbow gradient
  211.   for (i = 0; i < N_PIXELS_HALF; i++) {
  212.     if (i >= height) {
  213.       strip.setPixelColor(N_PIXELS_HALF + i,   0,   0, 0);
  214.     }
  215.     else {
  216.       uint32_t color = Wheel1(map(N_PIXELS_HALF + i, 0, strip.numPixels() - 1, (int)greenOffset, (int)blueOffset));
  217.       strip.setPixelColor(N_PIXELS_HALF + i, color);
  218.     }
  219.  
  220.   }
  221.  
  222.   // Draw peak dot
  223.   if (peakLeft > 0 && peakLeft <= N_PIXELS_HALF - 1) {
  224.     uint32_t color = Wheel1(map(N_PIXELS_HALF + peakLeft, 0, strip.numPixels() - 1, 30, 150));
  225.     strip.setPixelColor(N_PIXELS_HALF + peakLeft, color);
  226.   }
  227.  
  228.   strip.show(); // Update strip
  229.  
  230.   // Every few frames, make the peak pixel drop by 1:
  231.  
  232.   if (++dotCountLeft >= PEAK_FALL) { //fall rate
  233.  
  234.     if (peakLeft > 0) peakLeft--;
  235.     dotCountLeft = 0;
  236.   }
  237.  
  238.  
  239.   volLeft[volCountLeft] = n;                      // Save sample for dynamic leveling
  240.   if (++volCountLeft >= SAMPLES) volCountLeft = 0; // Advance/rollover sample counter
  241.  
  242.   // Get volume range of prior frames
  243.   minLvlLeft = maxLvlLeft = vol[0];
  244.   for (i = 1; i < SAMPLES; i++) {
  245.     if (volLeft[i] < minLvlLeft)      minLvlLeft = volLeft[i];
  246.     else if (volLeft[i] > maxLvlLeft) maxLvlLeft = volLeft[i];
  247.   }
  248.   // minLvl and maxLvl indicate the volume range over prior frames, used
  249.   // for vertically scaling the output graph (so it looks interesting
  250.   // regardless of volume level).  If they're too close together though
  251.   // (e.g. at very low volume levels) the graph becomes super coarse
  252.   // and 'jumpy'...so keep some minimum distance between them (this
  253.   // also lets the graph go to zero when no sound is playing):
  254.   if ((maxLvlLeft - minLvlLeft) < TOP) maxLvlLeft = minLvlLeft + TOP;
  255.   minLvlAvgLeft = (minLvlAvgLeft * 63 + minLvlLeft) >> 6; // Dampen min/max levels
  256.   maxLvlAvgLeft = (maxLvlAvgLeft * 63 + maxLvlLeft) >> 6; // (fake rolling average)
  257.  
  258.   n1   = analogRead(MIC_PIN_2);                        // Raw reading from mic
  259.   n1   = abs(n1 - 512 - DC_OFFSET); // Center on zero
  260.   n1   = (n1 <= NOISE) ? 0 : (n1 - NOISE);             // Remove noise/hum
  261.   lvlRight = ((lvlRight * 7) + n1) >> 3;    // "Dampened" reading (else looks twitchy)
  262.  
  263.   // Calculate bar height based on dynamic min/max levels (fixed point):
  264.   height = TOP * (lvlRight - minLvlAvgRight) / (long)(maxLvlAvgRight - minLvlAvgRight);
  265.  
  266.   if (height < 0L)       height = 0;     // Clip output
  267.   else if (height > TOP) height = TOP;
  268.   if (height > peakRight)     peakRight   = height; // Keep 'peak' dot at top
  269.   greenOffset += SPEED;
  270.   blueOffset += SPEED;
  271.   if (greenOffset >= 255) greenOffset = 0;
  272.   if (blueOffset >= 255) blueOffset = 0;
  273.  
  274.   // Color pixels based on rainbow gradient
  275.   for (i = 0; i < N_PIXELS_HALF; i++) {
  276.     if (i >= height) {
  277.       strip.setPixelColor(N_PIXELS_HALF - i - 1,   0,   0, 0);
  278.     }
  279.     else {
  280.       uint32_t color = Wheel1(map(i, 0, strip.numPixels() - 1, (int)greenOffset, (int)blueOffset));
  281.       strip.setPixelColor(N_PIXELS_HALF - i - 1, color);
  282.     }
  283.   }
  284.  
  285.   // Draw peak dot
  286.   if (peakRight > 0 && peakRight <= N_PIXELS_HALF - 1) {
  287.     uint32_t color = Wheel1(map(N_PIXELS_HALF - peakRight - 1, 0, strip.numPixels() - 1, 30, 150));
  288.     strip.setPixelColor(N_PIXELS_HALF - peakRight - 1, color);
  289.   }
  290.  
  291.   strip.show(); // Update strip
  292.  
  293.   // Every few frames, make the peak pixel drop by 1:
  294.  
  295.   if (++dotCountRight >= PEAK_FALL) { //fall rate
  296.  
  297.     if (peakRight > 0) peakRight--;
  298.     dotCountRight = 0;
  299.   }
  300.  
  301.  
  302.   volRight[volCountRight] = n;                      // Save sample for dynamic leveling
  303.   if (++volCountRight >= SAMPLES2) volCountRight = 0; // Advance/rollover sample counter
  304.  
  305.   // Get volume range of prior frames
  306.   minLvlRight = maxLvlRight = vol[0];
  307.   for (i = 1; i < SAMPLES2; i++) {
  308.     if (volRight[i] < minLvlRight)      minLvlRight = volRight[i];
  309.     else if (volRight[i] > maxLvlRight) maxLvlRight = volRight[i];
  310.   }
  311.   // minLvl and maxLvl indicate the volume range over prior frames, used
  312.   // for vertically scaling the output graph (so it looks interesting
  313.   // regardless of volume level).  If they're too close together though
  314.   // (e.g. at very low volume levels) the graph becomes super coarse
  315.   // and 'jumpy'...so keep some minimum distance between them (this
  316.   // also lets the graph go to zero when no sound is playing):
  317.   if ((maxLvlRight - minLvlRight) < TOP) maxLvlRight = minLvlRight + TOP;
  318.   minLvlAvgRight = (minLvlAvgRight * 63 + minLvlRight) >> 6; // Dampen min/max levels
  319.   maxLvlAvgRight = (maxLvlAvgRight * 63 + maxLvlRight) >> 6; // (fake rolling average)
  320. }
  321.  
  322.  
  323. void Vu3() {
  324.  
  325.   uint8_t i;
  326.   uint16_t minLvlLeft, maxLvlLeft;
  327.   uint16_t minLvlRight, maxLvlRight;
  328.   int n, n1, height;
  329.  
  330.   n = analogRead(MIC_PIN);             // Raw reading from mic
  331.   n = abs(n - 512 - DC_OFFSET);        // Center on zero
  332.   n = (n <= NOISE) ? 0 : (n - NOISE);  // Remove noise/hum
  333.   lvlLeft = ((lvlLeft * 7) + n) >> 3;    // "Dampened" reading (else looks twitchy)
  334.  
  335.   // Calculate bar height based on dynamic min/max levels (fixed point):
  336.   height = TOP * (lvlLeft - minLvlAvgLeft) / (long)(maxLvlAvgLeft - minLvlAvgLeft);
  337.  
  338.   if (height < 0L)       height = 0;      // Clip output
  339.   else if (height > TOP) height = TOP;
  340.   if (height > peakLeft)     peakLeft   = height; // Keep 'peak' dot at top
  341.  
  342.   greenOffset += SPEED;
  343.   blueOffset += SPEED;
  344.   if (greenOffset >= 255) greenOffset = 0;
  345.   if (blueOffset >= 255) blueOffset = 0;
  346.  
  347.   // Color pixels based on rainbow gradient
  348.   for (i = 0; i < N_PIXELS_QUARTER; i++) {
  349.     if (i >= height) {
  350.       strip.setPixelColor(N_PIXELS_QUARTER - i - 1, 0, 0, 0);
  351.     } else {
  352.       uint32_t color = Wheel(map(N_PIXELS_QUARTER - i - 1, 1, strip.numPixels() - 1, (int)greenOffset, (int)blueOffset));
  353.       strip.setPixelColor(N_PIXELS_QUARTER - i - 1, color);
  354.     }
  355.   }
  356.   // Draw peak dot
  357.   if (peakLeft > 0 && peakLeft <= N_PIXELS_QUARTER - 1) {
  358.     uint32_t color = Wheel(map(N_PIXELS_QUARTER - peakLeft - 1, 0, strip.numPixels() - 1, 30, 150));
  359.     strip.setPixelColor(N_PIXELS_QUARTER - peakLeft - 1, color);
  360.   }
  361.   strip.show(); // Update strip
  362.  
  363.   // Every few frames, make the peak pixel drop by 1:
  364.  
  365.   if (++dotCountLeft >= PEAK_FALL) { //fall rate
  366.  
  367.     if (peakLeft > 0) peakLeft--;
  368.     dotCountLeft = 0;
  369.   }
  370.   strip.show();  // Update strip
  371.  
  372.   volLeft[volCountLeft] = n;
  373.   if (++volCountLeft >= SAMPLES) {
  374.     volCountLeft = 0;
  375.   }
  376.  
  377.   // Get volume range of prior frames
  378.   minLvlLeft = maxLvlLeft = vol[0];
  379.   for (i = 1; i < SAMPLES; i++) {
  380.     if (volLeft[i] < minLvlLeft) {
  381.       minLvlLeft = volLeft[i];
  382.     } else if (volLeft[i] > maxLvlLeft) {
  383.       maxLvlLeft = volLeft[i];
  384.     }
  385.   }
  386.  
  387.   // minLvl and maxLvl indicate the volume range over prior frames, used
  388.   // for vertically scaling the output graph (so it looks interesting
  389.   // regardless of volume level).  If they're too close together though
  390.   // (e.g. at very low volume levels) the graph becomes super coarse
  391.   // and 'jumpy'...so keep some minimum distance between them (this
  392.   // also lets the graph go to zero when no sound is playing):
  393.   if ((maxLvlLeft - minLvlLeft) < TOP) {
  394.     maxLvlLeft = minLvlLeft + TOP;
  395.   }
  396.   minLvlAvgLeft = (minLvlAvgLeft * 63 + minLvlLeft) >> 6; // Dampen min/max levels
  397.   maxLvlAvgLeft = (maxLvlAvgLeft * 63 + maxLvlLeft) >> 6; // (fake rolling average)
  398.  
  399.   n1 = analogRead(MIC_PIN_2);             // Raw reading from mic
  400.   n1 = abs(n1 - 512 - DC_OFFSET);        // Center on zero
  401.   n1 = (n1 <= NOISE) ? 0 : (n1 - NOISE);  // Remove noise/hum
  402.   lvlRight = ((lvlRight * 7) + n1) >> 3;    // "Dampened" reading (else looks twitchy)
  403.  
  404.   // Calculate bar height based on dynamic min/max levels (fixed point):
  405.   height = TOP * (lvlRight - minLvlAvgRight) / (long)(maxLvlAvgRight - minLvlAvgRight);
  406.  
  407.   if (height < 0L)       height = 0;      // Clip output
  408.   else if (height > TOP) height = TOP;
  409.   if (height > peakRight)     peakRight   = height; // Keep 'peak' dot at top
  410.  
  411.   greenOffset += SPEED;
  412.   blueOffset += SPEED;
  413.   if (greenOffset >= 255) greenOffset = 0;
  414.   if (blueOffset >= 255) blueOffset = 0;
  415.  
  416.   // Color pixels based on rainbow gradient
  417.   for (i = 0; i < N_PIXELS_QUARTER; i++) {
  418.     if (i >= height) {
  419.       strip.setPixelColor(N_PIXELS_QUARTER + i, 0, 0, 0);
  420.     } else {
  421.       uint32_t color = Wheel(map(N_PIXELS_QUARTER + i, 0, strip.numPixels(), (int)greenOffset, (int)blueOffset));
  422.       strip.setPixelColor(N_PIXELS_QUARTER + i, color);
  423.     }
  424.   }
  425.   // Draw peak dot
  426.   if (peakRight > 0 && peakRight <= N_PIXELS_QUARTER) {
  427.     uint32_t color =  Wheel(map(N_PIXELS_QUARTER + peakLeft, 0, strip.numPixels(), 30, 150));
  428.     strip.setPixelColor(N_PIXELS_QUARTER + peakLeft, color);
  429.   }
  430.   strip.show(); // Update strip
  431.  
  432.   // Every few frames, make the peak pixel drop by 1:
  433.  
  434.   if (++dotCountRight >= PEAK_FALL) { //fall rate
  435.  
  436.     if (peakRight > 0) peakRight--;
  437.     dotCountRight = 0;
  438.   }
  439.   strip.show();  // Update strip
  440.  
  441.   volRight[volCountRight] = n1;
  442.   if (++volCountRight >= SAMPLES2) {
  443.     volCountRight = 0;
  444.   }
  445.  
  446.   // Get volume range of prior frames
  447.   minLvlRight = maxLvlRight = vol[0];
  448.   for (i = 1; i < SAMPLES2; i++) {
  449.     if (volRight[i] < minLvlRight) {
  450.       minLvlRight = volRight[i];
  451.     } else if (volRight[i] > maxLvlRight) {
  452.       maxLvlRight = volRight[i];
  453.     }
  454.   }
  455.  
  456.   // minLvl and maxLvl indicate the volume range over prior frames, used
  457.   // for vertically scaling the output graph (so it looks interesting
  458.   // regardless of volume level).  If they're too close together though
  459.   // (e.g. at very low volume levels) the graph becomes super coarse
  460.   // and 'jumpy'...so keep some minimum distance between them (this
  461.   // also lets the graph go to zero when no sound is playing):
  462.   if ((maxLvlRight - minLvlRight) < TOP) {
  463.     maxLvlRight = minLvlRight + TOP;
  464.   }
  465.   minLvlAvgRight = (minLvlAvgRight * 63 + minLvlRight) >> 6; // Dampen min/max levels
  466.   maxLvlAvgRight = (maxLvlAvgRight * 63 + maxLvlRight) >> 6; // (fake rolling average)
  467. }
  468.  
  469.  
  470. void Balls() {
  471.   for (int i = 0 ; i < NUM_BALLS ; i++) {
  472.     tCycle[i] =  millis() - tLast[i] ;     // Calculate the time since the last time the ball was on the ground
  473.  
  474.     // A little kinematics equation calculates positon as a function of time, acceleration (gravity) and intial velocity
  475.     h[i] = 0.5 * GRAVITY * pow( tCycle[i] / 1000 , 2.0 ) + vImpact[i] * tCycle[i] / 1000;
  476.  
  477.     if ( h[i] < 0 ) {
  478.       h[i] = 0;                            // If the ball crossed the threshold of the "ground," put it back on the ground
  479.       vImpact[i] = COR[i] * vImpact[i] ;   // and recalculate its new upward velocity as it's old velocity * COR
  480.       tLast[i] = millis();
  481.  
  482.       if ( vImpact[i] < 0.01 ) vImpact[i] = vImpact0;  // If the ball is barely moving, "pop" it back up at vImpact0
  483.     }
  484.     pos[i] = round( h[i] * (N_PIXELS - 1) / h0);       // Map "h" to a "pos" integer index position on the LED strip
  485.   }
  486.  
  487.   //Choose color of LEDs, then the "pos" LED on
  488.   for (int i = 0 ; i < NUM_BALLS ; i++) {
  489.   leds[pos[i]] = CHSV( uint8_t (i * 40) , 255, 255);
  490.   }
  491.   FastLED.show();
  492.   //Then off for the next loop around
  493.   for (int i = 0 ; i < NUM_BALLS ; i++) {
  494.     leds[pos[i]] = CRGB::Black;
  495.   }
  496. }
  497.  
  498.  
  499.  
  500. // Slightly different, this makes the rainbow equally distributed throughout
  501. void rainbowCycle(uint8_t wait) {
  502.   uint16_t i, j;
  503.  
  504.   for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
  505.     for (i = 0; i < strip.numPixels(); i++) {
  506.       strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
  507.     }
  508.     strip.show();
  509.     if (Pattern != nPatterns)  // <------------- add this
  510.       return;         // <------------ and this
  511.     delay(wait);
  512.   }
  513. }
  514. // HERE
  515.  
  516.  
  517. void vu() {
  518.  
  519.   uint8_t  i;
  520.   uint16_t minLvlLeft, maxLvlLeft;
  521.   uint16_t minLvlRight, maxLvlRight;
  522.   int      n, n1, height;
  523.  
  524.   n   = analogRead(MIC_PIN);                        // Raw reading from mic
  525.   n   = abs(n - 512 - DC_OFFSET); // Center on zero
  526.   n   = (n <= NOISE) ? 0 : (n - NOISE);             // Remove noise/hum
  527.   lvlLeft = ((lvlLeft * 7) + n) >> 3;    // "Dampened" reading (else looks twitchy)
  528.  
  529.   // Calculate bar height based on dynamic min/max levels (fixed point):
  530.   height = TOP * (lvlLeft - minLvlAvgLeft) / (long)(maxLvlAvgLeft - minLvlAvgLeft);
  531.  
  532.   if (height < 0L)       height = 0;     // Clip output
  533.   else if (height > TOP) height = TOP;
  534.   if (height > peakLeft)     peakLeft   = height; // Keep 'peak' dot at top
  535.  
  536.  
  537.   // Color pixels based on rainbow gradient
  538.   for (i = 0; i < N_PIXELS_HALF; i++) {
  539.     if (i >= height) {
  540.       strip.setPixelColor(N_PIXELS_HALF - i - 1,   0,   0, 0);
  541.     } else {
  542.       uint32_t color = Wheel(map(i, 0, strip.numPixels() - 1, 30, 150));
  543.       strip.setPixelColor(N_PIXELS_HALF - i - 1, color);
  544.     }
  545.  
  546.   }
  547.  
  548.  
  549.   // Draw peak dot
  550.   if (peakLeft > 0 && peakLeft <= N_PIXELS_HALF - 1) {
  551.     uint32_t color = Wheel(map(N_PIXELS_HALF - peakLeft - 1, 0, strip.numPixels() - 1, 30, 150));
  552.     strip.setPixelColor(N_PIXELS_HALF - peakLeft - 1, color);
  553.   }
  554.   strip.show(); // Update strip
  555.  
  556.   // Every few frames, make the peak pixel drop by 1:
  557.  
  558.   if (++dotCountLeft >= PEAK_FALL) { //fall rate
  559.     if (peakLeft > 0) peakLeft--;
  560.     dotCountLeft = 0;
  561.   }
  562.  
  563.   volLeft[volCountLeft] = n;                      // Save sample for dynamic leveling
  564.   if (++volCountLeft >= SAMPLES) volCountLeft = 0; // Advance/rollover sample counter
  565.  
  566.   // Get volume range of prior frames
  567.   minLvlLeft = maxLvlLeft = vol[0];
  568.   for (i = 1; i < SAMPLES; i++) {
  569.     if (volLeft[i] < minLvlLeft)      minLvlLeft = volLeft[i];
  570.     else if (volLeft[i] > maxLvlLeft) maxLvlLeft = volLeft[i];
  571.   }
  572.   // minLvl and maxLvl indicate the volume range over prior frames, used
  573.   // for vertically scaling the output graph (so it looks interesting
  574.   // regardless of volume level).  If they're too close together though
  575.   // (e.g. at very low volume levels) the graph becomes super coarse
  576.   // and 'jumpy'...so keep some minimum distance between them (this
  577.   // also lets the graph go to zero when no sound is playing):
  578.   if ((maxLvlLeft - minLvlLeft) < TOP) maxLvlLeft = minLvlLeft + TOP;
  579.   minLvlAvgLeft = (minLvlAvgLeft * 63 + minLvlLeft) >> 6; // Dampen min/max levels
  580.   maxLvlAvgLeft = (maxLvlAvgLeft * 63 + maxLvlLeft) >> 6; // (fake rolling average)
  581.  
  582.   n1   = analogRead(MIC_PIN_2);                        // Raw reading from mic
  583.   n1   = abs(n1 - 512 - DC_OFFSET); // Center on zero
  584.   n1   = (n1 <= NOISE) ? 0 : (n1 - NOISE);             // Remove noise/hum
  585.   lvlRight = ((lvlRight * 7) + n1) >> 3;    // "Dampened" reading (else looks twitchy)
  586.  
  587.   // Calculate bar height based on dynamic min/max levels (fixed point):
  588.   height = TOP * (lvlRight - minLvlAvgRight) / (long)(maxLvlAvgRight - minLvlAvgRight);
  589.  
  590.   if (height < 0L)       height = 0;     // Clip output
  591.   else if (height > TOP) height = TOP;
  592.   if (height > peakRight)     peakRight   = height; // Keep 'peak' dot at top
  593.  
  594.  
  595.   // Color pixels based on rainbow gradient
  596.   for (i = 0; i < N_PIXELS_HALF; i++) {
  597.     if (i >= height)  {
  598.       strip.setPixelColor(N_PIXELS_HALF + i,   0,   0, 0);
  599.     }
  600.     else {
  601.       uint32_t color =  Wheel(map(N_PIXELS_HALF + i, 0, strip.numPixels(), 30, 150));
  602.       strip.setPixelColor(N_PIXELS_HALF + i, color);
  603.     }
  604.   }
  605.  
  606.  
  607.   // Draw peak dot
  608.   if (peakRight > 0 && peakRight <= N_PIXELS_HALF) strip.setPixelColor(N_PIXELS_HALF + peakRight, Wheel(map(N_PIXELS_HALF + peakRight, 0, strip.numPixels(), 30, 150)));
  609.  
  610.   strip.show(); // Update strip
  611.  
  612.   // Every few frames, make the peak pixel drop by 1:
  613.  
  614.   if (++dotCountRight >= PEAK_FALL) { //fall rate
  615.  
  616.     if (peakRight > 0) peakRight--;
  617.     dotCountRight = 0;
  618.   }
  619.  
  620.  
  621.  
  622.   volRight[volCountRight] = n1;                      // Save sample for dynamic leveling
  623.   if (++volCountRight >= SAMPLES2) volCountRight = 0; // Advance/rollover sample counter
  624.  
  625.   // Get volume range of prior frames
  626.   minLvlRight = maxLvlRight = vol[0];
  627.   for (i = 1; i < SAMPLES2; i++) {
  628.     if (volRight[i] < minLvlRight)      minLvlRight = volRight[i];
  629.     else if (volRight[i] > maxLvlRight) maxLvlRight = volRight[i];
  630.   }
  631.   // minLvl and maxLvl indicate the volume range over prior frames, used
  632.   // for vertically scaling the output graph (so it looks interesting
  633.   // regardless of volume level).  If they're too close together though
  634.   // (e.g. at very low volume levels) the graph becomes super coarse
  635.   // and 'jumpy'...so keep some minimum distance between them (this
  636.   // also lets the graph go to zero when no sound is playing):
  637.   if ((maxLvlRight - minLvlRight) < TOP) maxLvlRight = minLvlRight + TOP;
  638.   minLvlAvgRight = (minLvlAvgRight * 63 + minLvlRight) >> 6; // Dampen min/max levels
  639.   maxLvlAvgRight = (maxLvlAvgRight * 63 + maxLvlRight) >> 6; // (fake rolling average)
  640.  
  641. }
  642.  
  643. // Input a value 0 to 255 to get a color value.
  644. // The colors are a transition r - g - b - back to r.
  645. uint32_t Wheel(byte WheelPos) {
  646.   if (WheelPos < 85) {
  647.     return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  648.   } else if (WheelPos < 170) {
  649.     WheelPos -= 85;
  650.     return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  651.   } else {
  652.     WheelPos -= 170;
  653.     return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  654.   }
  655. }
  656.  
  657.  
  658. void vu2() {
  659.  
  660.   uint8_t  i;
  661.   uint16_t minLvlLeft, maxLvlLeft;
  662.   uint16_t minLvlRight, maxLvlRight;
  663.   int      n, n1, height;
  664.  
  665.   n   = analogRead(MIC_PIN);                        // Raw reading from mic
  666.   n   = abs(n - 512 - DC_OFFSET); // Center on zero
  667.   n   = (n <= NOISE) ? 0 : (n - NOISE);             // Remove noise/hum
  668.   lvlLeft = ((lvlLeft * 7) + n) >> 3;    // "Dampened" reading (else looks twitchy)
  669.  
  670.   // Calculate bar height based on dynamic min/max levels (fixed point):
  671.   height = TOP * (lvlLeft - minLvlAvgLeft) / (long)(maxLvlAvgLeft - minLvlAvgLeft);
  672.  
  673.   if (height < 0L)       height = 0;     // Clip output
  674.   else if (height > TOP) height = TOP;
  675. //  if (height > peakLeft)     peakLeft   = height; // Keep 'peak' dot at top
  676.  
  677.  
  678.   // Color pixels based on rainbow gradient
  679.   for (i = 0; i < N_PIXELS_HALF; i++) {
  680.     if (i >= height) {
  681.       strip.setPixelColor(N_PIXELS_HALF - i - 1,   0,   0, 0);
  682.     } else {
  683.       uint32_t color = Wheel1(map(i, 128, strip.numPixels() - 1, 30, 150));
  684.       strip.setPixelColor(N_PIXELS_HALF - i - 1, color);
  685.     }
  686.  
  687.   }
  688.  
  689.   // Draw peak dot
  690.   if (peakLeft > 0 && peakLeft <= N_PIXELS_HALF - 1) {
  691.     uint32_t color = Wheel1(map(N_PIXELS_HALF - peakLeft - 1, 0, strip.numPixels() - 1, 30, 150));
  692.     strip.setPixelColor(N_PIXELS_HALF - peakLeft - 1, color);
  693.   }
  694.  
  695.   strip.show(); // Update strip
  696.  
  697.   // Every few frames, make the peak pixel drop by 1:
  698.  
  699.   if (++dotCountLeft >= PEAK_FALL) { //fall rate
  700.  
  701.     if (peakLeft > 0) peakLeft--;
  702.     dotCountLeft = 0;
  703.   }
  704.  
  705.  
  706.  
  707.   volLeft[volCountLeft] = n;                      // Save sample for dynamic leveling
  708.   if (++volCountLeft >= SAMPLES) volCountLeft = 0; // Advance/rollover sample counter
  709.  
  710.   // Get volume range of prior frames
  711.   minLvlLeft = maxLvlLeft = vol[0];
  712.   for (i = 1; i < SAMPLES; i++) {
  713.     if (volLeft[i] < minLvlLeft)      minLvlLeft = volLeft[i];
  714.     else if (volLeft[i] > maxLvlLeft) maxLvlLeft = volLeft[i];
  715.   }
  716.   // minLvl and maxLvl indicate the volume range over prior frames, used
  717.   // for vertically scaling the output graph (so it looks interesting
  718.   // regardless of volume level).  If they're too close together though
  719.   // (e.g. at very low volume levels) the graph becomes super coarse
  720.   // and 'jumpy'...so keep some minimum distance between them (this
  721.   // also lets the graph go to zero when no sound is playing):
  722.   if ((maxLvlLeft - minLvlLeft) < TOP) maxLvlLeft = minLvlLeft + TOP;
  723.   minLvlAvgLeft = (minLvlAvgLeft * 63 + minLvlLeft) >> 6; // Dampen min/max levels
  724.   maxLvlAvgLeft = (maxLvlAvgLeft * 63 + maxLvlLeft) >> 6; // (fake rolling average)
  725.  
  726.   n1   = analogRead(MIC_PIN_2);                        // Raw reading from mic
  727.   n1   = abs(n1 - 512 - DC_OFFSET); // Center on zero
  728.   n1   = (n1 <= NOISE) ? 0 : (n1 - NOISE);             // Remove noise/hum
  729.   lvlRight = ((lvlRight * 7) + n1) >> 3;    // "Dampened" reading (else looks twitchy)
  730.  
  731.   // Calculate bar height based on dynamic min/max levels (fixed point):
  732.   height = TOP * (lvlRight - minLvlAvgRight) / (long)(maxLvlAvgRight - minLvlAvgRight);
  733.  
  734.   if (height < 0L)       height = 0;     // Clip output
  735.   else if (height > TOP) height = TOP;
  736. //  if (height > peakRight)     peakRight   = height; // Keep 'peak' dot at top
  737.  
  738.  
  739.   // Color pixels based on rainbow gradient
  740.   for (i = 0; i < N_PIXELS_HALF; i++) {
  741.     if (i >= height)  {
  742.       strip.setPixelColor(N_PIXELS_HALF + i,   0,   0, 0);
  743.     }
  744.     else {
  745.       uint32_t color =  Wheel1(map(N_PIXELS_HALF + i, 128, strip.numPixels(), 30, 150));
  746.       strip.setPixelColor(N_PIXELS_HALF + i, color);
  747.     }
  748.   }
  749.  
  750.  
  751.   // Draw peak dot
  752.   if (peakRight > 0 && peakRight <= N_PIXELS_HALF) {
  753.   uint32_t color = Wheel1(map(N_PIXELS_HALF + peakRight, 0, strip.numPixels(), 30, 150));
  754.   strip.setPixelColor(N_PIXELS_HALF + peakRight, color);
  755.   }
  756.   strip.show(); // Update strip
  757.  
  758.   // Every few frames, make the peak pixel drop by 1:
  759.  
  760.   if (++dotCountRight >= PEAK_FALL) { //fall rate
  761.  
  762.     if (peakRight > 0) peakRight--;
  763.     dotCountRight = 0;
  764.   }
  765.  
  766.  
  767.  
  768.   volRight[volCountRight] = n1;                      // Save sample for dynamic leveling
  769.   if (++volCountRight >= SAMPLES2) volCountRight = 0; // Advance/rollover sample counter
  770.  
  771.   // Get volume range of prior frames
  772.   minLvlRight = maxLvlRight = vol[0];
  773.   for (i = 1; i < SAMPLES2; i++) {
  774.     if (volRight[i] < minLvlRight)      minLvlRight = volRight[i];
  775.     else if (volRight[i] > maxLvlRight) maxLvlRight = volRight[i];
  776.   }
  777.   // minLvl and maxLvl indicate the volume range over prior frames, used
  778.   // for vertically scaling the output graph (so it looks interesting
  779.   // regardless of volume level).  If they're too close together though
  780.   // (e.g. at very low volume levels) the graph becomes super coarse
  781.   // and 'jumpy'...so keep some minimum distance between them (this
  782.   // also lets the graph go to zero when no sound is playing):
  783.   if ((maxLvlRight - minLvlRight) < TOP) maxLvlRight = minLvlRight + TOP;
  784.   minLvlAvgRight = (minLvlAvgRight * 63 + minLvlRight) >> 6; // Dampen min/max levels
  785.   maxLvlAvgRight = (maxLvlAvgRight * 63 + maxLvlRight) >> 6; // (fake rolling average)
  786.  
  787. }
  788.  
  789. // Input a value 0 to 255 to get a color value.
  790. // The colors are a transition r - g - b - back to r.
  791. uint32_t Wheel1(byte WheelPos) {
  792.   if (WheelPos < 85) {
  793.     return strip.Color(WheelPos * 5, 255 - WheelPos * 5, 0);
  794.   } else if (WheelPos < 170) {
  795.     WheelPos -= 85;
  796.     return strip.Color(255 - WheelPos * 5, 0, WheelPos * 5);
  797.   } else {
  798.     WheelPos -= 170;
  799.     return strip.Color(0, WheelPos * 5, 255 - WheelPos * 5);
  800.   }
  801. }
  802.  
  803.  
  804.  
  805. //here................
  806.  
  807. void ripple() {
  808.  
  809.   if (currentBg == nextBg) {
  810.     nextBg = random(256);
  811.   }
  812.   else if (nextBg > currentBg) {
  813.     currentBg++;
  814.   } else {
  815.     currentBg--;
  816.   }
  817.   for (uint16_t l = 0; l < N_PIXELS; l++) {
  818.     leds[l] = CHSV(currentBg, 255, 50);         // strip.setPixelColor(l, Wheel(currentBg, 0.1));
  819.   }
  820.  
  821.   if (step == -1) {
  822.     center = random(N_PIXELS);
  823.     color = random(256);
  824.     step = 0;
  825.   }
  826.  
  827.   if (step == 0) {
  828.     leds[center] = CHSV(color, 255, 255);         // strip.setPixelColor(center, Wheel(color, 1));
  829.     step ++;
  830.   }
  831.   else {
  832.     if (step < maxSteps) {
  833.  
  834.       leds[wrap(center + step)] = CHSV(color, 255, pow(fadeRate, step) * 255);     //   strip.setPixelColor(wrap(center + step), Wheel(color, pow(fadeRate, step)));
  835.       leds[wrap(center - step)] = CHSV(color, 255, pow(fadeRate, step) * 255);     //   strip.setPixelColor(wrap(center - step), Wheel(color, pow(fadeRate, step)));
  836.       if (step > 3) {
  837.         leds[wrap(center + step - 3)] = CHSV(color, 255, pow(fadeRate, step - 2) * 255);   //   strip.setPixelColor(wrap(center + step - 3), Wheel(color, pow(fadeRate, step - 2)));
  838.         leds[wrap(center - step + 3)] = CHSV(color, 255, pow(fadeRate, step - 2) * 255);   //   strip.setPixelColor(wrap(center - step + 3), Wheel(color, pow(fadeRate, step - 2)));
  839.       }
  840.       step ++;
  841.     }
  842.     else {
  843.       step = -1;
  844.     }
  845.   }
  846.   LEDS.show();
  847.   delay(50);
  848. }
  849.  
  850.  
  851. int wrap(int step) {
  852.   if (step < 0) return N_PIXELS + step;
  853.   if (step > N_PIXELS - 1) return step - N_PIXELS;
  854.   return step;
  855. }
  856.  
  857.  
  858. void one_color_allHSV(int ahue, int abright) {                // SET ALL LEDS TO ONE COLOR (HSV)
  859.   for (int i = 0 ; i < N_PIXELS; i++ ) {
  860.     leds[i] = CHSV(ahue, 255, abright);
  861.   }
  862. }
  863.  
  864.  
  865. void ripple2() {
  866.   if (BG) {
  867.     if (currentBg == nextBg) {
  868.       nextBg = random(256);
  869.     }
  870.     else if (nextBg > currentBg) {
  871.       currentBg++;
  872.     } else {
  873.       currentBg--;
  874.     }
  875.     for (uint16_t l = 0; l < N_PIXELS; l++) {
  876.       strip.setPixelColor(l, Wheel(currentBg, 0.1));
  877.     }
  878.   } else {
  879.     for (uint16_t l = 0; l < N_PIXELS; l++) {
  880.       strip.setPixelColor(l, 0, 0, 0);
  881.     }
  882.   }
  883.  
  884.  
  885.   if (step == -1) {
  886.     center = random(N_PIXELS);
  887.     color = random(256);
  888.     step = 0;
  889.   }
  890.  
  891.  
  892.  
  893.   if (step == 0) {
  894.     strip.setPixelColor(center, Wheel(color, 1));
  895.     step ++;
  896.   }
  897.   else {
  898.     if (step < maxSteps) {
  899.       strip.setPixelColor(wrap(center + step), Wheel(color, pow(fadeRate, step)));
  900.       strip.setPixelColor(wrap(center - step), Wheel(color, pow(fadeRate, step)));
  901.       if (step > 3) {
  902.         strip.setPixelColor(wrap(center + step - 3), Wheel(color, pow(fadeRate, step - 2)));
  903.         strip.setPixelColor(wrap(center - step + 3), Wheel(color, pow(fadeRate, step - 2)));
  904.       }
  905.       step ++;
  906.     }
  907.     else {
  908.       step = -1;
  909.     }
  910.   }
  911.   strip.show();
  912.   delay(50);
  913. }
  914.  
  915. // Input a value 0 to 255 to get a color value.
  916. // The colours are a transition r - g - b - back to r.
  917. uint32_t Wheel(byte WheelPos, float opacity) {
  918.  
  919.   if (WheelPos < 85) {
  920.     return strip.Color((WheelPos * 3) * opacity, (255 - WheelPos * 3) * opacity, 0);
  921.   }
  922.   else if (WheelPos < 170) {
  923.     WheelPos -= 85;
  924.     return strip.Color((255 - WheelPos * 3) * opacity, 0, (WheelPos * 3) * opacity);
  925.   }
  926.   else {
  927.     WheelPos -= 170;
  928.     return strip.Color(0, (WheelPos * 3) * opacity, (255 - WheelPos * 3) * opacity);
  929.   }
  930. }
  931.  
  932.  
  933. void pattern2() {
  934.  
  935.   sinelon();                                                  // Call our sequence.
  936.   show_at_max_brightness_for_power();                         // Power managed display of LED's.
  937. } // loop()
  938.  
  939.  
  940. void sinelon() {
  941.   // a colored dot sweeping back and forth, with fading trails
  942.   fadeToBlackBy( leds, N_PIXELS, thisfade);
  943.   int pos1 = beatsin16(thisbeat, 0, N_PIXELS);
  944.   int pos2 = beatsin16(thatbeat, 0, N_PIXELS);
  945.   leds[(pos1 + pos2) / 2] += CHSV( myhue++ / 64, thissat, thisbri);
  946. }
  947. // Pattern 3 - JUGGLE
  948. void pattern3() {
  949.   ChangeMe();
  950.   juggle();
  951.   show_at_max_brightness_for_power();                         // Power managed display of LED's.
  952. } // loop()
  953.  
  954.  
  955. void juggle() {                                               // Several colored dots, weaving in and out of sync with each other
  956.   curhue = thishue;                                          // Reset the hue values.
  957.   fadeToBlackBy(leds, N_PIXELS, faderate);
  958.   for ( int i = 0; i < numdots; i++) {
  959.     leds[beatsin16(basebeat + i + numdots, 0, N_PIXELS)] += CHSV(curhue, thissat, thisbright); //beat16 is a FastLED 3.1 function
  960.     curhue += hueinc;
  961.   }
  962. } // juggle()
  963.  
  964.  
  965. void ChangeMe() {                                             // A time (rather than loop) based demo sequencer. This gives us full control over the length of each sequence.
  966.   uint8_t secondHand = (millis() / 1000) % 30;                // IMPORTANT!!! Change '30' to a different value to change duration of the loop.
  967.   static uint8_t lastSecond = 99;                             // Static variable, means it's only defined once. This is our 'debounce' variable.
  968.   if (lastSecond != secondHand) {                             // Debounce to make sure we're not repeating an assignment.
  969.     lastSecond = secondHand;
  970.     if (secondHand ==  0)  {
  971.       numdots = 1;  // You can change values here, one at a time , or altogether.
  972.       faderate = 2;
  973.     }
  974.     if (secondHand == 10)  {
  975.       numdots = 4;
  976.       thishue = 128;
  977.       faderate = 8;
  978.     }
  979.     if (secondHand == 20)  {
  980.       hueinc = 48;  // Only gets called once, and not continuously for the next several seconds. Therefore, no rainbows.
  981.       thishue = random8();
  982.     }
  983.   }
  984. } // ChangeMe()
  985.  
  986. void Twinkle () {
  987.   if (random(25) == 1) {
  988.     uint16_t i = random(N_PIXELS);
  989.     if (redStates[i] < 1 && greenStates[i] < 1 && blueStates[i] < 1) {
  990.       redStates[i] = random(256);
  991.       greenStates[i] = random(256);
  992.       blueStates[i] = random(256);
  993.     }
  994.   }
  995.  
  996.   for (uint16_t l = 0; l < N_PIXELS; l++) {
  997.     if (redStates[l] > 1 || greenStates[l] > 1 || blueStates[l] > 1) {
  998.       strip.setPixelColor(l, redStates[l], greenStates[l], blueStates[l]);
  999.  
  1000.       if (redStates[l] > 1) {
  1001.         redStates[l] = redStates[l] * Fade;
  1002.       } else {
  1003.         redStates[l] = 0;
  1004.       }
  1005.  
  1006.       if (greenStates[l] > 1) {
  1007.         greenStates[l] = greenStates[l] * Fade;
  1008.       } else {
  1009.         greenStates[l] = 0;
  1010.       }
  1011.  
  1012.       if (blueStates[l] > 1) {
  1013.         blueStates[l] = blueStates[l] * Fade;
  1014.       } else {
  1015.         blueStates[l] = 0;
  1016.       }
  1017.     } else {
  1018.       strip.setPixelColor(l, 0, 0, 0);
  1019.     }
  1020.   }
  1021.   strip.show();
  1022.   delay(10);
  1023. }
  1024.  
  1025. // TOO HERE
  1026.  
  1027. void rainbow(uint8_t wait) {
  1028.   uint16_t i, j;
  1029.  
  1030.   for (j = 0; j < 256; j++) {
  1031.     for (i = 0; i < strip.numPixels(); i++) {
  1032.       strip.setPixelColor(i, Wheel((i + j) & 255));
  1033.     }
  1034.     strip.show();
  1035.     // check if a button pressed
  1036.     if (Pattern != nPatterns)  // <------------- add this
  1037.       return;         // <------------ and this
  1038.     delay(wait);
  1039.   }
  1040. }
  1041.  
  1042. // List of patterns to cycle through.  Each is defined as a separate function below.
  1043. typedef void (*SimplePatternList[])();
  1044. SimplePatternList qPatterns = {vu, vu2, Vu3, Vu4, Twinkle, pattern3, pattern2, ripple2, ripple, Balls};
  1045. uint8_t qCurrentPatternNumber = 0; // Index number of which pattern is current
  1046.  
  1047. void nextPattern()
  1048. {
  1049.   // add one to the current pattern number, and wrap around at the end
  1050.   qCurrentPatternNumber = (qCurrentPatternNumber + 1) % ARRAY_SIZE( qPatterns);
  1051. }
  1052. void All()
  1053. {
  1054.   // Call the current pattern function once, updating the 'leds' array
  1055.   qPatterns[qCurrentPatternNumber]();
  1056.   EVERY_N_MINUTES( 1 ) {
  1057.     nextPattern();  // change patterns periodically
  1058.   }
  1059. }

Raw Paste


Login or Register to edit or fork this paste. It's free.