CPP   49

code

Guest on 12th January 2022 05:27:05 PM

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

Raw Paste


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