1#define motorPin1 2
2#define trigPin1 3
3#define echoPin1 4
4
5#define motorPin2 5
6#define trigPin2 6
7#define echoPin2 7
8
9#define motorPin3 8
10#define trigPin3 9
11#define echoPin3 10
12
13#define motorPin4 11
14#define trigPin4 12
15#define echoPin4 13
16
17#define motorPin5 16
18#define trigPin5 15
19#define echoPin5 14
20
21#define motorPin6 19
22#define trigPin6 18
23#define echoPin6 17
24
25#define motorPin7 22
26#define trigPin7 21
27#define echoPin7 20
28
29#define motorPin8 25
30#define trigPin8 24
31#define echoPin8 23
32void setup()
33{
34 Serial.begin(9600);
35 pinMode(echoPin1,INPUT);
36 pinMode(trigPin1,OUTPUT);
37 pinMode(motorPin1,OUTPUT);
38 digitalWrite(motorPin1,LOW);
39
40 pinMode(echoPin2,INPUT);
41 pinMode(trigPin2,OUTPUT);
42 pinMode(motorPin2,OUTPUT);
43 digitalWrite(motorPin2,LOW);
44
45 pinMode(echoPin3,INPUT);
46 pinMode(trigPin3,OUTPUT);
47 pinMode(motorPin3,OUTPUT);
48 digitalWrite(motorPin3,LOW);
49
50 pinMode(echoPin4,INPUT);
51 pinMode(trigPin4,OUTPUT);
52 pinMode(motorPin4,OUTPUT);
53 digitalWrite(motorPin4,LOW);
54
55 pinMode(echoPin5,INPUT);
56 pinMode(trigPin5,OUTPUT);
57 pinMode(motorPin5,OUTPUT);
58 digitalWrite(motorPin5,LOW);
59
60 pinMode(echoPin6,INPUT);
61 pinMode(trigPin6,OUTPUT);
62 pinMode(motorPin6,OUTPUT);
63 digitalWrite(motorPin6,LOW);
64
65 pinMode(echoPin7,INPUT);
66 pinMode(trigPin7,OUTPUT);
67 pinMode(motorPin7,OUTPUT);
68 digitalWrite(motorPin7,LOW);
69
70 pinMode(echoPin8,INPUT);
71 pinMode(trigPin8,OUTPUT);
72 pinMode(motorPin8,OUTPUT);
73 digitalWrite(motorPin8,LOW);
74
75
76}
77
78void loop()
79{
80 int duration, distance;
81
82
83 digitalWrite(trigPin1, HIGH);
84 digitalWrite(trigPin1, LOW);
85 duration = pulseIn(echoPin1, HIGH);
86 distance = (duration/2) / 29.1;
87 if (distance >=40){
88 Serial.println("Out of range");
89 digitalWrite(motorPin1,HIGH);
90 }
91 else {
92 Serial.println("su1 cm");
93 Serial.print(distance);
94 digitalWrite(motorPin1,LOW);
95 }
96
97 digitalWrite(trigPin2, HIGH);
98 digitalWrite(trigPin2, LOW);
99 duration = pulseIn(echoPin2, HIGH);
100 distance = (duration/2) / 29.1;
101 if (distance >=40){
102 Serial.println("Out of range");
103 digitalWrite(motorPin2,HIGH);
104 }
105 else {
106 Serial.println("su2 cm");
107 Serial.print(distance);
108 digitalWrite(motorPin2,LOW);
109 }
110
111 digitalWrite(trigPin3, HIGH);
112 digitalWrite(trigPin3, LOW);
113 duration = pulseIn(echoPin3, HIGH);
114 distance = (duration/2) / 29.1;
115 if (distance >=40){
116 Serial.println("Out of range");
117 digitalWrite(motorPin3,HIGH);
118 }
119 else {
120 Serial.println("su3 cm");
121 Serial.print(distance);
122 digitalWrite(motorPin3,LOW);
123 }
124
125 digitalWrite(trigPin4, HIGH);
126 digitalWrite(trigPin4, LOW);
127 duration = pulseIn(echoPin4, HIGH);
128 distance = (duration/2) / 29.1;
129 if (distance >=40){
130 Serial.println("Out of range");
131 digitalWrite(motorPin4,HIGH);
132 }
133 else {
134 Serial.println("su4 cm");
135 Serial.print(distance);
136 digitalWrite(motorPin4,LOW);
137 }
138
139 digitalWrite(trigPin5, HIGH);
140 digitalWrite(trigPin5, LOW);
141 duration = pulseIn(echoPin5, HIGH);
142 distance = (duration/2) / 29.1;
143 if (distance >=40){
144 Serial.println("Out of range");
145 digitalWrite(motorPin5,HIGH);
146 }
147 else {
148 Serial.println("su5 cm");
149 Serial.print(distance);
150 digitalWrite(motorPin5,LOW);
151 }
152
153 digitalWrite(trigPin6, HIGH);
154 digitalWrite(trigPin6, LOW);
155 duration = pulseIn(echoPin6, HIGH);
156 distance = (duration/2) / 29.1;
157 if (distance >=40){
158 Serial.println("Out of range");
159 digitalWrite(motorPin6,HIGH);
160 }
161 else {
162 Serial.println("su6 cm");
163 Serial.print(distance);
164 digitalWrite(motorPin6,LOW);
165 }
166
167 digitalWrite(trigPin7, HIGH);
168 digitalWrite(trigPin7, LOW);
169 duration = pulseIn(echoPin7, HIGH);
170 distance = (duration/2) / 29.1;
171 if (distance >=40){
172 Serial.println("Out of range");
173 digitalWrite(motorPin7,HIGH);
174 }
175 else {
176 Serial.println("su7 cm");
177 Serial.print(distance);
178 digitalWrite(motorPin7,LOW);
179 }
180
181 digitalWrite(trigPin8, HIGH);
182 digitalWrite(trigPin8, LOW);
183 duration = pulseIn(echoPin8, HIGH);
184 distance = (duration/2) / 29.1;
185 if (distance >=40){
186 Serial.println("Out of range");
187 digitalWrite(motorPin8,HIGH);
188 }
189 else {
190 Serial.println("su8 cm");
191 Serial.print(distance);
192 digitalWrite(motorPin8,LOW);
193 }
194
195 delay(500);
196}
197
198
199