Guest User

Switching modes with pushbutton

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