Finally Got My Code to Work!!!

I finally got my rainbow cycle code to work properly with the microphone input. It’s really sensitive now and reacts really well.

If your curious, here is my code:

//This is a code that combines Neopixel code with input from a mic

#include

#define PIN 11

// hook up the out of the mic to analog input A2
//#define MIC_IN 2
#define PHOTORESISTOR A0
#define MIC_IN A2

int sampleWindow = 50;

//#include
//
//HIDSerial serial;

// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 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)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);

double brightness = .1;
int defaultSpeed = 15;
int lightingSpeed = defaultSpeed;
int time = 0; // start time at 0
uint16_t i = 0;
uint16_t j = 0;

// for mic
// this will be the highest peak, so start it very small
int signalMax;

// this will be the lowest peak, so start it very high
int signalMin;

// will hold the current value from the microphone
int sample;

// keep track of how long we’re sampling
int sampleTime;

void setup()
{
Serial.begin(9600);
strip.begin();
strip.show(); // Initialize all pixels to ‘off’

startSampling();
}

void loop() {

// if we just finished a sampling sound window
if ( sampleTime >= sampleWindow )
{
double soundSensed = getSoundSample();
lightingSpeed = round(60/((1+soundSensed)));

Serial.println( lightingSpeed );

// and restart sampling
startSampling();
}
// otherwise, keep getting samples
else
{
keepSampling();
}

// if enough time has passed
if ( time >= lightingSpeed )
{
// light the next dot
rainbowCycle();

// reset the timer
time = 0;
}

//wait 1 ms between loops
delay( 1 );

// increase time
time++;
sampleTime++;
}

// this is the function that makes the led strip cycle through the colors
void rainbowCycle()
{
// if end of j loop, reset
if ( j >= 256*5 )
j = 0;

for(i=0; i< strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
}
strip.show();

// update i and j
j++;
}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r – g – b – back to r.
uint32_t Wheel(byte WheelPos)
{
if(WheelPos < 85) {
return strip.Color(brightness * WheelPos * 3, brightness * (255 – WheelPos * 3), 0);
} else if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(brightness * (255 – WheelPos * 3), 0, brightness * WheelPos * 3);
} else {
WheelPos -= 170;
return strip.Color(0, brightness * WheelPos * 3, brightness * (255 – WheelPos * 3));
}
}

void startSampling()
{
// this will be the highest peak, so start it very small
signalMax = 0;

// this will be the lowest peak, so start it very high
signalMin = 1024;

// start sample timer
sampleTime = 0;
}

void keepSampling()
{
// read a value from mic and record it into sample variable
sample = analogRead( MIC_IN );
// Serial.print(sample);

// toss out spurious readings
if (sample signalMax)
{
// this is the new max — save it
signalMax = sample;
}
// otherwise, if the current sample is smaller than the min
else if (sample < signalMin)
{
// this is the new min — save it
signalMin = sample;
}
}

}

double getSoundSample()
{
// now that we’ve collected our data,
// determine the peak-peak amplitude as max – min
int peakDifference = signalMax – signalMin;

// give it back to the caller of this method
return peakDifference;
}