-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobot.ino
237 lines (224 loc) · 5.09 KB
/
robot.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include <Servo.h>
Servo myservo;
int pos = 0;
const int trig = 10;
const int echo = 9;
int tien1 = 7;
int tien2 = 5;
int lui1 = 6;
int lui2 = 4;
void dithang();
void disangtrai(int goc);
void disangphai(int goc);
void diluitrai(int goc);
void diluiphai(int goc);
void dilui();
void diquaydau();
void dunglai();
int dokhoangcach();
void setup()
{
myservo.attach(3);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
pinMode(tien1, OUTPUT);
pinMode(tien2, OUTPUT);
pinMode(lui1, OUTPUT);
pinMode(lui2, OUTPUT);
digitalWrite(tien1, LOW);
digitalWrite(tien2, LOW);
digitalWrite(lui1, LOW);
digitalWrite(lui2, LOW);
myservo.write(90);
delay(1000);
int kcmaxt = 0, kcmidt = 0;
while (1)
{
int khoangcach, gd, i, gdmax = 0, gdmid = 0, gdmin = 0, kcmax = 0, kc810[21], kc90 = 500;
for (gd = 90; gd >= 15; gd--)
{
myservo.write(gd);
delay(10);
}
for (gd = 15; gd <= 165; gd++)
{
myservo.write(gd);
delay(10);
khoangcach = dokhoangcach();
if (khoangcach < 10)
gdmin = gd;
else
{
if (khoangcach < 30)
gdmid = gd;
else
{
if (khoangcach > kcmax)
{
kcmax = khoangcach;
gdmax = gd;
}
}
}
if (gd >= 80 && gd <= 100)
{
kc810[gd - 80] = dokhoangcach();
if (kc810[gd - 80] == 0)
kc810[gd - 80] = 500;
else
{
if (kc810[gd - 80] < kc90)
kc90 = kc810[gd - 80];
}
}
}
for (gd = 165; gd >= 90; gd--)
{
myservo.write(gd);
delay(10);
}
while (kc90 > 50 && gdmid < 15 && gdmin < 15)
{
if (kc90 == kcmidt)
dilui();
else
{
dithang();
kc90 = dokhoangcach();
}
}
dunglai();
if (gdmin == 0)
{
if (gdmid == 0)
{
if (gdmax < 80)
disangphai(gdmax);
else
disangtrai(gdmax);
}
else
{
if (gdmid < 80)
{
if (gdmax < 80)
diquaydau();
else
disangtrai(gdmax);
}
else
{
if (gdmid > 100)
{
if (gdmax < 80)
disangphai(gdmax);
else
diquaydau();
}
else
{
if (gdmax < 80)
disangphai(gdmax);
else
{
if (gdmax > 100)
disangtrai(gdmax);
else
diquaydau();
}
}
}
}
}
else
{
if (gdmin < 80)
diluiphai(gdmin);
else
{
if (gdmin > 100)
diluitrai(gdmin);
else
dilui();
}
}
i = kcmax / 10;
if (i == kcmaxt)
dilui();
else
kcmaxt = i;
}
}
void loop()
{
}
void dithang()
{
digitalWrite(tien1, HIGH);
digitalWrite(tien2, HIGH);
analogWrite(tien2, 230);
}
void disangtrai(int goc)
{
int temp = abs(goc - 90) * 3;
digitalWrite(tien2, HIGH);
delay(temp);
digitalWrite(tien2, LOW);
}
void disangphai(int goc)
{
int temp = abs(goc - 90) * 3;
digitalWrite(tien1, HIGH);
delay(temp);
digitalWrite(tien1, LOW);
}
void diluitrai(int goc)
{
int temp = abs(goc - 90) * 5;
digitalWrite(lui1, HIGH);
delay(temp);
digitalWrite(lui1, LOW);
}
void diluiphai(int goc)
{
int temp = abs(goc - 90) * 5;
digitalWrite(lui2, HIGH);
delay(temp);
digitalWrite(lui2, LOW);
}
void dilui()
{
digitalWrite(lui1, HIGH);
digitalWrite(lui2, HIGH);
delay(500);
digitalWrite(lui1, LOW);
digitalWrite(lui2, LOW);
}
void diquaydau()
{
digitalWrite(tien1, HIGH);
digitalWrite(lui2, HIGH);
delay(300);
digitalWrite(tien1, LOW);
digitalWrite(lui2, LOW);
}
void dunglai()
{
digitalWrite(tien1, LOW);
digitalWrite(tien2, LOW);
digitalWrite(lui1, LOW);
digitalWrite(lui2, LOW);
}
int dokhoangcach()
{
long tg;
int kc;
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
tg = pulseIn(echo, HIGH, 3600000);
kc = int(tg / 2 / 29.412);
return kc;
}