-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinalCodeVersion.ino
More file actions
391 lines (363 loc) · 9.04 KB
/
FinalCodeVersion.ino
File metadata and controls
391 lines (363 loc) · 9.04 KB
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#define servoPin 9
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BNO055.h>
#include <utility/imumaths.h>
#include <EEPROM.h>
Adafruit_BNO055 bno = Adafruit_BNO055(55);
int motorLVOLT = 5;
int motorLGND = 6;
int IRSensor = 9;
int motorRVOLT = 11;
int turnC = 3;
int motorRGND = 10;
int prevTime = 0;
int s = 25;
int f = 38;
int t = 43; // 50
int cons = 100;
// 1
// 4 2
// 3
int servo0 = 3.5;
int i = 1;
int leftMSpeed = 200; //190
int rightMSpeed = 200; //200
int ang = 3;
volatile long motorPositionA = 0;
volatile long motorPositionB = 0;
//boolean stopAUTO = true;
void setup() {
pinMode(motorLVOLT, OUTPUT);
pinMode(motorRVOLT, OUTPUT);
pinMode(motorLGND, OUTPUT);
pinMode(motorRVOLT, OUTPUT);
pinMode(IRSensor, INPUT);
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(2), updateMotorPositionA, RISING);
attachInterrupt(digitalPinToInterrupt(3), updateMotorPositionB, RISING);
Serial.begin(9600);
Serial.println("Orientation Sensor Test"); Serial.println("");
/* Initialise the sensor */
if(!bno.begin())
{
/* There was a problem detecting the BNO055 ... check your connections */
Serial.print("Ooops, no BNO055 detected ... Check your wiring or I2C ADDR!");
while(1);
}
delay(1000);
bno.setExtCrystalUse(true);
}
void forwardAndRight(){
forward(50,1);
align(2);
forward(8.5,2);
align(2);
}
void forwardAndLeft(){
forward(50,1);
align(4);
forward(8.5,4);
align(4);
}
void forward(int deljay, int head){
// deljay-=2;
deljay *= 20;
analogWrite(motorLVOLT, leftMSpeed);
analogWrite(motorRVOLT, rightMSpeed);
analogWrite(motorLGND, 0);
// delay(500);
analogWrite(motorRGND, 0);
delay(500);
// delay((deljay*1150/50));
align(head);
// stop(100);
// delay((deljay*1150/50));
motorPositionB = 0;
while (true){
Serial.println(motorPositionB);
delay(0.1);
align(head);
// changeSpeed(head);
int sensorStatus = digitalRead(IRSensor);
if (sensorStatus == 1){
break;
}
if(motorPositionB < -1*deljay*42.5/49){
break;
}
else{
analogWrite(motorLVOLT, leftMSpeed);
analogWrite(motorRVOLT, rightMSpeed);
analogWrite(motorLGND, 0);
analogWrite(motorRGND, 0);
}
}
align(head);
stop(100);
motorPositionB=0;
}
void backward(int deljay, int head){
// deljay-=2;
deljay *= 20;
analogWrite(motorLVOLT, 0);
analogWrite(motorRVOLT, 0);
analogWrite(motorLGND, leftMSpeed);
// delay(500);
analogWrite(motorRGND, rightMSpeed);
delay(500);
// delay((deljay*1150/50));
align(head);
// stop(100);
// delay((deljay*1150/50));
motorPositionB = 0;
while (true){
Serial.println(motorPositionB);
delay(0.1);
align(head);
// changeSpeed(head);
int sensorStatus = digitalRead(IRSensor);
if (sensorStatus == 1){
break;
}
if(motorPositionB > 1*deljay*42.5/49){
break;
}
else{
analogWrite(motorLVOLT, 0);
analogWrite(motorRVOLT, 0);
analogWrite(motorLGND, leftMSpeed);
// delay(500);
analogWrite(motorRGND, rightMSpeed);
}
}
align(head);
stop(100);
motorPositionB=0;
}
void right(int deljay, int stoppo){
digitalWrite(motorRVOLT, 200);
digitalWrite(motorLVOLT, 0);
digitalWrite(motorRGND, 0);
digitalWrite(motorLGND, 200);
delay(deljay);
stop(stoppo);
}
void left(int deljay, int stoppo){
digitalWrite(motorLVOLT, 200);
digitalWrite(motorRVOLT, 0);
digitalWrite(motorLGND, 0);
digitalWrite(motorRGND, 200);
delay(deljay);
stop(stoppo);
}
void stop(int deljay){
analogWrite(motorLGND, 0);
analogWrite(motorRGND, 0);
analogWrite(motorLVOLT, 0);
analogWrite(motorRVOLT, 0);
delay(deljay);
}
void align(int heading){
if(heading == 1){
sensors_event_t event;
bno.getEvent(&event);
prevTime = 1;
// Serial.print("X: ");
// Serial.println(event.orientation.x, 4);
while(event.orientation.x!=0){
sensors_event_t event;
bno.getEvent(&event);
if((event.orientation.x > 0 && event.orientation.x < ang) || (event.orientation.x > 360-ang && event.orientation.x < 360)||prevTime>150){
break;
}
Serial.print("X: ");
Serial.println(event.orientation.x, 4);
if (event.orientation.x < 360 && event.orientation.x >= 180){
right(turnC*(360-event.orientation.x), 100);
}
if (event.orientation.x > 0 && event.orientation.x < 180){
left(turnC*event.orientation.x,100);
}
}
}
else if(heading == 2){
sensors_event_t event;
bno.getEvent(&event);
// Serial.print("X: ");
// Serial.println(event.orientation.x, 4);
prevTime = 1;
while(event.orientation.x!=90){
sensors_event_t event;
bno.getEvent(&event);
if (event.orientation.x < 90+ang && event.orientation.x > 90-ang||prevTime>150){
break;
}
Serial.println(prevTime++);
// Serial.print("X: ");
// Serial.println(event.orientation.x, 4);
if ((event.orientation.x < 90 && event.orientation.x > 0)){
right(turnC*(90-event.orientation.x), 100);
}
else if ((event.orientation.x < 360 && event.orientation.x > 270)){
right(turnC*(450-event.orientation.x),100);
}
else if (event.orientation.x > 90 && event.orientation.x < 180){
left(turnC*(event.orientation.x-90),100);
}
else if (event.orientation.x > 180 && event.orientation.x < 270){
left(turnC*(event.orientation.x-90),100);
}
}
}
else if(heading == 4){
sensors_event_t event;
bno.getEvent(&event);
prevTime = 1;
// Serial.print("X: ");
// Serial.println(event.orientation.x, 4);
while(event.orientation.x!=270){
sensors_event_t event;
bno.getEvent(&event);
if (event.orientation.x < 270+ang && event.orientation.x > 270-ang||prevTime>150){
rightMSpeed = 200;
break;
}
Serial.print("X: ");
Serial.println(event.orientation.x, 4);
if ((event.orientation.x < 360 && event.orientation.x > 270)){
rightMSpeed = 235;
left(turnC*(event.orientation.x-270), 100);
}
else if ((event.orientation.x < 90 && event.orientation.x > 0)){
rightMSpeed = 200;
left(turnC*25,100);
}
else if (event.orientation.x > 90 && event.orientation.x < 270){
right(turnC*(270 - event.orientation.x),100);
}
}
}
else if(heading == 3){
sensors_event_t event;
bno.getEvent(&event);
prevTime = 1;
// Serial.print("X: ");
// Serial.println(event.orientation.x, 4);
while(event.orientation.x!=180){
sensors_event_t event;
bno.getEvent(&event);
if((event.orientation.x > 180-ang && event.orientation.x < 180) || (event.orientation.x > 180 && event.orientation.x < 180+ang)||prevTime>150){
break;
}
Serial.print("X: ");
Serial.println(event.orientation.x, 4);
if (event.orientation.x < 360 && event.orientation.x >= 180){
left(turnC*(event.orientation.x-180), 100);
}
if (event.orientation.x > 0 && event.orientation.x < 180){
right(turnC*(180-event.orientation.x),100);
}
}
}
}
void updateMotorPositionA(){
if(digitalRead(4) != digitalRead(2)){
motorPositionA++;
}
else{
motorPositionA--;
}
}
void updateMotorPositionB(){
if(digitalRead(3) != digitalRead(7)){
motorPositionB++;
}
else{
motorPositionB--;
}
}
void alignfor(int head){
align(head);
forward(2,head);
}
void loop() {
// About 2.4 sec per forward
// About 1 sec per turn
// *IMPORTANT REMINDERS*
// Code should always start with delay(1000) and forward(s,1)
// Then every consecutive straight including the one immediately after forward(t,1) should be forward(f, head)
// Immediately after EVERY align the forward should be forward(t, head) and then move to the consecutives
// Include a delay of cons in between every single command except the first two
// Try NOT to go left very much
// RT Code
delay(1000);
forward(s,1);
delay(cons);
align(2);
delay(cons);
forward(t,2);
delay(cons);
align(1);
delay(cons);
forward(t,1);
delay(cons);
align(4);
delay(cons);
forward(t,4);
delay(cons);
forward(f,4);
delay(cons);
align(3);
delay(cons);
forward(t,3);
delay(cons);
align(4);
delay(cons);
forward(t,4);
delay(cons);
align(1);
delay(cons);
forward(t,1);
delay(cons);
forward(f,1);
delay(cons);
forward(f,1);
delay(cons);
align(2);
delay(cons);
forward(t,2);
delay(cons);
forward(f,2);
delay(cons);
align(3);
delay(cons);
forward(t,3);
delay(cons);
forward(f,3);
delay(cons);
align(2);
delay(cons);
forward(t,2);
delay(cons);
align(3);
delay(cons);
forward(t,3);
delay(cons);
align(4);
delay(cons);
forward(t,4);
delay(cons);
forward(f,4);
delay(cons);
forward(f,4);
delay(cons);
align(1);
delay(cons);
forward(s,1);
delay(cons);
while(1==1){
stop(100);
}
}
// lolo