Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //A sketch to demonstrate the tone() function
- //Specify digital pin on the Arduino that the positive lead of piezo buzzer is attached.
- int piezoPin = 8;
- int inputPin = 5; // choose the input pin (for PIR sensor)
- int pirState = LOW; // we start, assuming no motion detected
- int val = 0; // variable for reading the pin status
- void alarm() {
- int frequency = 100;
- for (int frequency; frequency< 1500; frequency++) {
- tone(piezoPin, frequency, 500);
- }
- }
- void setup() {
- pinMode(inputPin, INPUT);
- Serial.begin(9600);
- }//close setup
- void loop() {
- /*Tone needs 2 arguments, but can take three
- 1) Pin#
- 2) Frequency - this is in hertz (cycles per second) which determines the pitch of the noise made
- 3) Duration - how long teh tone plays
- */
- delay(1000);
- val = digitalRead(inputPin);
- if (val == HIGH) {
- // motion detected
- // PLAY THE SOUND
- Serial.println("Motion detected. Sound the alarm!");
- alarm();
- }
- else {
- noTone(piezoPin);
- Serial.println("No motion detected...");
- }
- //tone(piezoPin, 1000, 500);
- //delay(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement