Advertisement
Guest User

Untitled

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