pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

C++ pastebin - collaborative debugging tool View Help


Posted by monitron on Wed 22 Apr 05:33
report abuse | View followups from kinnison | download | new post

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

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me so that I can delete my post