123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- #include <Adafruit_DotStar.h>
- #include <SPI.h> // COMMENT OUT THIS LINE FOR GEMMA OR TRINKET
-
- #define DEBUG true
-
- #define NUMPIXELS 72 // Number of LEDs in strip
- #define DATAPIN 4
- #define CLOCKPIN 5
- #define DECAYFACTOR 0.95
- Adafruit_DotStar strip = Adafruit_DotStar(
- NUMPIXELS, DATAPIN, CLOCKPIN, DOTSTAR_BRG);
-
- #define MICPIN A0 // microphone
- //Turns out QUIETPIN is not that important
- //#define QUIETPIN A1 // potentiometer to determine "quiet" level (no output below this)
- #define LOUD 768 // constant. Higher than that means compress to 0..255
- #define BUFFSIZE 96 // at least NUMPIXELS!
- #define AVGSIZE 5
- #define MILLI_AMNESIA 23 // How much to "falsely remeber" current value as history (1..999)
- int rawBuff[BUFFSIZE];
- int notHighBuff[BUFFSIZE];
- int lowBuff[BUFFSIZE];
- int index, minLow, maxLow, minMid, maxMid, minHigh, maxHigh, dynRange;
-
- #define rawLow(i) (lowBuff[i])
- #define rawMid(i) (notHighBuff[i]-lowBuff[i])
- #define rawHigh(i) (rawBuff[i]-notHighBuff[i])
- #define normLow(i) map(rawLow(i),minLow,maxLow,0,dynRange)
- #define normMid(i) map(rawMid(i),minMid,maxMid,0,dynRange)
- #define normHigh(i) map(rawHigh(i),minHigh,maxHigh,0,dynRange)
-
- unsigned long lastTime, thisTime;
-
- void setup() {
- if (DEBUG) {
- Serial.begin(230400);
- }
- lastTime = millis();
- index = 0;
- strip.begin(); // Initialize pins for output
- strip.show(); // Turn all LEDs off ASAP
- for (int i = 0; i < BUFFSIZE; i++) {
- rawBuff[i] = notHighBuff[i] = lowBuff[i] = 512;
- }
- }
-
-
- void loop() {
- thisTime = millis();
- unsigned long rate = 1000 / max(thisTime - lastTime, 1);
- rawBuff[index] = analogRead(MICPIN);
- notHighBuff[index] = 0;
- int nextIndex = (index + 1) % BUFFSIZE;
- for (int i = nextIndex + BUFFSIZE - AVGSIZE; i % BUFFSIZE != nextIndex; i++) {
- notHighBuff[index] += rawBuff[i % BUFFSIZE];
- }
- notHighBuff[index] /= AVGSIZE;
- lowBuff[index] = (notHighBuff[index] + notHighBuff[(index + BUFFSIZE - AVGSIZE) % BUFFSIZE]) / 2;
- minLow = minMid = minHigh = 1024;
- maxLow = maxMid = maxHigh = -1;
- long pseudoRaw = rawBuff[index] * MILLI_AMNESIA;
- long pseudoNotHigh = notHighBuff[index] * MILLI_AMNESIA;
- long pseudoLow = lowBuff[index] * MILLI_AMNESIA;
- for (int i = 0; i < BUFFSIZE; i++) {
- rawBuff[i] = (long(rawBuff[i] * long(1000 - MILLI_AMNESIA) + pseudoRaw)) / 1000;
- notHighBuff[i] = (long(notHighBuff[i] * long(1000 - MILLI_AMNESIA) + pseudoNotHigh)) / 1000;
- lowBuff[i] = (long(lowBuff[i] * long(1000 - MILLI_AMNESIA) + pseudoLow)) / 1000;
- minLow = min(minLow, rawLow(i));
- minMid = min(minMid, rawMid(i));
- minHigh = min(minHigh, rawHigh(i));
- maxLow = max(maxLow, rawLow(i));
- maxMid = max(maxMid, rawMid(i));
- maxHigh = max(maxHigh, rawHigh(i));
- }
- int quiet = LOUD / 5; // was: map(analogRead(QUIETPIN), 0, 1024, 0, LOUD * 7 / 8);
- dynRange = constrain(
- map(max(maxLow, max(maxMid, maxHigh)) - min(minLow, min(minMid, minHigh)),
- quiet, LOUD, 0, 255),
- 0, 255);
-
- int buffIndex = index;
- float ledFactor = 1.0;
- for (int i = 0; i < NUMPIXELS; i++) {
- uint32_t rgb = uint32_t(
- ((uint32_t(float(normHigh(buffIndex)) * ledFactor) << 8) +
- uint32_t(float(normMid(buffIndex)) * ledFactor) << 8) +
- uint32_t(float(normLow(buffIndex)) * ledFactor));
- strip.setPixelColor(NUMPIXELS - i, rgb);
- buffIndex = (buffIndex + BUFFSIZE - 1) % BUFFSIZE;
- ledFactor *= DECAYFACTOR;
- }
- strip.show();
- if (DEBUG) {
- Serial.print(dynRange);
- Serial.print(' ');
- Serial.print(normLow(index));
- Serial.print(' ');
- Serial.print(normMid(index));
- Serial.print(' ');
- Serial.print(normHigh(index));
- Serial.print(' ');
- Serial.print('\n');
- }
- index = nextIndex;
- lastTime = thisTime;
- delay(1); // this is considered good manners ;)
- }
|