Hi Guys,
I had to shelve this project for a while while I worked on some other pieces, but I am back at it.
I managed to fry one of the op-amp breakouts that I bought, and then decided to mess with the math a little rather than cook more boards.
I think I have a working sensor set now I just had to amplify the numbers after the shifting operation.
// sparkfun electret mic sketch
const int ledPin = 13;
const int middleValue = 512;
const int numberOfSamples = 128;
int micPower = 12;
int sample;
long signal;
long averageReading;
long runningAverage=0;
long output=0;
const int averagedOver= 16;
const int threshold= 350;
void setup() {
pinMode(ledPin, OUTPUT);
digitalWrite(micPower,HIGH);
Serial.begin(9600);
}
void loop(){
long sumOfSquares = 0;
for (int i=0; i<numberOfSamples; i++) {
sample = analogRead(4);
signal = (sample - middleValue);
signal *= signal;
sumOfSquares += signal;
}
averageReading = sqrt (sumOfSquares / numberOfSamples);
runningAverage = ((( averagedOver - 1 ) * runningAverage ) + averageReading ) >> 4 ;
output = ((runningAverage)*8);
if (output>threshold){
digitalWrite(ledPin,HIGH);
}else{
digitalWrite(ledPin, LOW);
}
Serial.println(output);
}
Likely very ineffecient, I am an artist, and program scavenger rather than an experienced coder so any advice is welcome.
I have now been trying to get this code integrated with a simple LED mood lamp to try and use the noise reading to affect the way the lights shift. The problem that I am running into is as follows:
While I get reasonably stable values back fromthe independant sensing program (output = between 245 and 310 in a quiet room jumping to over over 500 at moderate room volume), the integrated code provides more eratic readings. The new code is not yet designed to affect the lights, just run concurrently, so it has gaps in the stream of sensor readings while the chip does the counts for the change in lighting. The base line when the serial monitor is first opened is usually normal, (245-330) but the subsequent sensor cycles can have base readings as high as 800.
It appears that the numbers are highest when I have more power running to the LED's(15 Led's running off 3PWM pins), but I have no documented proof. When no LED's are lit the numbers seem to come in between 200-260.
Below is the code I have integrated, again, likely a mess sorry. Any Ideas what I am doing wrong?
// noise reactive sculpture
const int ledPin = 13;
const int middleValue = 512;
const int numberOfSamples = 128;
int count;
int micPower = 12;
int sample;
long signal;
long averageReading;
long runningAverage=0;
long output=0;
const int averagedOver= 16;
const int threshold= 550;
float RGB1[3];
float RGB2[3];
float INC[3];
int red, green, blue=0;
int RedPin = 11;
int GreenPin = 10;
int BluePin = 9;
void setup()
{
pinMode(ledPin, OUTPUT);
digitalWrite(micPower,HIGH);
Serial.begin(9600);
randomSeed(analogRead(0));
RGB1[0] = 0;
RGB1[1] = 0;
RGB1[2] = 0;
RGB2[0] = random(256);
RGB2[1] = random(256);
RGB2[2] = random(256);
}
void loop()
{
for(count=400; count>0; count--)// take 400 readings per loop to establish a common base line.
{
{
long sumOfSquares = 0;
for (int i=0; i<numberOfSamples; i++)
{
sample = analogRead(4);
signal = (sample - middleValue);
signal *= signal;
sumOfSquares += signal;
}
averageReading = sqrt (sumOfSquares / numberOfSamples);
runningAverage = ((( averagedOver - 1 ) * runningAverage ) + averageReading ) >> 4 ;//running average
output = ((runningAverage)*8);//boost the numbers so the difference is larger, and easier to use
if (output>threshold)//code remnant that will be changed later when the Mic values will affect the action of the lighting.
{
digitalWrite(ledPin,HIGH);
}
else{
digitalWrite(ledPin, LOW);
}
Serial.println(output);
count = (count-1);
}
}
randomSeed(analogRead(0));//nothing hooked to 0 the rest of the code deals with the shifting patterns in the lights.
for (int x=0; x<3; x++) {
INC[x] = (RGB1[x] - RGB2[x]) / 256; }
for (int x=0; x<256; x++) {
red = int(RGB1[0]);
green = int(RGB1[1]);
blue = int(RGB1[2]);
analogWrite (RedPin, red);
analogWrite (GreenPin, green);
analogWrite (BluePin, blue);
delay(10);
RGB1[0] -= INC[0];
RGB1[1] -= INC[1];
RGB1[2] -= INC[2];
}
for (int x=0; x<3; x++) {
RGB2[x] = random(556)-300;
RGB2[x] = constrain(RGB2[x], 0, 255);
delay(100);
}
}
Please pardon the Novel and thanks again.