-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
323 lines (285 loc) · 7.22 KB
/
main.c
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#include <Ultrasonic.h>
void stop(); //暫停
void gs(); //直走
void bl(); //大左轉
void ml(); //中左轉
void sl(); //小左轉
void br(); //大右轉
void mr(); //中右轉
void sr(); //小右轉
int _ABVAR_1_L2 = 0 ;
int _ABVAR_2_L1 = 0 ;
int _ABVAR_3_R1 = 0 ;
int _ABVAR_4_R2 = 0 ;
int _ABVAR_5_IR = 0 ;
int _ABVAR_6_MFSpeed = 0 ;
int _ABVAR_7_MBSpeed = 0 ;
int _ABVAR_8_UltraSonic = 0 ;
// 黑1 白0
bool bool_ABVAR_1_L2 = 0 ;
bool bool_ABVAR_2_L1 = 0 ;
bool bool_ABVAR_3_R1 = 0 ;
bool bool_ABVAR_4_R2 = 0 ;
// 直到回到正軌 碰到LR2變1 直到碰到LR1才變回0
bool bool_overL = 0;
bool bool_overR = 0;
int ardublockUltrasonicSensorCodeAutoGeneratedReturnCM(int trigPin, int echoPin) //測距離
{
long duration;
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(20);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
duration = duration / 59;
if ((duration < 2) || (duration > 300)) return false;
return duration;
}
void US(); // 超音波
void L2_R2();
void L1_R1();
void setup()
{
pinMode( 11 , OUTPUT);
pinMode( 5 , OUTPUT);
pinMode( 6 , OUTPUT);
pinMode( 10 , OUTPUT);
digitalWrite( 12 , LOW );
Serial.begin(9600);
pinMode( 7 , OUTPUT);
pinMode( 8 , OUTPUT);
pinMode( 2 , OUTPUT);
pinMode( 3 , OUTPUT);
pinMode( 6 , OUTPUT);
pinMode( 11 , OUTPUT);
pinMode( 5 , OUTPUT);
pinMode( 10 , OUTPUT);
}
void loop()
{
_ABVAR_1_L2 = analogRead(0) ;
_ABVAR_2_L1 = analogRead(1) ;
_ABVAR_3_R1 = analogRead(2) ;
_ABVAR_4_R2 = analogRead(3) ;
_ABVAR_5_IR = 850 ; // 大於多少是白色
_ABVAR_6_MFSpeed = 175 ; //右馬達速度
_ABVAR_7_MBSpeed = 170 ; //左馬達速度
_ABVAR_8_UltraSonic = ardublockUltrasonicSensorCodeAutoGeneratedReturnCM( 12 , 13 ) ;
// bool bool_ABVAR_1_L2 = 0 ;
// bool bool_ABVAR_2_L1 = 0 ;
// bool bool_ABVAR_3_R1 = 0 ;
// bool bool_ABVAR_4_R2 = 0 ;
if (( _ABVAR_1_L2 ) >= ( _ABVAR_5_IR )) {
bool_ABVAR_1_L2 = 0;
} else {
bool_ABVAR_1_L2 = 1;
}
if (( _ABVAR_2_L1 ) >= ( _ABVAR_5_IR )) {
bool_ABVAR_2_L1 = 0;
} else {
bool_ABVAR_2_L1 = 1;
}
if (( _ABVAR_3_R1 ) >= ( _ABVAR_5_IR )) {
bool_ABVAR_3_R1 = 0;
} else {
bool_ABVAR_3_R1 = 1;
}
if (( _ABVAR_4_R2 ) >= ( _ABVAR_5_IR )) {
bool_ABVAR_4_R2 = 0;
} else {
bool_ABVAR_4_R2 = 1;
}
// print
Serial.print("L2 =");
Serial.print(_ABVAR_1_L2);
Serial.print(" ");
Serial.print(bool_ABVAR_1_L2);
Serial.println();
Serial.print("L1 =");
Serial.print(_ABVAR_2_L1);
Serial.print(" ");
Serial.print(bool_ABVAR_2_L1);
Serial.println();
Serial.print("R1 =");
Serial.print(_ABVAR_3_R1);
Serial.print(" ");
Serial.print(bool_ABVAR_3_R1);
Serial.println();
Serial.print("R2 =");
Serial.print(_ABVAR_4_R2);
Serial.print(" ");
Serial.print(bool_ABVAR_4_R2);
Serial.println();
Serial.print("US =");
Serial.print(_ABVAR_8_UltraSonic);
Serial.println();
digitalWrite( 7 , LOW );
digitalWrite( 8 , LOW );
digitalWrite( 2 , LOW );
digitalWrite( 3 , LOW );
// L1_R1();
// L2_R2();
judge();
// US();
}
void judge(){ //判斷
US();
digitalWrite( 7 , LOW);
digitalWrite( 8 , LOW);
digitalWrite( 2 , LOW);
digitalWrite( 3 , LOW);
digitalWrite( 6 , LOW);
digitalWrite( 11 , LOW);
digitalWrite( 5 , LOW);
digitalWrite( 10 , LOW);
if (bool_ABVAR_3_R1 && bool_ABVAR_1_L2 && bool_ABVAR_2_L1 && bool_ABVAR_4_R2) { //都黑色
stop();
bool_overR = 0;
bool_overL = 0;
}
if (!bool_ABVAR_3_R1 && !bool_ABVAR_1_L2 && !bool_ABVAR_2_L1 && !bool_ABVAR_4_R2) { //都白色
if (bool_overR) {
br();
} else if (bool_overL) {
bl();
} else {
gs();
}
}
if (bool_ABVAR_3_R1 && !bool_ABVAR_4_R2) { //正正
sr();
bool_overR = 0;
}
if (bool_ABVAR_2_L1 && !bool_ABVAR_1_L2) { //正正
sl();
bool_overL = 0;
}
if (bool_ABVAR_1_L2 && !bool_ABVAR_2_L1) { //正反
ml();
bool_overL = 1;
}
if (bool_ABVAR_4_R2 && !bool_ABVAR_3_R1) { //正反
mr();
bool_overR = 1;
}
if (bool_ABVAR_3_R1 && bool_ABVAR_4_R2) { //正反 兩個都碰
mr();
bool_overR = 1;
}
if (bool_ABVAR_2_L1 && bool_ABVAR_1_L2) { //正反 兩個都碰
ml();
bool_overL = 1;
}
}
void US()
{
if (( ( ( _ABVAR_8_UltraSonic ) < ( 18 ) ) && ( ( _ABVAR_8_UltraSonic ) > ( 0 ) ) ))
{
stop();
}
}
void stop() {
digitalWrite( 6 , LOW );
analogWrite(5 , 0);
digitalWrite( 11 , LOW );
analogWrite(10 , 0);
digitalWrite( 7 , LOW );
digitalWrite( 8 , LOW );
digitalWrite( 2 , LOW );
digitalWrite( 3 , LOW );
delay( 100 );
digitalWrite( 7 , HIGH );
digitalWrite( 8 , HIGH );
digitalWrite( 2 , HIGH );
digitalWrite( 3 , HIGH );
delay( 100 );
};
void gs() { //直走
digitalWrite( 2 , HIGH );
digitalWrite( 8 , HIGH );
analogWrite(5 , 170);
analogWrite(10 , 170);
};
void sl() { //小左轉
digitalWrite( 2 , HIGH );
digitalWrite( 8 , HIGH );
analogWrite(5 , 130);
analogWrite(10 , 170);
};
void ml() { //中左轉
digitalWrite( 2 , HIGH );
digitalWrite( 8 , HIGH );
analogWrite(6 , 0);
analogWrite(10 , 170);
}
void bl() { //大左轉
digitalWrite( 2 , HIGH );
digitalWrite( 8 , HIGH );
analogWrite(6 , 170);
analogWrite(10 , 170);
};
void sr() { //小右轉
digitalWrite( 2 , HIGH );
digitalWrite( 8 , HIGH );
analogWrite(5 , 170);
analogWrite(10 , 130);
};
void mr() { //中右轉
digitalWrite( 2 , HIGH );
digitalWrite( 8 , HIGH );
analogWrite(5 , 170);
analogWrite(11 , 0);
};
void br() { //大右轉
digitalWrite( 2 , HIGH );
digitalWrite( 8 , HIGH );
analogWrite(5 , 170);
analogWrite(11 , 170);
};
void L2_R2()
{
if (( ( ( _ABVAR_1_L2 ) < ( _ABVAR_5_IR ) ) && ( ( ( _ABVAR_2_L1 ) > ( _ABVAR_5_IR ) ) && ( ( ( _ABVAR_3_R1 ) > ( _ABVAR_5_IR ) ) && ( ( _ABVAR_4_R2 ) > ( _ABVAR_5_IR ) ) ) ) ))
{
digitalWrite( 7 , HIGH );
while ( ( ( analogRead(1) ) > ( _ABVAR_5_IR ) ) )
{
digitalWrite( 5 , LOW );
analogWrite(6 , _ABVAR_7_MBSpeed);
digitalWrite( 11 , LOW );
analogWrite(10 , _ABVAR_6_MFSpeed);
}
}
if (( ( ( _ABVAR_4_R2 ) < ( _ABVAR_5_IR ) ) && ( ( ( _ABVAR_3_R1 ) > ( _ABVAR_5_IR ) ) && ( ( ( _ABVAR_2_L1 ) > ( _ABVAR_5_IR ) ) && ( ( _ABVAR_1_L2 ) > ( _ABVAR_5_IR ) ) ) ) ))
{
digitalWrite( 3 , HIGH );
while ( ( ( analogRead(2) ) >= ( _ABVAR_5_IR ) ) )
{
digitalWrite( 6 , LOW );
analogWrite(5 , _ABVAR_6_MFSpeed);
digitalWrite( 10 , LOW );
analogWrite(11 , _ABVAR_7_MBSpeed);
}
}
}
void L1_R1()
{
if (( ( ( _ABVAR_2_L1 ) < ( _ABVAR_5_IR ) ) && ( ( ( _ABVAR_1_L2 ) > ( _ABVAR_5_IR ) ) && ( ( ( _ABVAR_3_R1 ) > ( _ABVAR_5_IR ) ) && ( ( _ABVAR_4_R2 ) > ( _ABVAR_5_IR ) ) ) ) ))
{
digitalWrite( 8 , HIGH );
digitalWrite( 5 , LOW );
analogWrite(6 , _ABVAR_7_MBSpeed);
digitalWrite( 11 , LOW );
analogWrite(10 , _ABVAR_6_MFSpeed);
}
if (( ( ( _ABVAR_3_R1 ) < ( _ABVAR_5_IR ) ) && ( ( ( _ABVAR_4_R2 ) > ( _ABVAR_5_IR ) ) && ( ( ( _ABVAR_2_L1 ) > ( _ABVAR_5_IR ) ) && ( ( _ABVAR_1_L2 ) > ( _ABVAR_5_IR ) ) ) ) ))
{
digitalWrite( 2 , HIGH );
digitalWrite( 6 , LOW );
analogWrite(5 , _ABVAR_6_MFSpeed);
digitalWrite( 10 , LOW );
analogWrite(11 , _ABVAR_7_MBSpeed);
}
}