-
Notifications
You must be signed in to change notification settings - Fork 0
/
abc.ino
51 lines (43 loc) · 1021 Bytes
/
abc.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
//define pins numbers
const int trigPin=9;
const int echoPin=10;
const int buzzerPin=11;
const int ledPin=13;
const int ldrPin=A0;
//define variables
long duration;
int distance;
int safetydistance;
void setup() {
Serial.begin(9600);
pinMode(ledPin,OUTPUT);
pinMode(
buzzerPin,OUTPUT);
pinMode(trigPin,OUTPUT);
pinMode(echoPin,INPUT);
pinMode(ldrPin,INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
int ldrstatus=analogRead(ldrPin);//read the state of ldr value
digitalWrite(trigPin,OUTPUT);
delayMicroseconds(1000);
digitalWrite(trigPin,LOW);
duration=pulseIn(echoPin,HIGH);
distance=(duration/2)/28.5;
Serial.println(distance);
if((distance<=400)&&(ldrstatus<=400)) {
tone(buzzerPin,200);
digitalWrite(ledPin,HIGH);
delay(500);
noTone(buzzerPin);
digitalWrite(ledPin,LOW);
delay(100);
Serial.println("--------ALARM ACTIVATED------");
}
else{
noTone(buzzerPin);
digitalWrite(ledPin,LOW);
Serial.println("ALARM DEACTIVATED");
}
}