Advertisement
Guest User

Code that needs to be modified.

a guest
Nov 25th, 2016
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.83 KB | None | 0 0
  1. #include "FastLED.h"
  2.  
  3. #define DATA_PIN 0
  4. #define MIC_PIN A1
  5. #define DC_OFFSET 0
  6. #define NOISE 50
  7. #define SAMPLES 60
  8. #define TOP (NUM_LEDS +2) // Allow dot to go slightly off scale
  9. #define PEAK_FALL 20
  10. #define BUTTON_LEAD 4
  11. #define NUMBER_OF_MODES 6
  12. #define LED_TYPE WS2812B
  13. #define COLOR_ORDER GRB
  14. #define NUM_LEDS 175
  15. CRGB leds[NUM_LEDS];
  16.  
  17.  
  18. #define BRIGHTNESS 255
  19. #define FRAMES_PER_SECOND 120
  20.  
  21. //The following constant is the delay between each pixel flash
  22. const int interval = 25;
  23. unsigned long previousMillis = millis();
  24. int mode = 1; //Default mode is one.
  25.  
  26. byte
  27. peak = 0, // Used for falling dot
  28. dotCount = 0, // Frame counter for delaying dot-falling speed
  29. volCount = 0; // Frame counter for storing past volume data
  30.  
  31. int
  32. vol[SAMPLES], // Collection of prior volume samples
  33. lvl = 10, // Current "dampened" audio level
  34. minLvlAvg = 0, // For dynamic adjustment of graph low & high
  35. maxLvlAvg = 512;
  36.  
  37. bool buttonState = LOW;
  38. bool lastButtonState = LOW;
  39.  
  40.  
  41. void setup(){
  42. pinMode(BUTTON_LEAD, INPUT_PULLUP);
  43. memset(vol,0,sizeof(int)*SAMPLES);//Thanks Neil!
  44. FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  45. FastLED.setBrightness(BRIGHTNESS);
  46. }
  47.  
  48. uint8_t gHue = 0;
  49.  
  50. void loop() {
  51.  
  52. switch (mode) {
  53. case 1:
  54. sinelon();
  55. break;
  56.  
  57. case 2:
  58. vuscanner();
  59. break;
  60.  
  61. case 3:
  62. confetti();
  63. break;
  64.  
  65. case 4:
  66. juggle();
  67. break;
  68.  
  69. case 5:
  70. bpm();
  71. break;
  72.  
  73. default:
  74. mode = 1;
  75. break;
  76. }
  77. FastLED.show();
  78. EVERY_N_MILLISECONDS( 20 ) { gHue++; }
  79. }
  80.  
  81. /* monitor button press */
  82. bool buttonListener() {
  83. bool modeChanged = false;
  84.  
  85. buttonState = digitalRead(BUTTON_LEAD);
  86.  
  87. if (buttonState != lastButtonState) {
  88. if (buttonState == LOW) {
  89. mode++;
  90. modeChanged = true;
  91. delay(250); // Debounce delay. I checked the best parameter and 250 was it.
  92. }
  93. }
  94. lastButtonState = buttonState;
  95.  
  96. return modeChanged;
  97. }
  98.  
  99. void sinelon() {
  100. if(buttonListener()) { return; }
  101. if(millis() - previousMillis >= interval) {
  102. previousMillis = millis();
  103. fadeToBlackBy(leds, NUM_LEDS, 20);
  104. int pos = beatsin16(13,0,NUM_LEDS);
  105. leds[pos] += CHSV(gHue, 255, 192);
  106. }
  107. }
  108.  
  109. void confetti() {
  110. if(buttonListener()) { return; }
  111. if(millis() - previousMillis >= interval) {
  112. previousMillis = millis();
  113. fadeToBlackBy(leds, NUM_LEDS, 10);
  114. int pos = random16(NUM_LEDS);
  115. leds[pos] += CHSV( gHue + random8(64), 200, 255);
  116. }
  117. }
  118.  
  119. void juggle() {
  120. if(buttonListener()) { return; }
  121. if(millis() - previousMillis >= interval) {
  122. previousMillis = millis();
  123. fadeToBlackBy(leds, NUM_LEDS, 20);
  124. byte dothue = 0;
  125. for(int i = 0; i < 8; i++) {
  126. leds[beatsin16(i+7,0,NUM_LEDS)] |= CHSV(dothue, 200, 255);
  127. dothue += 32;
  128. }
  129. }
  130. }
  131.  
  132.  
  133. void vuscanner() {
  134. if(buttonListener()) { return; }
  135. uint8_t i;
  136. uint16_t minLvl, maxLvl;
  137. int n, height;
  138.  
  139. n = analogRead(MIC_PIN); // Raw reading from mic
  140. n = abs(n - 512 - DC_OFFSET); // Center on zero
  141.  
  142. n = (n <= NOISE) ? 0 : (n - NOISE); // Remove noise/hum
  143. lvl = ((lvl * 7) + n) >> 3; // "Dampened" reading (else looks twitchy)
  144.  
  145. // Calculate bar height based on dynamic min/max levels (fixed point):
  146. height = TOP * (lvl - minLvlAvg) / (long)(maxLvlAvg - minLvlAvg);
  147.  
  148. if (height < 0L) height = 0; // Clip output
  149. else if (height > TOP) height = TOP;
  150. if (height > peak) peak = height; // Keep 'peak' dot at top
  151.  
  152.  
  153. // Color pixels based on rainbow gradient
  154. for (i=0; i<NUM_LEDS; i++) {
  155. if (i >= height) leds[i].setRGB( 0, 0,0);
  156. //uncomment one of the following lines to set strip effect
  157. //else leds[i] = CHSV(map(i,0,NUM_LEDS-1,0,255), 255, 255); //CHSV values set for rainbow
  158. else leds[i] = CHSV(gHue, 255, 255); //CHSV values set to fade rgb
  159. }
  160.  
  161. // Draw peak dot
  162. if (peak > 0 && peak <= NUM_LEDS-1) leds[peak] = CHSV(map(peak,0,NUM_LEDS-1,0,255), 255, 255);
  163.  
  164. // Every few frames, make the peak pixel drop by 1:
  165.  
  166. if (++dotCount >= PEAK_FALL) { // fall rate
  167. if(peak > 0) peak--;
  168. dotCount = 0;
  169. }
  170.  
  171. vol[volCount] = n; // Save sample for dynamic leveling
  172. if (++volCount >= SAMPLES) volCount = 0; // Advance/rollover sample counter
  173.  
  174. // Get volume range of prior frames
  175. minLvl = maxLvl = vol[0];
  176. for (i=1; i<SAMPLES; i++) {
  177. if (vol[i] < minLvl) minLvl = vol[i];
  178. else if (vol[i] > maxLvl) maxLvl = vol[i];
  179. }
  180. // minLvl and maxLvl indicate the volume range over prior frames, used
  181. // for vertically scaling the output graph (so it looks interesting
  182. // regardless of volume level). If they're too close together though
  183. // (e.g. at very low volume levels) the graph becomes super coarse
  184. // and 'jumpy'...so keep some minimum distance between them (this
  185. // also lets the graph go to zero when no sound is playing):
  186. if((maxLvl - minLvl) < TOP) maxLvl = minLvl + TOP;
  187. minLvlAvg = (minLvlAvg * 63 + minLvl) >> 6; // Dampen min/max levels
  188. maxLvlAvg = (maxLvlAvg * 63 + maxLvl) >> 6; // (fake rolling average)
  189. }
  190.  
  191. void bpm() {
  192. if(buttonListener()) { return; }
  193. if(millis() - previousMillis >= interval) {
  194. previousMillis = millis();
  195. uint8_t BeatsPerMinute = 62;
  196. CRGBPalette16 palette = PartyColors_p;
  197. uint8_t beat = beatsin8(BeatsPerMinute, 64, 255);
  198. for(int i = 0; i < NUM_LEDS; i++) {
  199. leds[i] = ColorFromPalette(palette, gHue+(i*2), beat-gHue+(i*10));
  200. }
  201. }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement