Share Pastebin
Guest
Public paste!

monitron

By: a guest | Apr 22nd, 2009 | Syntax: C++ | Size: 3.46 KB | Hits: 242 | Expires: Never
Copy text to clipboard
  1. /*
  2.  * Noise Announcer for Arduino
  3.  * Justin Streufert - monitron@gmail.com
  4.  * http://monitron.vox.com/
  5.  *
  6.  * Uses an amplified microphone to detect loud noises. Sets off a light and
  7.  * sound alarm, as well as sending a message over serial that can be used as
  8.  * a remote trigger.
  9.  * Goes into a "cool-down" state after an alarm sounds, so as to mitigate annoyance.
  10.  *
  11.  * TODO possible improvements:
  12.  * - Configurable remotely through XBee
  13.  * - Better noise discrimination?
  14.  * - Serial Heartbeat
  15.  *
  16.  * BOM:
  17.  * Radio Shack 273-059 Piezo Buzzer on pin 5
  18.  * Radio Shack 276-028 "Full Color" LED on pins 9, 10 & 11, connected to +5V through a resistor
  19.  * Radio Shack 270-092 Electret microphone on pin 2
  20.  *   connected through a LM386 X200 amplifier circuit as seen here:
  21.  *   http://www.josepino.com/circuits/?mini_amplifier_lm386.jpc
  22.  * Optional XBee and XBee Shield
  23.  */
  24.  
  25. // Pins
  26. const int micPin    = 2;  // Analog Input pin for amplified microphone signal
  27. const int buzzerPin = 5;  // Digital Output pin for piezo buzzer
  28. const int redPin    = 9;  // Digital Output pin for red LED   (sink; LOW = on)
  29. const int greenPin  = 10; // Digital Output pin for green LED (sink; LOW = on)
  30. const int bluePin   = 11; // Digital Output pin for blue LED  (sink; LOW = on)
  31.  
  32. // Configuration Constants
  33. const int micCenter = 490;          // The bias of the LM386 amplifier. Approximate, empirically defined.
  34. const int numToAverage = 300;       // The number of samples to take before taking an average.
  35. const int loudNoiseThreshold = 240; // How loud do things need to get before we complain?
  36. const int buzzDuration = 6000;      // The number of cycles of buzzing
  37. const int buzzPeriod = 185;         // Determines the frequency of the buzzing (us)
  38. const int coolDuration = 60000;     // Minimum time between alarms (ms)
  39.  
  40. // Working Variables
  41. int numCollected = 0;    // Number of samples collected so far
  42. long sampleTotal = 0;    // The total value of the samples collected so far
  43.  
  44. void setup() {
  45.   // Declare LED and buzzer pins as outputs
  46.   pinMode(redPin, OUTPUT);
  47.   pinMode(greenPin, OUTPUT);
  48.   pinMode(bluePin, OUTPUT);
  49.   pinMode(buzzerPin, OUTPUT);
  50.   // Set initial state of LED (green)
  51.   digitalWrite(greenPin, LOW);
  52.   digitalWrite(bluePin, HIGH);
  53.   digitalWrite(redPin, HIGH);
  54.   // Start serial connection to XBee or PC
  55.   Serial.begin(9600);
  56. }
  57.  
  58. void loop() {
  59.   int averaged;
  60.   int i;
  61.   int val;
  62.   numCollected++;
  63.   val = abs(analogRead(micPin) - micCenter); // read & normalize value from mic
  64.   sampleTotal += val;
  65.   if (numCollected == numToAverage) { // Ready to check for loudness?
  66.     averaged = sampleTotal / numToAverage;
  67.     // Enable for debugging:
  68.     // Serial.print("Avg: ");
  69.     // Serial.println(averaged);
  70.     if(averaged > loudNoiseThreshold) {
  71.       Serial.println("LOUD!!!"); // Tell the world :)
  72.       digitalWrite(greenPin, HIGH); // Switch to the red light
  73.       digitalWrite(redPin, LOW);
  74.       for(i=1;i!=buzzDuration;i++) { // Make a noise through the piezo buzzer by toggling it on and off
  75.         delayMicroseconds(buzzPeriod);
  76.         digitalWrite(buzzerPin, HIGH);
  77.         delayMicroseconds(buzzPeriod);
  78.         digitalWrite(buzzerPin, LOW);
  79.       }
  80.       digitalWrite(redPin, HIGH); // Go to blue
  81.       digitalWrite(bluePin, LOW);
  82.       delay(coolDuration);
  83.       digitalWrite(greenPin, LOW); // Back to green
  84.       digitalWrite(bluePin, HIGH);
  85.     }
  86.     numCollected = 0; // Reset to collect new samples
  87.     sampleTotal = 0;
  88.   }
  89. }