Advertisement
Guest User

loudhouse

a guest
Sep 25th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. //A sketch to demonstrate the tone() function
  2.  
  3. //Specify digital pin on the Arduino that the positive lead of piezo buzzer is attached.
  4. int piezoPin = 8;
  5. int inputPin = 5; // choose the input pin (for PIR sensor)
  6. int pirState = LOW; // we start, assuming no motion detected
  7. int val = 0; // variable for reading the pin status
  8.  
  9. void alarm() {
  10. int frequency = 100;
  11. for (int frequency; frequency< 1500; frequency++) {
  12. tone(piezoPin, frequency, 500);
  13. }
  14. }
  15. void setup() {
  16. pinMode(inputPin, INPUT);
  17. Serial.begin(9600);
  18. }//close setup
  19.  
  20. void loop() {
  21.  
  22. /*Tone needs 2 arguments, but can take three
  23. 1) Pin#
  24. 2) Frequency - this is in hertz (cycles per second) which determines the pitch of the noise made
  25. 3) Duration - how long teh tone plays
  26. */
  27. delay(1000);
  28. val = digitalRead(inputPin);
  29. if (val == HIGH) {
  30. // motion detected
  31. // PLAY THE SOUND
  32. Serial.println("Motion detected. Sound the alarm!");
  33. alarm();
  34. }
  35. else {
  36. noTone(piezoPin);
  37. Serial.println("No motion detected...");
  38. }
  39.  
  40. //tone(piezoPin, 1000, 500);
  41. //delay(1000);
  42.  
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement