Last active
January 28, 2019 10:24
-
-
Save BoyanHH/1f72683ee037a0d136aa04997326b957 to your computer and use it in GitHub Desktop.
Arduino alarm buzzer with sensor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Trigger pin to pin 12 | |
| Echo pin to pin 13 | |
| Buzzer to pin 8 | |
| GND pin of buzzer and sensor to GND | |
| */ | |
| int temp=0; | |
| int HZ=400; | |
| void setup() { | |
| Serial.begin (9600); | |
| pinMode(trigPin, OUTPUT); | |
| pinMode(echoPin, INPUT); | |
| pinMode(Buzzer, OUTPUT); | |
| } | |
| void loop() { | |
| int duration=0, distance=0; | |
| digitalWrite(trigPin, HIGH); | |
| delayMicroseconds(1000); | |
| digitalWrite(trigPin, LOW); | |
| duration = pulseIn(echoPin, HIGH); | |
| distance = (duration/2) / 29.1; | |
| if (distance >= 80 || distance <= 0){ | |
| digitalWrite(Buzzer, LOW); | |
| } | |
| else { | |
| for(;temp<10;temp++,HZ+=300) //Melody | |
| { | |
| tone(Buzzer, HZ); // Frequency HZ for time delay(). | |
| delay(200); | |
| } | |
| for(;temp>0;temp–,HZ-=300) //The melody backwards | |
| { | |
| tone(Buzzer, HZ); | |
| delay(200); | |
| } | |
| } | |
| noTone(Buzzer); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment