Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. /*
  2. LED VU meter for Arduino and Adafruit NeoPixel LEDs.
  3.  
  4. Hardware requirements:
  5. - Most Arduino or Arduino-compatible boards (ATmega 328P or better).
  6. - Adafruit Electret Microphone Amplifier (ID: 1063)
  7. - Adafruit Flora RGB Smart Pixels (ID: 1260)
  8. OR
  9. - Adafruit NeoPixel Digital LED strip (ID: 1138)
  10. - Optional: battery for portable use (else power through USB or adapter)
  11. Software requirements:
  12. - Adafruit NeoPixel library
  13.  
  14. Connections:
  15. - 3.3V to mic amp +
  16. - GND to mic amp -
  17. - Analog pin to microphone output (configurable below)
  18. - Digital pin to LED data input (configurable below)
  19. See notes in setup() regarding 5V vs. 3.3V boards - there may be an
  20. extra connection to make and one line of code to enable or disable.
  21.  
  22. Written by Adafruit Industries. Distributed under the BSD license.
  23. This paragraph must be included in any redistribution.
  24. */
  25.  
  26. #include <Adafruit_NeoPixel.h>
  27.  
  28. #define N_PIXELS 120 // Number of pixels in strand
  29. #define MIC_PIN A0 // Microphone is attached to this analog pin
  30. #define LED_PIN 6 // NeoPixel LED strand is connected to this pin
  31. #define DC_OFFSET 0 // DC offset in mic signal - if unusure, leave 0
  32. #define NOISE 1 // Noise/hum/interference in mic signal
  33. #define SAMPLES 40 // Length of buffer for dynamic level adjustment
  34. #define TOP (N_PIXELS + 2) // Allow dot to go slightly off scale
  35. #define PEAK_FALL 60 // Rate of peak falling dot
  36.  
  37. byte
  38. peak = 0, // Used for falling dot
  39. dotCount = 0, // Frame counter for delaying dot-falling speed
  40. volCount = 0; // Frame counter for storing past volume data
  41. int
  42. vol[SAMPLES], // Collection of prior volume samples
  43. lvl = 20, // Current "dampened" audio level
  44. minLvlAvg = 0, // For dynamic adjustment of graph low & high
  45. maxLvlAvg = 512;
  46. Adafruit_NeoPixel
  47. strip = Adafruit_NeoPixel(N_PIXELS, LED_PIN, NEO_GRB + NEO_KHZ800);
  48.  
  49. void setup() {
  50.  
  51. // This is only needed on 5V Arduinos (Uno, Leonardo, etc.).
  52. // Connect 3.3V to mic AND TO AREF ON ARDUINO and enable this
  53. // line. Audio samples are 'cleaner' at 3.3V.
  54. // COMMENT OUT THIS LINE FOR 3.3V ARDUINOS (FLORA, ETC.):
  55. analogReference(EXTERNAL);
  56.  
  57. memset(vol, 0, sizeof(vol));
  58. strip.begin();
  59. }
  60.  
  61. void loop() {
  62. uint8_t i;
  63. uint16_t minLvl, maxLvl;
  64. int n, height;
  65.  
  66.  
  67.  
  68. n = analogRead(MIC_PIN); // Raw reading from mic
  69. n = abs(n - 512 - DC_OFFSET); // Center on zero
  70. n = (n <= NOISE) ? 0 : (n - NOISE); // Remove noise/hum
  71. lvl = ((lvl * 7) + n) >> 3; // "Dampened" reading (else looks twitchy)
  72.  
  73. // Calculate bar height based on dynamic min/max levels (fixed point):
  74. height = TOP * (lvl - minLvlAvg) / (long)(maxLvlAvg - minLvlAvg);
  75.  
  76. if(height < 0L) height = 0; // Clip output
  77. else if(height > TOP) height = TOP;
  78. if(height > peak) peak = height; // Keep 'peak' dot at top
  79.  
  80.  
  81. // Color pixels based on rainbow gradient
  82. for(i=0; i<N_PIXELS; i++) {
  83. if(i >= height) strip.setPixelColor(i, 0, 0, 0);
  84. else strip.setPixelColor(i, 255,255,255); //Farbe ändern Du fotze
  85.  
  86. }
  87.  
  88.  
  89. // Draw peak dot
  90. if(peak > 0 && peak <= N_PIXELS-1) strip.setPixelColor(i, 255, 255, 255);
  91.  
  92. strip.show(); // Update strip
  93.  
  94. // Every few frames, make the peak pixel drop by 1:
  95.  
  96. if(++dotCount >= PEAK_FALL) { //fall rate
  97.  
  98. if(peak > 0) peak--;
  99. dotCount = 0;
  100. }
  101.  
  102.  
  103.  
  104. vol[volCount] = n; // Save sample for dynamic leveling
  105. if(++volCount >= SAMPLES) volCount = 0; // Advance/rollover sample counter
  106.  
  107. // Get volume range of prior frames
  108. minLvl = maxLvl = vol[0];
  109. for(i=1; i<SAMPLES; i++) {
  110. if(vol[i] < minLvl) minLvl = vol[i];
  111. else if(vol[i] > maxLvl) maxLvl = vol[i];
  112. }
  113. // minLvl and maxLvl indicate the volume range over prior frames, used
  114. // for vertically scaling the output graph (so it looks interesting
  115. // regardless of volume level). If they're too close together though
  116. // (e.g. at very low volume levels) the graph becomes super coarse
  117. // and 'jumpy'...so keep some minimum distance between them (this
  118. // also lets the graph go to zero when no sound is playing):
  119. if((maxLvl - minLvl) < TOP) maxLvl = minLvl + TOP;
  120. minLvlAvg = (minLvlAvg * 63 + minLvl) >> 6; // Dampen min/max levels
  121. maxLvlAvg = (maxLvlAvg * 63 + maxLvl) >> 6; // (fake rolling average)
  122.  
  123. }
  124.  
  125. // Input a value 0 to 255 to get a color value.
  126. // The colors are a transition r - g - b - back to r.
  127. uint32_t Wheel(byte WheelPos) {
  128. if(WheelPos < 85) {
  129. return strip.Color(WheelPos * 3, 255 - WheelPos * 255, 0);
  130. } else if(WheelPos < 170) {
  131. WheelPos -= 85;
  132. return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  133. } else {
  134. WheelPos -= 170;
  135. return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  136. }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement