-
Notifications
You must be signed in to change notification settings - Fork 0
/
buzzer.ino
52 lines (46 loc) · 870 Bytes
/
buzzer.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
Arduino 1.8.19
*/
int buzzer = 5; // buzzer
int trig = 9;
int echo = 7;
void setup()
{
Serial.begin(10000);
pinMode(buzzer, OUTPUT);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
}
void loop()
{
digitalWrite(trig, LOW);
delayMicroseconds(3);
digitalWrite(trig, HIGH);
delayMicroseconds(3);
digitalWrite(trig, LOW);
int a = pulseIn(echo, HIGH);
int distance = a * 0.0343 / 2;
Serial.print("Value of US sensor");
Serial.println(distance);
if (distance < 50)
{
digitalWrite(buzzer, LOW);
if (distance < 25)
{
digitalWrite(buzzer, LOW);
delay(800);
digitalWrite(buzzer, LOW);
delay(800);
}
if (distance < 10)
{
digitalWrite(buzzer, HIGH);
delay(600);
digitalWrite(buzzer, LOW);
delay(600);
digitalWrite(buzzer, HIGH);
delay(600);
digitalWrite(buzzer, LOW);
delay(600);
}
}