-
Notifications
You must be signed in to change notification settings - Fork 0
/
sonar.ino
60 lines (53 loc) · 1.38 KB
/
sonar.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
53
54
55
56
57
58
59
60
const int trigPin1 = 9;
const int echoPin1 = 10;
const int trigPin2 = 1;
const int echoPin2 = 2;
const int trigPin3 = 3;
const int echoPin3 = 4;
const int trigPin4 = 5;
const int echoPin4 = 6;
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
duration = pulseIn(echoPin1, HIGH);
distance1= duration*0.034/2;
Serial.print("Distance1: ");
Serial.println(distance1);
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
duration = pulseIn(echoPin2, HIGH);
distance2= duration*0.034/2;
Serial.print("Distance1: ");
Serial.println(distance2);
digitalWrite(trigPin3, LOW);
delayMicroseconds(2);
digitalWrite(trigPin3, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin3, LOW);
duration = pulseIn(echoPin3, HIGH);
distance3= duration*0.034/2;
Serial.print("Distance3: ");
Serial.println(distance3);
digitalWrite(trigPin4, LOW);
delayMicroseconds(2);
digitalWrite(trigPin4, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin4, LOW);
duration = pulseIn(echoPin4, HIGH);
distance4= duration*0.034/2;
Serial.print("Distance4: ");
Serial.println(distance4);
}