Advertisement
Guest User

Untitled

a guest
Sep 27th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.99 KB | None | 0 0
  1. int soundDetectedPin = 10; // Use Pin 10 as our Input
  2. int soundDetectedVal = HIGH; // This is where we record our Sound Measurement
  3. boolean bAlarm = false;
  4.  
  5. unsigned long lastSoundDetectTime; // Record the time that we measured a sound
  6.  
  7.  
  8. int soundAlarmTime = 500; // Number of milli seconds to keep the sound alarm high
  9.  
  10.  
  11. void setup ()
  12. {
  13.   Serial.begin(9600);  
  14.   pinMode (soundDetectedPin, INPUT) ; // input from the Sound Detection Module
  15. }
  16. void loop ()
  17. {
  18.   soundDetectedVal = digitalRead (soundDetectedPin) ; // read the sound alarm time
  19.  
  20.   if (soundDetectedVal == LOW) // If we hear a sound
  21.   {
  22.  
  23.     lastSoundDetectTime = millis(); // record the time of the sound alarm
  24.     // The following is so you don't scroll on the output screen
  25.     if (!bAlarm){
  26.       Serial.println("LOUD, LOUD");
  27.       bAlarm = true;
  28.     }
  29.   }
  30.   else
  31.   {
  32.     if( (millis()-lastSoundDetectTime) > soundAlarmTime  &&  bAlarm){
  33.       Serial.println("quiet");
  34.       bAlarm = false;
  35.     }
  36.   }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement