#include #ifdef __AVR__ #include // Required for 16 MHz Adafruit Trinket #endif #define DEBUG true #define LED_COUNT 9 // Number of LEDs in strip #define BUFFSIZE 72 // at least LED_COUNT! #define HUESTEP ((65536/LED_COUNT)+23) #define BASELINE_FACTOR 0.5 #define DECAY_FACTOR 0.2 #define MICPIN A7 // microphone #define LED_PIN 4 // Declare our NeoPixel strip object: Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_RGB + NEO_KHZ800); // Argument 1 = Number of pixels in NeoPixel strip // Argument 2 = Arduino pin number (most are valid) // Argument 3 = Pixel type flags, add together as needed: // NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs) // NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers) // NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products) // NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2) // NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products) #define AVGSIZE 4 int rawBuff[BUFFSIZE] = {0}; int avgBuff[BUFFSIZE] = {0}; int index = 0, hue = 0; 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 } void loop() { thisTime = millis(); rawBuff[index] = analogRead(MICPIN); int nextIndex = (index + 1) % BUFFSIZE; int minVal = 1024, maxVal = -1; for (int i = 0; i < BUFFSIZE; i++) { avgBuff[i] = 0; for (int j = 0; j < AVGSIZE ; j++) { avgBuff[i] += rawBuff[(i + BUFFSIZE - j) % BUFFSIZE]; } avgBuff[i] /= AVGSIZE; minVal = min(minVal, avgBuff[i]); maxVal = max(maxVal, avgBuff[i]); } int baseline; for (int i = 0; i < LED_COUNT; i++) { int value = 0; if (i) { value = (BASELINE_FACTOR * baseline) + ((1.0 - BASELINE_FACTOR) * constrain( map(avgBuff[(index + BUFFSIZE - i) % BUFFSIZE], minVal, maxVal , -255, 255), 0, 255) * pow(1.0 - DECAY_FACTOR, i)); } else { baseline = constrain( map(avgBuff[(index + BUFFSIZE - i) % BUFFSIZE], minVal, maxVal , -255, 255), 0, 255); value = baseline; } strip.setPixelColor(i, strip.ColorHSV(hue , 255, value)); hue = (hue + HUESTEP) % 65536; if (DEBUG) { Serial.print(minVal); Serial.print(' '); Serial.print(maxVal); Serial.print(' '); Serial.print(baseline); Serial.print(' '); Serial.print(value); Serial.print('\n'); } } strip.show(); index = nextIndex; lastTime = thisTime; delay(10); }