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;
}

Finicky Code

I’d altered the code, and worked with my professor to try to work out some bugs. Today, we figured out that the soundSensed program I copied from some old code took too long to spit out information to properly work with the altered led code to get the light strip to respond better with the sound sensed from the microphone. I’ve sent the code to my professor, and plan on working with her to get all this sorted out tomorrow.

Code is All About the Details

Went to TA hours with Nikki and we discovered that the reason the Serial monitor on Arduino wasn’t working was that we forgot to write Serial.begin(); into the setup. This means we never launched the serial monitor. I then went through the serial monitor, and printed out the sound sensed. Initially, the amount of sound sensed from the microphone varied slightly, and now it is constant, and doesn’t change no matter how loudly I scream at the microphone. I fear my microphone may have died. I’ve put in another hour and half, with little tangible progress…

Frustrations

I spent at least 2 hours today working with my code when I finally got my hands on a led strip I borrowed from a friend. Initially, I thought my code worked, but it doesn’t seem to have much response to any input from the microphone. When I tried to switch my code and microphone over to the Arduino Uno so I could run the serial monitor, I ran into serious problems. I wanted to use the serial monitor to analyze the input received from the microphone, but whenever I try to run it, nothing happens. I’m not sure if this is a problem with my code or my computer settings. My professor and I suspect it has do do with my computer settings, but I don’t know, and there’s not much I can do to progress from here until I figure it out, especially since I still don’t have a working led strip. I may have to use the small individual flora neopixels that I bought for my second project, and order more later. I think if I can’t make much progress with the code, I will at least move forward with building, since I finally got my hands on a pair of suspenders.

Coding Success

I’m going to relish my small coding victory from tonight. After a total of three hours spent in the lab tonight, I have a code that should work with both the led strip and a microphone to make the colors cycle faster when more sound is sensed. Unfortunately, I cannot test this code because I do not have a working rgb strip. I will post more about the code tomorrow, and hopefully put it up once I get some sleep and clean up the code a bit.