Guest User

Ursula's seashell by Adafruit

a guest
Nov 7th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.28 KB | None | 0 0
  1. /* soundbracelet for FastLED 2.1 or greater
  2.  
  3. Converted by: Andrew Tuline
  4.  
  5. Date: Oct, 2014
  6.  
  7. this code is based Neopixel code by John Burroughs:
  8.  
  9. https://www.youtube.com/watch?v=JjX8X5D8RW0&feature=youtu.be
  10. https://plus.google.com/105445034001275025240/posts/jK2fxRx79kj
  11. http://www.slickstreamer.info/2014/07/led-bracelet-vu-meter-3dprinting.html
  12.  
  13. That was based on the Adafruit LED Ampli-tie project at:
  14.  
  15. https://learn.adafruit.com/led-ampli-tie/overview
  16.  
  17. This was written for a Sparkfun INMP401 MEMS microphone/pre-amp. In this case, it's plugged into A5 of the Arduino.
  18.  
  19. You may need to only use battery power as noise from the USB can affect the signals.
  20.  
  21. Plug Vcc of the microphone into 3.3V of Arduino. Connect 3.3V of Arduino to aref pin, and gnd to gnd.
  22.  
  23. FastLED is available at https://github.com/FastLED/FastLED
  24.  
  25. */
  26.  
  27.  
  28. #include "FastLED.h" // FastLED library. Preferably the latest copy of FastLED 2.1.
  29.  
  30. // Fixed definitions cannot change on the fly.
  31. #define LED_DT 0 // Serial data pin for WS2812B or WS2801
  32. #define COLOR_ORDER GRB // Are they RGB, GRB or what??
  33. #define LED_TYPE WS2812B // What kind of strip are you using?
  34. #define NUM_LEDS 23 // Number of LED's
  35.  
  36. // Initialize changeable global variables.
  37. uint8_t max_bright = 255; // Overall brightness definition. It can be changed on the fly.
  38.  
  39. struct CRGB leds[NUM_LEDS]; // Initialize our LED array.
  40.  
  41.  
  42. #define MIC_PIN A1 // Analog port for microphone
  43.  
  44. #define DC_OFFSET 0 // DC offset in mic signal - if unusure, leave 0
  45. // I calculated this value by serialprintln lots of mic values
  46. #define NOISE 100 // Noise/hum/interference in mic signal and increased value until it went quiet
  47. #define SAMPLES 60 // Length of buffer for dynamic level adjustment
  48. #define TOP (NUM_LEDS + 2) // Allow dot to go slightly off scale
  49. #define PEAK_FALL 10 // Rate of peak falling dot
  50.  
  51. byte
  52. peak = 0, // Used for falling dot
  53. dotCount = 0, // Frame counter for delaying dot-falling speed
  54. volCount = 0; // Frame counter for storing past volume data
  55. int
  56. vol[SAMPLES], // Collection of prior volume samples
  57. lvl = 10, // Current "dampened" audio level
  58. minLvlAvg = 0, // For dynamic adjustment of graph low & high
  59. maxLvlAvg = 512;
  60.  
  61.  
  62. void setup() {
  63.  
  64. // This is only needed on 5V Arduinos (Uno, Leonardo, etc.).
  65. // Connect 3.3V to mic AND TO AREF ON ARDUINO and enable this
  66. // line. Audio samples are 'cleaner' at 3.3V.
  67. // COMMENT OUT THIS LINE FOR 3.3V ARDUINOS (FLORA, ETC.):
  68. //analogReference(EXTERNAL);
  69.  
  70. // delay(1000); // Power-up safety delay or something like that.
  71. // Serial.begin(57600);
  72. LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS);
  73. FastLED.setBrightness(max_bright);
  74. set_max_power_in_volts_and_milliamps(5, 500); // FastLED 2.1 Power management set at 5V, 500mA
  75. } // setup()
  76.  
  77.  
  78.  
  79. void loop() {
  80. seashell();
  81. show_at_max_brightness_for_power(); // Power managed FastLED display
  82. // Serial.println(LEDS.getFPS());
  83. } // loop()
  84.  
  85.  
  86.  
  87. void seashell() {
  88.  
  89. uint8_t i;
  90. uint16_t minLvl, maxLvl;
  91. int n, height;
  92.  
  93. n = analogRead(MIC_PIN); // Raw reading from mic
  94. n = abs(n - 512 - DC_OFFSET); // Center on zero
  95.  
  96. n = (n <= NOISE) ? 0 : (n - NOISE); // Remove noise/hum
  97. lvl = ((lvl * 7) + n) >> 3; // "Dampened" reading (else looks twitchy)
  98.  
  99. // Calculate bar height based on dynamic min/max levels (fixed point):
  100. height = TOP * (lvl - minLvlAvg) / (long)(maxLvlAvg - minLvlAvg);
  101.  
  102. if (height < 0L) height = 0; // Clip output
  103. else if (height > TOP) height = TOP;
  104. if (height > peak) peak = height; // Keep 'peak' dot at top
  105.  
  106.  
  107. // Color pixels based on rainbow gradient
  108. for (i=0; i<NUM_LEDS; i++) {
  109. if (i >= height) leds[i].setRGB( 0, 0,0);
  110. else leds[i] = CHSV(map(i,0,NUM_LEDS-1,50,70), 255, 255); //constrained to yellow color, change CHSV values for rainbow
  111. }
  112.  
  113. // Draw peak dot
  114. if (peak > 0 && peak <= NUM_LEDS-1) leds[peak] = CHSV(map(peak,0,NUM_LEDS-1,50,70), 255, 255);
  115.  
  116. // Every few frames, make the peak pixel drop by 1:
  117.  
  118. if (++dotCount >= PEAK_FALL) { // fall rate
  119. if(peak > 0) peak--;
  120. dotCount = 0;
  121. }
  122.  
  123. vol[volCount] = n; // Save sample for dynamic leveling
  124. if (++volCount >= SAMPLES) volCount = 0; // Advance/rollover sample counter
  125.  
  126. // Get volume range of prior frames
  127. minLvl = maxLvl = vol[0];
  128. for (i=1; i<SAMPLES; i++) {
  129. if (vol[i] < minLvl) minLvl = vol[i];
  130. else if (vol[i] > maxLvl) maxLvl = vol[i];
  131. }
  132. // minLvl and maxLvl indicate the volume range over prior frames, used
  133. // for vertically scaling the output graph (so it looks interesting
  134. // regardless of volume level). If they're too close together though
  135. // (e.g. at very low volume levels) the graph becomes super coarse
  136. // and 'jumpy'...so keep some minimum distance between them (this
  137. // also lets the graph go to zero when no sound is playing):
  138. if((maxLvl - minLvl) < TOP) maxLvl = minLvl + TOP;
  139. minLvlAvg = (minLvlAvg * 63 + minLvl) >> 6; // Dampen min/max levels
  140. maxLvlAvg = (maxLvlAvg * 63 + maxLvl) >> 6; // (fake rolling average)
  141.  
  142. } // fastbracelet()
Advertisement
Add Comment
Please, Sign In to add comment