Advertisement
Guest User

Programming of Sound Sensor

a guest
Oct 27th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.42 KB | None | 0 0
  1. #include <Adafruit_NeoPixel.h>
  2. #include <math.h>
  3.  
  4. #define N_PIXELS 24 // Number of pixels in strand
  5. #define MIC_PIN A0 // Microphone is attached to this analog pin
  6. #define LED_PIN 2 // NeoPixel LED strand is connected to this pin
  7. #define SAMPLE_WINDOW 10 // Sample window for average level
  8. #define PEAK_HANG 0 //Time of pause before peak dot falls
  9. #define PEAK_FALL 0 //Rate of falling peak dot
  10. #define INPUT_FLOOR 10 //Lower range of analogRead input
  11. #define INPUT_CEILING 300 //Max range of analogRead input, the lower the value the more sensitive (1023 = max)
  12.  
  13.  
  14.  
  15. byte peak = 16; // Peak level of column; used for falling dots
  16. unsigned int sample;
  17.  
  18. byte dotCount = 0; //Frame counter for peak dot
  19. byte dotHangCount = 0; //Frame counter for holding peak dot
  20.  
  21. Adafruit_NeoPixel strip = Adafruit_NeoPixel(N_PIXELS, LED_PIN, NEO_GRB + NEO_KHZ800);
  22.  
  23. void setup()
  24. {
  25. // This is only needed on 5V Arduinos (Uno, Leonardo, etc.).
  26. // Connect 3.3V to mic AND TO AREF ON ARDUINO and enable this
  27. // line. Audio samples are 'cleaner' at 3.3V.
  28. // COMMENT OUT THIS LINE FOR 3.3V ARDUINOS (FLORA, ETC.):
  29. // analogReference(EXTERNAL);
  30.  
  31. // Serial.begin(9600);
  32. strip.begin();
  33. strip.show(); // Initialize all pixels to 'off'
  34.  
  35. }
  36.  
  37. void loop()
  38. {
  39. unsigned long startMillis= millis(); // Start of sample window
  40. float peakToPeak = 0; // peak-to-peak level
  41.  
  42. unsigned int signalMax = 0;
  43. unsigned int signalMin = 1023;
  44. unsigned int c, y;
  45.  
  46.  
  47. // collect data for length of sample window (in mS)
  48. while (millis() - startMillis < SAMPLE_WINDOW)
  49. {
  50. sample = analogRead(MIC_PIN);
  51. if (sample < 1024) // toss out spurious readings
  52. {
  53. if (sample > signalMax)
  54. {
  55. signalMax = sample; // save just the max levels
  56. }
  57. else if (sample < signalMin)
  58. {
  59. signalMin = sample; // save just the min levels
  60. }
  61. }
  62. }
  63. peakToPeak = signalMax - signalMin; // max - min = peak-peak amplitude
  64.  
  65. // Serial.println(peakToPeak);
  66.  
  67.  
  68. //Fill the strip with rainbow gradient
  69. for (int i=0;i<=strip.numPixels()-1;i++){
  70. strip.setPixelColor(i,Wheel(map(i,0,strip.numPixels()-1,30,150)));
  71. }
  72.  
  73.  
  74. //Scale the input logarithmically instead of linearly
  75. c = fscale(INPUT_FLOOR, INPUT_CEILING, strip.numPixels(), 0, peakToPeak, 2);
  76.  
  77.  
  78.  
  79.  
  80. if(c < peak) {
  81. peak = c; // Keep dot on top
  82. dotHangCount = 0; // make the dot hang before falling
  83. }
  84. if (c <= strip.numPixels()) { // Fill partial column with off pixels
  85. drawLine(strip.numPixels(), strip.numPixels()-c, strip.Color(0, 0, 0));
  86. }
  87.  
  88. // Set the peak dot to match the rainbow gradient
  89. y = strip.numPixels() - peak;
  90.  
  91. strip.setPixelColor(y-1,Wheel(map(y,0,strip.numPixels()-1,30,150)));
  92.  
  93. strip.show();
  94.  
  95. // Frame based peak dot animation
  96. if(dotHangCount > PEAK_HANG) { //Peak pause length
  97. if(++dotCount >= PEAK_FALL) { //Fall rate
  98. peak++;
  99. dotCount = 0;
  100. }
  101. }
  102. else {
  103. dotHangCount++;
  104. }
  105. }
  106.  
  107. //Used to draw a line between two points of a given color
  108. void drawLine(uint8_t from, uint8_t to, uint32_t c) {
  109. uint8_t fromTemp;
  110. if (from > to) {
  111. fromTemp = from;
  112. from = to;
  113. to = fromTemp;
  114. }
  115. for(int i=from; i<=to; i++){
  116. strip.setPixelColor(i, c);
  117. }
  118. }
  119.  
  120.  
  121. float fscale( float originalMin, float originalMax, float newBegin, float
  122. newEnd, float inputValue, float curve){
  123.  
  124. float OriginalRange = 0;
  125. float NewRange = 0;
  126. float zeroRefCurVal = 0;
  127. float normalizedCurVal = 0;
  128. float rangedValue = 0;
  129. boolean invFlag = 0;
  130.  
  131.  
  132. // condition curve parameter
  133. // limit range
  134.  
  135. if (curve > 10) curve = 10;
  136. if (curve < -10) curve = -10;
  137.  
  138. curve = (curve * -.1) ; // - invert and scale - this seems more intuitive - postive numbers give more weight to high end on output
  139. curve = pow(10, curve); // convert linear scale into lograthimic exponent for other pow function
  140.  
  141. /*
  142. Serial.println(curve * 100, DEC); // multply by 100 to preserve resolution
  143. Serial.println();
  144. */
  145.  
  146. // Check for out of range inputValues
  147. if (inputValue < originalMin) {
  148. inputValue = originalMin;
  149. }
  150. if (inputValue > originalMax) {
  151. inputValue = originalMax;
  152. }
  153.  
  154. // Zero Refference the values
  155. OriginalRange = originalMax - originalMin;
  156.  
  157. if (newEnd > newBegin){
  158. NewRange = newEnd - newBegin;
  159. }
  160. else
  161. {
  162. NewRange = newBegin - newEnd;
  163. invFlag = 1;
  164. }
  165.  
  166. zeroRefCurVal = inputValue - originalMin;
  167. normalizedCurVal = zeroRefCurVal / OriginalRange; // normalize to 0 - 1 float
  168.  
  169. // Check for originalMin > originalMax - the math for all other cases i.e. negative numbers seems to work out fine
  170. if (originalMin > originalMax ) {
  171. return 0;
  172. }
  173.  
  174. if (invFlag == 0){
  175. rangedValue = (pow(normalizedCurVal, curve) * NewRange) + newBegin;
  176.  
  177. }
  178. else // invert the ranges
  179. {
  180. rangedValue = newBegin - (pow(normalizedCurVal, curve) * NewRange);
  181. }
  182.  
  183. return rangedValue;
  184. }
  185.  
  186.  
  187. // Input a value 0 to 255 to get a color value.
  188. // The colours are a transition r - g - b - back to r.
  189. uint32_t Wheel(byte WheelPos) {
  190. if(WheelPos < 85) {
  191. return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  192. }
  193. else if(WheelPos < 170) {
  194. WheelPos -= 85;
  195. return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  196. }
  197. else {
  198. WheelPos -= 170;
  199. return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  200. }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement