|  | @@ -0,0 +1,107 @@
 | 
		
	
		
			
			|  | 1 | +#include <Adafruit_DotStar.h>
 | 
		
	
		
			
			|  | 2 | +#include <SPI.h>         // COMMENT OUT THIS LINE FOR GEMMA OR TRINKET
 | 
		
	
		
			
			|  | 3 | +
 | 
		
	
		
			
			|  | 4 | +#define DEBUG true
 | 
		
	
		
			
			|  | 5 | +
 | 
		
	
		
			
			|  | 6 | +#define NUMPIXELS 72 // Number of LEDs in strip
 | 
		
	
		
			
			|  | 7 | +#define DATAPIN    4
 | 
		
	
		
			
			|  | 8 | +#define CLOCKPIN   5
 | 
		
	
		
			
			|  | 9 | +#define DECAYFACTOR 0.95
 | 
		
	
		
			
			|  | 10 | +Adafruit_DotStar strip = Adafruit_DotStar(
 | 
		
	
		
			
			|  | 11 | +                           NUMPIXELS, DATAPIN, CLOCKPIN, DOTSTAR_BRG);
 | 
		
	
		
			
			|  | 12 | +
 | 
		
	
		
			
			|  | 13 | +#define MICPIN   A0 // microphone
 | 
		
	
		
			
			|  | 14 | +//Turns out QUIETPIN is not that important
 | 
		
	
		
			
			|  | 15 | +//#define QUIETPIN A1 // potentiometer to determine "quiet" level (no output below this)
 | 
		
	
		
			
			|  | 16 | +#define LOUD 768    // constant. Higher than that means compress to 0..255
 | 
		
	
		
			
			|  | 17 | +#define BUFFSIZE 96 // at least NUMPIXELS!
 | 
		
	
		
			
			|  | 18 | +#define AVGSIZE 5
 | 
		
	
		
			
			|  | 19 | +#define MILLI_AMNESIA 23 // How much to "falsely remeber" current value as history (1..999)
 | 
		
	
		
			
			|  | 20 | +int rawBuff[BUFFSIZE];
 | 
		
	
		
			
			|  | 21 | +int notHighBuff[BUFFSIZE];
 | 
		
	
		
			
			|  | 22 | +int lowBuff[BUFFSIZE];
 | 
		
	
		
			
			|  | 23 | +int index, minLow, maxLow, minMid, maxMid, minHigh, maxHigh, dynRange;
 | 
		
	
		
			
			|  | 24 | +
 | 
		
	
		
			
			|  | 25 | +#define rawLow(i) (lowBuff[i])
 | 
		
	
		
			
			|  | 26 | +#define rawMid(i) (notHighBuff[i]-lowBuff[i])
 | 
		
	
		
			
			|  | 27 | +#define rawHigh(i) (rawBuff[i]-notHighBuff[i])
 | 
		
	
		
			
			|  | 28 | +#define normLow(i) map(rawLow(i),minLow,maxLow,0,dynRange)
 | 
		
	
		
			
			|  | 29 | +#define normMid(i) map(rawMid(i),minMid,maxMid,0,dynRange)
 | 
		
	
		
			
			|  | 30 | +#define normHigh(i) map(rawHigh(i),minHigh,maxHigh,0,dynRange)
 | 
		
	
		
			
			|  | 31 | +
 | 
		
	
		
			
			|  | 32 | +unsigned long lastTime, thisTime;
 | 
		
	
		
			
			|  | 33 | +
 | 
		
	
		
			
			|  | 34 | +void setup() {
 | 
		
	
		
			
			|  | 35 | +  if (DEBUG) {
 | 
		
	
		
			
			|  | 36 | +    Serial.begin(230400);
 | 
		
	
		
			
			|  | 37 | +  }
 | 
		
	
		
			
			|  | 38 | +  lastTime = millis();
 | 
		
	
		
			
			|  | 39 | +  index = 0;
 | 
		
	
		
			
			|  | 40 | +  strip.begin(); // Initialize pins for output
 | 
		
	
		
			
			|  | 41 | +  strip.show();  // Turn all LEDs off ASAP
 | 
		
	
		
			
			|  | 42 | +  for (int i = 0; i < BUFFSIZE; i++) {
 | 
		
	
		
			
			|  | 43 | +    rawBuff[i] = notHighBuff[i] = lowBuff[i] = 512;
 | 
		
	
		
			
			|  | 44 | +  }
 | 
		
	
		
			
			|  | 45 | +}
 | 
		
	
		
			
			|  | 46 | +
 | 
		
	
		
			
			|  | 47 | +
 | 
		
	
		
			
			|  | 48 | +void loop() {
 | 
		
	
		
			
			|  | 49 | +  thisTime = millis();
 | 
		
	
		
			
			|  | 50 | +  unsigned long rate = 1000 / max(thisTime - lastTime, 1);
 | 
		
	
		
			
			|  | 51 | +  rawBuff[index] = analogRead(MICPIN);
 | 
		
	
		
			
			|  | 52 | +  notHighBuff[index] = 0;
 | 
		
	
		
			
			|  | 53 | +  int nextIndex = (index + 1) % BUFFSIZE;
 | 
		
	
		
			
			|  | 54 | +  for (int i = nextIndex + BUFFSIZE - AVGSIZE; i % BUFFSIZE != nextIndex; i++) {
 | 
		
	
		
			
			|  | 55 | +    notHighBuff[index] += rawBuff[i % BUFFSIZE];
 | 
		
	
		
			
			|  | 56 | +  }
 | 
		
	
		
			
			|  | 57 | +  notHighBuff[index] /= AVGSIZE;
 | 
		
	
		
			
			|  | 58 | +  lowBuff[index] = (notHighBuff[index] + notHighBuff[(index + BUFFSIZE - AVGSIZE) % BUFFSIZE]) / 2;
 | 
		
	
		
			
			|  | 59 | +  minLow = minMid = minHigh = 1024;
 | 
		
	
		
			
			|  | 60 | +  maxLow = maxMid = maxHigh = -1;
 | 
		
	
		
			
			|  | 61 | +  long pseudoRaw = rawBuff[index] * MILLI_AMNESIA;
 | 
		
	
		
			
			|  | 62 | +  long pseudoNotHigh = notHighBuff[index] * MILLI_AMNESIA;
 | 
		
	
		
			
			|  | 63 | +  long pseudoLow = lowBuff[index] * MILLI_AMNESIA;
 | 
		
	
		
			
			|  | 64 | +  for (int i = 0; i < BUFFSIZE; i++) {
 | 
		
	
		
			
			|  | 65 | +    rawBuff[i] = (long(rawBuff[i] * long(1000 - MILLI_AMNESIA) + pseudoRaw)) / 1000;
 | 
		
	
		
			
			|  | 66 | +    notHighBuff[i] = (long(notHighBuff[i] * long(1000 - MILLI_AMNESIA) + pseudoNotHigh)) / 1000;
 | 
		
	
		
			
			|  | 67 | +    lowBuff[i] = (long(lowBuff[i] * long(1000 - MILLI_AMNESIA) + pseudoLow)) / 1000;
 | 
		
	
		
			
			|  | 68 | +    minLow =   min(minLow, rawLow(i));
 | 
		
	
		
			
			|  | 69 | +    minMid =   min(minMid, rawMid(i));
 | 
		
	
		
			
			|  | 70 | +    minHigh =  min(minHigh, rawHigh(i));
 | 
		
	
		
			
			|  | 71 | +    maxLow =   max(maxLow, rawLow(i));
 | 
		
	
		
			
			|  | 72 | +    maxMid =   max(maxMid, rawMid(i));
 | 
		
	
		
			
			|  | 73 | +    maxHigh =  max(maxHigh, rawHigh(i));
 | 
		
	
		
			
			|  | 74 | +  }
 | 
		
	
		
			
			|  | 75 | +  int quiet = LOUD / 5; // was: map(analogRead(QUIETPIN), 0, 1024, 0, LOUD * 7 / 8);
 | 
		
	
		
			
			|  | 76 | +  dynRange = constrain(
 | 
		
	
		
			
			|  | 77 | +               map(max(maxLow, max(maxMid, maxHigh)) - min(minLow, min(minMid, minHigh)),
 | 
		
	
		
			
			|  | 78 | +                   quiet, LOUD, 0, 255),
 | 
		
	
		
			
			|  | 79 | +               0, 255);
 | 
		
	
		
			
			|  | 80 | +
 | 
		
	
		
			
			|  | 81 | +  int buffIndex = index;
 | 
		
	
		
			
			|  | 82 | +  float ledFactor = 1.0;
 | 
		
	
		
			
			|  | 83 | +  for (int i = 0;  i < NUMPIXELS; i++) {
 | 
		
	
		
			
			|  | 84 | +    uint32_t rgb = uint32_t(
 | 
		
	
		
			
			|  | 85 | +                     ((uint32_t(float(normHigh(buffIndex)) * ledFactor) << 8) +
 | 
		
	
		
			
			|  | 86 | +                      uint32_t(float(normMid(buffIndex)) * ledFactor) << 8) +
 | 
		
	
		
			
			|  | 87 | +                     uint32_t(float(normLow(buffIndex)) * ledFactor));
 | 
		
	
		
			
			|  | 88 | +    strip.setPixelColor(NUMPIXELS - i, rgb);
 | 
		
	
		
			
			|  | 89 | +    buffIndex = (buffIndex + BUFFSIZE - 1) % BUFFSIZE;
 | 
		
	
		
			
			|  | 90 | +    ledFactor *= DECAYFACTOR;
 | 
		
	
		
			
			|  | 91 | +  }
 | 
		
	
		
			
			|  | 92 | +  strip.show();
 | 
		
	
		
			
			|  | 93 | +  if (DEBUG) {
 | 
		
	
		
			
			|  | 94 | +    Serial.print(dynRange);
 | 
		
	
		
			
			|  | 95 | +    Serial.print(' ');
 | 
		
	
		
			
			|  | 96 | +    Serial.print(normLow(index));
 | 
		
	
		
			
			|  | 97 | +    Serial.print(' ');
 | 
		
	
		
			
			|  | 98 | +    Serial.print(normMid(index));
 | 
		
	
		
			
			|  | 99 | +    Serial.print(' ');
 | 
		
	
		
			
			|  | 100 | +    Serial.print(normHigh(index));
 | 
		
	
		
			
			|  | 101 | +    Serial.print(' ');
 | 
		
	
		
			
			|  | 102 | +    Serial.print('\n');
 | 
		
	
		
			
			|  | 103 | +  }
 | 
		
	
		
			
			|  | 104 | +  index = nextIndex;
 | 
		
	
		
			
			|  | 105 | +  lastTime = thisTime;
 | 
		
	
		
			
			|  | 106 | +  delay(1); // this is considered good manners ;)
 | 
		
	
		
			
			|  | 107 | +}
 |