Noise Monitor: Electret Quacking Duck

When the noise level is too high the duck will quack.

The electret mic will pick up noise from across the room.
The quacks are randomized in the code, so the length of each quack will be varied, as will the space between the quacks.
I used the same electret mic / op amp set up as this project:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1263440809/0#0
The electret mic requires a special circuit documented on that page.
The code uses the True Random library. It’s a bit more random than the normal arduino random function. It is available here: http://code.google.com/p/tinkerit/wiki/TrueRandom
The ‘quacking’ is a buzzer. http://www.radioshack.com/product/index.jsp?productId=2062395
The op amp to go with the mic is here:http://www.sparkfun.com/commerce/product_info.php?products_id=9816
The arduino and buzzer were sewn inside the beanie baby duck.

#include <TrueRandom.h>
//This uses the TrueRandom Library

int speaker = 9; //the piezo buzzer on pin 9
int tone = 1500;

int sensorPin = 4;    //Microphone Sensor Pin
int sensorValue = 0; 

long unsigned present = 0;
long unsigned past = 0;


int RandomQuack = 0;
int RandomRest = 0;
int VarQuack = 0;
int VarRest = 0;



/*
Quack = 200
quick = 100
with a 100 space between

*/

void setup () {

pinMode (speaker, OUTPUT);
Serial.begin(9600);
}

void loop ()
{
sensorValue = analogRead(sensorPin);    
Serial.println(sensorValue);
  
  
if (sensorValue<509){ // if the noise level is high
  
  

  
//-------------------------------
VarQuack = TrueRandom.random(0,2);  // assign a Random Quack  
if (VarQuack == 0){               // it will be either 100 or 200 in duration
  RandomQuack = 100;
  Serial.println("short Quack");
} else {
  RandomQuack = 250;
  Serial.println("Quack");
}

VarRest = TrueRandom.random(0,2);  // assign a Random Rest time between quacks
if (VarRest == 0){               // it will be 50 or 100
  RandomRest = 50;
} else {
  RandomRest = 100;
}

  
past = millis();

//----------------------------------
do{                                     // QUACK do/while loop
present = millis();

digitalWrite (speaker, HIGH);
delayMicroseconds (tone / 2);
digitalWrite (speaker, LOW);
delayMicroseconds (tone / 2);
tone = 1500;
tone = map(tone, 0, 1023, 1000, 5000);

} while (present - past < RandomQuack);          // a random QUACK duration

delay (RandomRest);                   // random delay between Quacks

//----------------------------------------------
// assign Random Values again
VarQuack = TrueRandom.random(0,2);  // assign a Random Quack  
if (VarQuack == 0){               // it will be either 100 or 200 in duration
  RandomQuack = 100;
  Serial.println("short Quack");
} else {
  RandomQuack = 200;
  Serial.println("Quack");
}

VarRest = TrueRandom.random(0,2);  // assign a Random Rest time between quacks
if (VarRest == 0){               // it will be 50 or 100
  RandomRest = 50;
} else {
  RandomRest = 100;
}

//------------------------------------
past = millis();

do{  // second random quack
present = millis();
  
digitalWrite (speaker, HIGH);
delayMicroseconds (tone / 2);
digitalWrite (speaker, LOW);
delayMicroseconds (tone / 2);
tone = 1500;
tone = map(tone, 0, 1023, 1000, 5000);

} while (present - past < RandomQuack);     // another random quack duration
//-------------------------------------

delay (RandomRest);                   // delay between quack


//----------------------------------------------
// assign Random Values again
VarQuack = TrueRandom.random(0,2);  // assign a Random Quack  
if (VarQuack == 0){               // it will be either 100 or 200 in duration
  RandomQuack = 100;
  Serial.println("short Quack");
  Serial.println(" ");
} else {
  RandomQuack = 200;
  Serial.println("Quack");
  Serial.println(" ");
}

VarRest = TrueRandom.random(0,2);  // assign a Random Rest time between quacks
if (VarRest == 0){               // it will be 50 or 100
  RandomRest = 50;
} else {
  RandomRest = 100;
}
//-------------------------------------------


past = millis();

do{  // Third random quack
present = millis();
  
digitalWrite (speaker, HIGH);
delayMicroseconds (tone / 2);
digitalWrite (speaker, LOW);
delayMicroseconds (tone / 2);
tone = 1500;
tone = map(tone, 0, 1023, 1000, 5000);

} while (present - past < RandomQuack);     // another random quack duration
//-------------------------------------

delay (RandomRest);                   

delay(1000);

} else { 
  digitalWrite(speaker, LOW);
}


}