-
Notifications
You must be signed in to change notification settings - Fork 0
/
8array.ino
370 lines (268 loc) · 6.6 KB
/
8array.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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
const int motorRPin1 = 2; // signal pin 1 for the right motor, connect to IN1
const int motorRPin2 = 4; // signal pin 2 for the right motor, connect to IN2
const int motorREnable = 3; // enable pin for the right motor (PWM enabled)
const int motorLPin1 = 7; // signal pin 1 for the left motor, connect to IN3
const int motorLPin2 = 5; // signal pin 2 for the left motor, connect to IN4
const int motorLEnable = 6; // enable pin for the left motor (PWM enabled)
const int trig = 10;
const int echo = 11;
const int led =13;
const int irPins[8] = {12, A0, A2, A3, A4, A5 , 9 , 8};
int irSensorDigital[8] = {0, 0, 0, 0, 0, 0, 0, 0};
int i, j = 0;
int d = 1000;
int duration=0,distance=0;
int irSensors = B00000000;
int motorLSpeed = 100;
int motorRSpeed = 100;
int error = 30; // 145 best 200 // normal 255 // mad 0
int errorm = 60;
void setup()
{
Serial.begin(9600);
pinMode(motorLPin1, OUTPUT);
pinMode(motorLPin2, OUTPUT);
pinMode(motorLEnable, OUTPUT);
pinMode(motorRPin1, OUTPUT);
pinMode(motorRPin2, OUTPUT);
pinMode(motorREnable, OUTPUT);
pinMode(echo,INPUT);
pinMode(trig,OUTPUT);
pinMode(led,OUTPUT);
/* Set-up IR sensor pins as input */
for (int i = 0; i <= 7; i++)
{
pinMode(irPins[i], INPUT);
}
}
void loop() {
scanD();
check();
sonar();
}
void check( )
{
switch (irSensors) {
/*case B00000000: // on white paper
stopme();
break;*/
case B01111111: // leftmost sensor on the line
rightR();
break;
case B10111111:
rightS();
break;
case B11011111:
rightG();
break;
case B11101111:
go();
break;
case B11110111:
go();
break;
case B11111011:
leftG();
break;
case B11111101:
leftS();
break;
case B11111110:
leftR();
break;
case B00111111:
rightR();
break;
case B10011111:
rightS();
break;
case B11001111:
rightG();
break;
case B11100111:
go();
break;
case B11110011:
leftG();
break;
case B11111001:
leftS();
break;
case B11111100:
leftR();
break;
case B00011111:
rightR();
break;
case B10001111:
rightS();
break;
case B11000111:
go();
break;
case B11100011:
go();
break;
case B11110001:
leftS();
break;
case B11111000:
leftR();
break;
case B00001111:
rightR();
break;
case B10000111:
rightS();
break;
case B11000011:
go();
break;
case B11100001:
leftS();
break;
case B11110000:
leftR();
break;
case B00000111:
rightR();
break;
case B10000011:
rightS();
break;
case B11000001:
leftS();
break;
case B11100000:
leftR();
break;
case B00000011:
rightS();
break;
case B10000001:
go();
break;
case B11000000:
leftS();
break;
case B00000001:
rightG();
break;
case B10000000:
leftG();
break;
case B11111111:
stopme();
break;
case B00000000:
{
go();
delay(20);
break;
}
default:
Serial.print("Unhandled case: ");
}
}
void rightR()
{
Serial.println(" right motor forward (spin) sharp");
analogWrite(motorREnable, motorRSpeed -errorm);
digitalWrite(motorRPin1, LOW);
digitalWrite(motorRPin2, HIGH);
analogWrite(motorLEnable, motorLSpeed +error);
digitalWrite(motorLPin1, LOW);
digitalWrite(motorLPin2, HIGH);
}
void rightS()
{
Serial.println(" right motor forward (spin) slow");
analogWrite(motorREnable, motorRSpeed - error);
digitalWrite(motorRPin1, LOW);
digitalWrite(motorRPin2, HIGH);
analogWrite(motorLEnable, motorLSpeed +error);
digitalWrite(motorLPin1, LOW);
digitalWrite(motorLPin2, HIGH);
}
void rightG()
{
Serial.println(" right motor forward (spin) gentle");
analogWrite(motorREnable, motorRSpeed - error);
digitalWrite(motorRPin1, LOW);
digitalWrite(motorRPin2, HIGH);
analogWrite(motorLEnable, motorLSpeed+error);
digitalWrite(motorLPin1, LOW);
digitalWrite(motorLPin2, HIGH);
}
void leftR() //turn left
{
Serial.println(" left motor forward (spin) sharp");
analogWrite(motorREnable, motorRSpeed +errorm);
digitalWrite(motorRPin1, HIGH);
digitalWrite(motorRPin2, LOW);
analogWrite(motorLEnable, motorLSpeed - error);
digitalWrite(motorLPin1, HIGH);
digitalWrite(motorLPin2, LOW);
}
void leftS() //turn left
{
Serial.println(" left motor forward (spin) slow");
analogWrite(motorREnable, motorRSpeed+error);
digitalWrite(motorRPin1, HIGH);
digitalWrite(motorRPin2, LOW);
analogWrite(motorLEnable, motorLSpeed - error);
digitalWrite(motorLPin1, HIGH);
digitalWrite(motorLPin2, LOW);
}
void leftG() //turn left
{
Serial.println(" left motor forward (spin) gentle");
analogWrite(motorREnable, motorRSpeed +error);
digitalWrite(motorRPin1, HIGH);
digitalWrite(motorRPin2, LOW);
analogWrite(motorLEnable, motorLSpeed - error);
digitalWrite(motorLPin1, HIGH);
digitalWrite(motorLPin2, LOW);
}
void go()
{
Serial.println(" forward ");
analogWrite(motorREnable, motorRSpeed);
digitalWrite(motorRPin1, HIGH);
digitalWrite(motorRPin2, LOW);
analogWrite(motorLEnable, motorLSpeed);
digitalWrite(motorLPin1, LOW);
digitalWrite(motorLPin2, HIGH);
}
void stopme()
{
Serial.println(" stop");
analogWrite(motorREnable, motorRSpeed);
digitalWrite(motorRPin1, LOW);
digitalWrite(motorRPin2, LOW);
analogWrite(motorLEnable, motorLSpeed);
digitalWrite(motorLPin1, LOW);
digitalWrite(motorLPin2, LOW);
}
void scanD()
{
for ( byte count = 0; count < 8; count++ )
{
bitWrite(irSensors, count, !digitalRead( irPins[count] ));
}
Serial.println(irSensors, BIN);
}
void sonar()
{
digitalWrite(trig,LOW);
delayMicroseconds(2);
digitalWrite(trig,HIGH);
delayMicroseconds(10);
duration=pulseIn(echo,HIGH);
distance=(duration/58.2);
delay(10);
if(distance<18)
{
digitalWrite(led,HIGH);
}
}