-
Notifications
You must be signed in to change notification settings - Fork 1
/
bluebot.ino
293 lines (265 loc) · 5.02 KB
/
bluebot.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
//BlueBot V1.0
//2017.05.03
//Clifton C. Craig
#include <Servo.h> //servo library
Servo myservo; // create servo object to control servo
char getstr;
int AUTONOMOUS = 1;
int MANUAL = 0;
int state = 0;
int Echo = A4;
int Trig = A5;
//Right wheel
int ENA = 11;
int in1 = 9;
int in2 = 8;
//Left wheel
int ENB = 5;
int in3 = 7;
int in4 = 6;
int ABS = 150;
int rightDistance = 0,leftDistance = 0,middleDistance = 0;
int THRESHOLD = 30;
void _mForward()
{
analogWrite(ENA,ABS);
analogWrite(ENB,ABS);
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
digitalWrite(in3,LOW);
digitalWrite(in4,HIGH);
Serial.println("go forward!");
}
void _mBack()
{
analogWrite(ENA,ABS);
analogWrite(ENB,ABS);
digitalWrite(in1,HIGH);
digitalWrite(in2,LOW);
digitalWrite(in3,HIGH);
digitalWrite(in4,LOW);
Serial.println("go back!");
}
void _mleft()
{
analogWrite(ENA,ABS);
analogWrite(ENB,ABS);
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
digitalWrite(in3,HIGH);
digitalWrite(in4,LOW);
Serial.println("go left!");
}
void _mright()
{
analogWrite(ENA,ABS);
analogWrite(ENB,ABS);
digitalWrite(in1,HIGH);
digitalWrite(in2,LOW);
digitalWrite(in3,LOW);
digitalWrite(in4,HIGH);
Serial.println("go right!");
}
void _mStop()
{
digitalWrite(ENA,LOW);
digitalWrite(ENB,LOW);
Serial.println("Stop!");
}
/*Ultrasonic distance measurement Sub function*/
int Distance_test()
{
delay(90);
digitalWrite(Trig, LOW);
delayMicroseconds(2);
digitalWrite(Trig, HIGH);
delayMicroseconds(30);
digitalWrite(Trig, LOW);
float Fdistance = pulseIn(Echo, HIGH);
Fdistance= Fdistance/58;
return (int)Fdistance;
}
void setup()
{
myservo.attach(3);// attach servo on pin 3 to servo object
Serial.begin(9600);
pinMode(Echo, INPUT);
pinMode(Trig, OUTPUT);
pinMode(in1,OUTPUT);
pinMode(in2,OUTPUT);
pinMode(in3,OUTPUT);
pinMode(in4,OUTPUT);
pinMode(ENA,OUTPUT);
pinMode(ENB,OUTPUT);
_mStop();
lookAhead();
#ifdef debug
debug();
#endif
}
int lookNScan(int pos)
{
myservo.write(pos);//setservo position according to scaled value
delay(500);
return Distance_test();
}
void lookAhead()
{
//setservo position according to scaled value
middleDistance = lookNScan(90);
#ifdef send
Serial.print("middleDistance=");
Serial.println(middleDistance);
#endif
}
int lookOffRight()
{
int offRight = lookNScan(65);
#ifdef send
Serial.print("offRight=");
Serial.println(offRight);
#endif
return offRight;
}
int lookOffLeft()
{
int offLeft = lookNScan(115);
#ifdef send
Serial.print("offLeft=");
Serial.println(offLeft);
#endif
return offLeft;
}
void loop()
{
getstr=Serial.read();
if(getstr=='A') {
stateChange();
} else if(state == AUTONOMOUS) {
autonomous();
} else if(state == MANUAL) {
processCmd();
}
}
void processCmd()
{
if(getstr=='f')
{
_mForward();
}
else if(getstr=='b')
{
_mBack();
delay(200);
}
else if(getstr=='l')
{
_mleft();
delay(200);
}
else if(getstr=='r')
{
_mright();
delay(200);
}
else if(getstr=='s')
{
_mStop();
}
}
void stateChange()
{
if(state == MANUAL)
state = AUTONOMOUS;
else if(state == AUTONOMOUS)
state = MANUAL;
}
void autonomous()
{
lookAhead();
int r = lookOffRight();
int l = lookOffLeft();
Serial.print("offRight=");
Serial.print(r);
Serial.print(" offLeft=");
Serial.println(l);
if(middleDistance<=THRESHOLD || l<=THRESHOLD || r<=THRESHOLD)
{
handleCollision();
}
else
_mForward();
}
void handleCollision()
{
_mStop();
rightDistance = lookNScan(5);
delay(1000);
#ifdef send
Serial.print("rightDistance=");
Serial.println(rightDistance);
#endif
leftDistance = lookNScan(180);
delay(1000);
#ifdef send
Serial.print("leftDistance=");
Serial.println(leftDistance);
#endif
lookAhead();
delay(500);
if(rightDistance>leftDistance)
{
_mBack();
delay(500);
_mright();
delay(500);
}
else if(rightDistance<leftDistance)
{
_mBack();
delay(500);
_mleft();
delay(500);
}
else if((rightDistance<=THRESHOLD)||(leftDistance<=THRESHOLD))
{
_mBack();
delay(1500);
}
else
{
_mForward();
}
}
void debug()
{
Serial.print("Debugging! Enter l, r, f, b, or x to exit debug.");
int resume = 1;
while(resume == 1) {
int last = ' ';
if(Serial.available() > 0)
last = Serial.read();
switch(last)
{
case 'l':
_mleft();
break;
case 'r':
_mright();
break;
case 'f':
_mForward();
break;
case 'b':
_mBack();
break;
case 'x':
Serial.print("Done Debugging!");
return;
break;
}
delay(500);
_mStop();
last = ' ';
}
}