Skip to content

Commit 2f8dc25

Browse files
authored
Create distance.ino
1 parent 653c389 commit 2f8dc25

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

distance/distance.ino

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
two_ultrasonic.ino
2+
//Sonar 1
3+
int echoPin1 = 3;
4+
int initPin1 = 2;
5+
int distance1 =0;
6+
7+
//Sonar 2
8+
int echoPin2 = 8;
9+
int initPin2 = 7;
10+
int distance2 =0;
11+
12+
int ledPin3 = 13; // for led
13+
14+
// for indivisual sensor -> led will start blowing and stop when distance is graeter than critcal distance =>50 cm
15+
int isDisSensor = 0;
16+
int isDisSensor2 = 0;
17+
18+
void setup() {
19+
pinMode(initPin1, OUTPUT);
20+
pinMode(echoPin1, INPUT);
21+
pinMode(initPin2, OUTPUT);
22+
pinMode(echoPin2, INPUT);
23+
pinMode(ledPin3, OUTPUT);
24+
Serial.begin(9600);
25+
n }
26+
27+
void loop() {
28+
distance1 = getDistance(initPin1, echoPin1);
29+
printDistance(1, distance1);
30+
delay(1000);
31+
32+
distance2 = getDistance(initPin2, echoPin2);
33+
printDistance(2, distance2);
34+
delay(1000);
35+
}
36+
37+
/* called by individual senser to get distance
38+
first triger the triger and than delay with 1 micro second
39+
than stop the triger and
40+
start echo to get wave back to sensor
41+
and messure distance
42+
---- last line */
43+
int getDistance (int initPin, int echoPin){
44+
digitalWrite(initPin, HIGH);
45+
delayMicroseconds(10);
46+
digitalWrite(initPin, LOW);
47+
48+
unsigned long pulseTime = pulseIn(echoPin, HIGH);
49+
int distance = pulseTime/58;
50+
return distance;
51+
52+
}
53+
54+
/* LOW ==> stop
55+
* HIGH ==> start working
56+
*
57+
*
58+
* id == sesonr id
59+
distance == distance got from sensar
60+
if
61+
checking for minimum distance <= 50
62+
write out of rang and
63+
check by which sensor this method call
64+
making that sensor mini distcane == 1
65+
and led will call to High
66+
else
67+
write the -----
68+
cheking from which sensor called
69+
making that sensor mini distcane variable == 0
70+
and call led to LOW */
71+
72+
void printDistance(int id, int dist) {
73+
Serial.print("sensor number ");
74+
Serial.print(id);
75+
Serial.print(": ");
76+
if (dist <= 50 || dist == 0 ){
77+
Serial.print ( dist);
78+
Serial.println(" Out of range");
79+
if(id ==1) {
80+
isDisSensor = 1;
81+
digitalWrite(ledPin3, HIGH);
82+
} else {
83+
isDisSensor2 = 1;
84+
digitalWrite(ledPin3, HIGH);
85+
}
86+
} else {
87+
Serial.print("----------");
88+
if(id ==1) {
89+
if(isDisSensor == 1){
90+
isDisSensor = 0;
91+
digitalWrite(ledPin3, LOW);
92+
}
93+
} else {
94+
if(isDisSensor2 == 1){
95+
isDisSensor2 = 0;
96+
digitalWrite(ledPin3, LOW);
97+
}
98+
}
99+
100+
Serial.print(dist, DEC);
101+
Serial.println(" cm");
102+
}
103+
}

0 commit comments

Comments
 (0)