average noise level

I have a sound sensor on pin AO. I need to make a small program that samples the average noise level at regular intervolves. This average then becomes the base level until next time we sample it. Then I want to turn on the LED when the volume goes above the average level by a give amount.

What is the question ?

raschemmel:
What is the question ?

I need a small program that will take samples to get the average noise level at regular intervolves. This average then becomes the base level until next time we sample it. Then I want to turn on the LED when the volume goes above the average level by a give amount.

There have been Arduino projects in the past that did sound spectrum analysis. You should be able to find them in the playground, or ask Google. I suspect what you're trying to do here would be simpler and the existing projects would do most of the work for you.

Do you have a microphone or a sensor ?

Slee_The_Sloth:

raschemmel:
What is the question ?

I need a small program that will take samples to get the average noise level at regular intervolves. This average then becomes the base level until next time we sample it. Then I want to turn on the LED when the volume goes above the average level by a give amount.

Simple enough project, once you start learning how to create your own sketches. Or are you asking someone to write a sketch for you to your specifications?

Ok here is my sketch. Its ruff. It dose work, but it dose not seem to learn like I had hoped. I had hoped that if the average background sound went up then the original threshold would to. When watch the Serial output you can see the avarage climb a bit. But it never relay makes the adjustment I wanted.

int led = 13;
// int threshold = 300; //Change This or 475
int threshold;
int volume_Right;
int volume_Left;
int avrage1;
int avrage2;
int avrage3;
int avrage4;
int avrage5;
int FinalwAvarge = 0;
int x = 5;
int melissa = 0;
void setup() {                
  Serial.begin(9600); // For debugging
  pinMode(led, OUTPUT);     
}
 
void loop() {
  volume_Left = analogRead(A1);
  volume_Right = analogRead(A0); // Reads the value from the Analog PIN A0
// Serial.println("  ");
/// Serial.print(" Right volume: ");
// Serial.println(volume_Right);
// Serial.println("  ");

 if( x > 0)
   {
    FinalwAvarge = 0;  
   volume_Right = analogRead(A0);
   int avrage1 = volume_Right;
   //Serial.print(" Curent volume: ");
   //Serial.println(volume_Right);
   delay(10);
   volume_Right = analogRead(A0);
   int avrage2 = volume_Right;
   //Serial.print(" Curent volume: ");
   //Serial.println(volume_Right);
   delay(10);
   int avrage3 = volume_Right;
   // Serial.print(" Curent volume: ");
   // Serial.println(volume_Right);
   delay(10);
   volume_Right = analogRead(A0);
   int avrage4 = volume_Right;
   //Serial.print(" Curent volume: ");
   //Serial.println(volume_Right);
   delay(10);
   volume_Right = analogRead(A0);
   int avrage5 = volume_Right;
   volume_Right = analogRead(A0);
   // Serial.print(" Curent volume: ");
   // Serial.println(volume_Right);
   delay(10);
   FinalwAvarge = FinalwAvarge + avrage1 + avrage2 + avrage3 + avrage4 + avrage5 ;
   delay(10);
   FinalwAvarge = (FinalwAvarge / 5);
   delay(10);
 
 
    threshold = (FinalwAvarge + 25) ;
 
    Serial.print(" ***** FinalwAvarge: ");
    Serial.println(FinalwAvarge);
 
    Serial.print(" ******** threshold: ");
    Serial.println(threshold);
 Serial.println(x);
    Serial.println("-------------------------------");
  //  delay(500);
    x = x - 1;
    melissa =  25; /// the time counted betwean samples 250
   }
 
 
 if(volume_Right >= threshold)
    {
     digitalWrite(led, HIGH); //Turn ON Led
     Serial.println(" ****************************** ");
     Serial.print(" XXXXXXX Trip volume: ");
     Serial.println(volume_Right);
     Serial.println(" ****************************** ");
    }     
 
 if(volume_Right <= threshold)
    {
     digitalWrite(led, LOW); //Turn ON Led 
    } 
 
 
 volume_Right = 0;
 

   
 if (melissa == 0 )
   {
    x = 1; 
   }  
 if ( melissa >= 1 )
   {
     melissa =  melissa - 1 ; 
     delay(10);
//     Serial.print("......................... Melissa: ");
//     Serial.println(melissa);
   }  
}

try this as a starter

// Released to the public domain
int led = 13;

int volumeRight;
int volumeLeft;

int averageLeft;
int averageRight;

unsigned long lastAverage = 0;
unsigned long lastCompare = 0;

void setup() 
{                
  Serial.begin(115200);
  Serial.println("start...");
  pinMode(led, OUTPUT);  
  digitalWrite(led, LOW);
}

void loop() 
{
  // get average once per minute
  if (millis() - lastAverage > 60000UL)  // UL = Unsigned Long
  {
    lastAverage = millis();

    averageRight = 0;
    for (int i=0; i<8 ; i++)
    {
      averageRight += analogRead(A0);
    }
    averageRight /= 8;

    averageLeft = 0;
    for (int i=0; i<8 ; i++)
    {
      averageLeft += analogRead(A1);
    }
    averageLeft /= 8;
  }

  // compare each seco
  if (millis() - lastCompare > 1000) 
  {
    lastCompare = millis();
    volumeRight = analogRead(A0);
    volumeLeft = analogRead(A1);

    if (volumeRight > averageRight + 25) digitalWrite(led, HIGH);
    else digitalWrite(led, LOW);
    if (volumeLeft > averageLeft) digitalWrite(led, HIGH);
    else digitalWrite(led, LOW);

    // a simple graph on serial
    int avg = (volumeRight + volumeLeft)/2;
      for (int i=0; i< avg/16; i++) Serial.print('+');
    Serial.println();
  }
}

I haven't seen any discussion whatsoever about the hardware being used. If this is an audio application , doesn't it seem reasonable to assume that the microphone the OP is using is crap and not a good mic ? In addition, is it not also reasonable to assume the OP does not know that an AC signal needs to be coupled with a capacitor of the correct value ? Is it not also reasonable to assume the OP doesn't have a clue about audio amplifier pre-amp design ? Everyone is talking about software while the hardware could be F'd up nine ways from sunday.

raschemmel:
I haven't seen any discussion whatsoever about the hardware being used. If this is an audio application , doesn't it seem reasonable to assume that the microphone the OP is using is crap and not a good mic ? In addition, is it not also reasonable to assume the OP does not know that an AC signal needs to be coupled with a capacitor of the correct value ? Is it not also reasonable to assume the OP doesn't have a clue about audio amplifier pre-amp design ? Everyone is talking about software while the hardware could be F'd up nine ways from sunday.

No, (s)he didn't tell (yet) - assuming everything is crap is just an assumption and as good/bad as any other assumption.

You are definitely right that the are many ways to fail [been there] :wink:

@OP,
How about a photo of a schematic drawn by hand of your circuit, along with a photo of your circuit so we can investigate the possibility that your problem is not software related.

Sorry guys. The mic is actually pretty good. or it seems to be. My basic program works nice, When the volume reaches a set point the sketch triggers and MP3 Trigger from spark fun. This when my R2 hears a sound above 476 he responds to it. If he heares a noise above 665 he screams as if having been scared. The problem is I need to make adjustments for when he is in a room that has a lot of background noise. So I wanted the sketch to sample the ambient noise every know and then and then reset the thresh hold above that. Here is the mike I'm using:

Why don't you just do noise cancelling ? You sample the ambient noise , run it into and inverting op amp circuit then put it into an op amp summing amplifier as input 1 and put the signal the R2 is "hearing" into input 2 (obviously you need two mics for this).
The invered audio signal of the ambient , being as it is a mirror image of the original, when summed with the original will cancel out the ambient noise. The caveat is that the primary mic and the cancelling mic cannot be facing the same side of R2. The primary mic must face front, the ambient mic must face to the rear or sides. You can have mics on all four sides and use the three that don't face front for ambients , summing THOSE together and that SUM(NOISE) would be input -1 of the summer that sums the primary mic and ambient signals (two inputs). This is all old stuff but is works. It may be more trouble than you care to go to but
it would solve your problem with ambient noise. Surely one of the software guys is going to come back and say , but if you have the extra mics. the Summing Amp functions can be done in software with out all those silly op amps....

robtillaart:
try this as a starter

// Released to the public domain

int led = 13;

int volumeRight;
int volumeLeft;

int averageLeft;
int averageRight;

unsigned long lastAverage = 0;
unsigned long lastCompare = 0;

void setup()
{               
  Serial.begin(115200);
  Serial.println(“start…”);
  pinMode(led, OUTPUT); 
  digitalWrite(led, LOW);
}

void loop()
{
  // get average once per minute
  if (millis() - lastAverage > 60000UL)  // UL = Unsigned Long
  {
    lastAverage = millis();

averageRight = 0;
    for (int i=0; i<8 ; i++)
    {
      averageRight += analogRead(A0);
    }
    averageRight /= 8;

averageLeft = 0;
    for (int i=0; i<8 ; i++)
    {
      averageLeft += analogRead(A1);
    }
    averageLeft /= 8;
  }

// compare each seco
  if (millis() - lastCompare > 1000)
  {
    lastCompare = millis();
    volumeRight = analogRead(A0);
    volumeLeft = analogRead(A1);

if (volumeRight > averageRight + 25) digitalWrite(led, HIGH);
    else digitalWrite(led, LOW);
    if (volumeLeft > averageLeft) digitalWrite(led, HIGH);
    else digitalWrite(led, LOW);

// a simple graph on serial
    int avg = (volumeRight + volumeLeft)/2;
      for (int i=0; i< avg/16; i++) Serial.print(’+’);
    Serial.println();
  }
}

This sketch is really close to what I need. Thank you for taking the time to make it. The only thing In need to do is to get it to compare the if ( volumeleft > avg) ech time it runs threw the sketch. As it is it only dose that when it samples the sound. Thank you all so much for your help. I did see the counting of millis sense last sample. Much better way of counting then my running down a counter.

raschemmel:
@OP,
How about a photo of a schematic drawn by hand of your circuit, along with a photo of your circuit so we can investigate the possibility that your problem is not software related.

There is not much of a wiring schematic, even if I draw it out. But here it is:

raschemmel:
Why don't you just do noise cancelling ? You sample the ambient noise , run it into and inverting op amp circuit then put it into an op amp summing amplifier as input 1 and put the signal the R2 is "hearing" into input 2 (obviously you need two mics for this).
The invered audio signal of the ambient , being as it is a mirror image of the original, when summed with the original will cancel out the ambient noise. The caveat is that the primary mic and the cancelling mic cannot be facing the same side of R2. The primary mic must face front, the ambient mic must face to the rear or sides. You can have mics on all four sides and use the three that don't face front for ambients , summing THOSE together and that SUM(NOISE) would be input -1 of the summer that sums the primary mic and ambient signals (two inputs). This is all old stuff but is works. It may be more trouble than you care to go to but
it would solve your problem with ambient noise. Surely one of the software guys is going to come back and say , but if you have the extra mics. the Summing Amp functions can be done in software with out all those silly op amps....

Thanks for the input. :slight_smile: I did fail to say that R2 has 5 of these mic's on his head. I had not explain the 5 mic's, as I thought if I worked simply and got one to work the others could be added relativity easy.

My second note, is that space is so limited with in R2. This has been a long ongoing project. I'm trying to use the existing Ardunio boards in him. I have simply run out of room. In some places I have them stacked on top of each other. Here is a short video of R2 in action (Vary short). :slight_smile:

A look inside the electronics bay:

Thank you again to all who have offered help. :slight_smile:

Does each mic have a dedicated mic breakout board and dedicated digital input pin ?

Yes each mike has its own breakout board and its on signal or input pin. using pins a0 left front, A1 right front, A3 left ,a8 right mic, a4 rear mic.,

I should also note that I have a Arduino Uno I use as a test board set up with one mic. this way I dont have to mess up R2's main sketches while testing.

You should change your program so R2 behaves like a human such that he turns to face the loudest sound, thereby always directing himself toward the source of it. If you do that A sound behind him will remain after the ambient noise is cancelled because that sound has a lower volume on the other three sides. The sound that is present on three sides but has a lower volume than the front side will be cancelled. (as mentioned , this can be done in s/w)