// Sound activated LEDs with the Trinket and NeoPixels #include #define MIC_PIN A0 // Microphone #define LED_PIN 6 // NeoPixel LED strand #define N_PIXELS 4 // number of pixels in LED strand #define N 100 // Number of samples to take each time readSamples is called #define fadeDelay 10 // delay time for each fade amount #define noiseLevel 2 // slope level of average mic noise without sound Adafruit_NeoPixel strip = Adafruit_NeoPixel(N_PIXELS, LED_PIN, NEO_GRB + NEO_KHZ800); int samples[N]; // storage for a sample collection set int periodFactor = 0; // keep track of number of ms for period calculation int t1 = -1; // times of slope > 100 detected. int T; // period between times scaled to milliseconds int slope; // the slope of two collected data sample points byte periodChanged = 0; // Arduino setup Method void setup() { strip.begin(); ledsOff(); delay(500); displayColor(Wheel(100)); strip.show(); delay(500); } // Arduino loop Method void loop() { readSamples(); } // Read and Process Sample Data from Mic void readSamples() { for(int i=0; i0) { slope = samples[i] - samples[i-1]; } else { slope = samples[i] - samples[N-1]; } // Check if Slope greater than noiseLevel - sound that is not at noise level detected if(abs(slope) > noiseLevel) { if(slope < 0) { calculatePeriod(i); if(periodChanged == 1) { displayColor(getColor(T)); } } } else { ledsOff(); } periodFactor += 1; delay(1); } } void calculatePeriod(int i) { if(t1 == -1) { // t1 has not been set t1 = i; } else { // t1 was set so calc period int period = periodFactor*(i - t1); periodChanged = T==period ? 0 : 1; T = period; //Serial.println(T); // reset t1 to new i value t1 = i; periodFactor = 0; } } uint32_t getColor(int period) { if(period == -1) return Wheel(0); else if(period > 400) return Wheel(5); else return Wheel(map(-1*period, -400, -1, 50, 255)); } void fadeOut() { for(int i=0; i<5; i++) { strip.setBrightness(110 - i*20); strip.show(); // Update strip delay(fadeDelay); periodFactor +=fadeDelay; } } void fadeIn() { strip.setBrightness(100); strip.show(); // Update strip // fade color in for(int i=0; i<5; i++) { //strip.setBrightness(20*i + 30); //strip.show(); // Update strip delay(fadeDelay); periodFactor+=fadeDelay; } } void ledsOff() { fadeOut(); for(int i=0; i