-
Notifications
You must be signed in to change notification settings - Fork 0
/
tablebot_III.ino
283 lines (236 loc) · 4.91 KB
/
tablebot_III.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
// Phase III TABLEBot Code
#include <Servo.h> // Load "Servo" library
Servo servoLeft; // Left drive servo
Servo servoRight; // Right drive servo
const int BumpLeft = 4; // Left bumper Pin 4
const int BumpRight = 5; // Right bumper Pin 5
int BumpStateLeft = 0; // Set Left Bump State value
int BumpStateRight = 0; // Set Right Bump State value
int pingPin1 = 10; // Set Ping Sensor Pin
int pingPin2 = 11; // Set Ping Sensor Pin
long raw_distance1; // Value for Raw Distance Ping Pulse
long raw_distance2; // Value for Raw Distance Ping Pulse
int val6 = 0; // Value for Left Front IR
int val7 = 0; // Value for Right Front IR
int val8 = 0; // Value for Left Rear IR
int val9 = 0; // Value for Right Rear IR
int x = 0; // Counter Variable
int y = 0; // Counter Variable
void setup() {
Serial.begin(9600); // Setup serial monitor for debug
servoLeft.attach(2); // Set left servo to pin 2
servoRight.attach(3); // Set right servo to pin 3
pinMode(BumpLeft, INPUT_PULLUP); // Set BumperLeft to input with pullup resistor
pinMode(BumpRight, INPUT_PULLUP); // Set BumperRight to input with pullup resistor
}
void loop() {
BumpStateLeft = digitalRead(BumpLeft);
// Serial.println (BumpStateLeft, DEC);
if (BumpStateLeft == 0) {
goal();
}
BumpStateRight = digitalRead(BumpRight);
// Serial.println (BumpStateRight, DEC);
if (BumpStateRight == 0) {
goal();
}
read_ping1();
if (raw_distance1 < 700) {
goal();
}
if (raw_distance1 < 5000) {
read_ping2();
}
if (raw_distance2 - 500 > raw_distance1) {
forward();
}
if (raw_distance1 > 5000) {
x = 0;
while (x < 100) {
clockwise();
x++;
}
x = 0;
while (x < 100) {
stop();
x++;
}
y++;
if (y > 10) {
x = 0;
while (x < 200) {
forward();
x++;
y = 0;
}
}
}
}
void read_ping1() {
pinMode(pingPin1, OUTPUT);
digitalWrite(pingPin1, LOW);
delayMicroseconds(2);
digitalWrite(pingPin1, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin1, LOW);
pinMode(pingPin1, INPUT);
raw_distance1 = pulseIn(pingPin1, HIGH);
Serial.print(raw_distance1);
Serial.println();
delay(10);
}
void read_ping2() {
pinMode(pingPin2, OUTPUT);
digitalWrite(pingPin2, LOW);
delayMicroseconds(2);
digitalWrite(pingPin2, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin2, LOW);
pinMode(pingPin2, INPUT);
raw_distance2 = pulseIn(pingPin2, HIGH);
Serial.print(raw_distance2);
Serial.println();
delay(10);
}
void stop() {
servoLeft.write(90);
servoRight.write(90);
delay (2);
// ir_check();
}
void left_forward() {
servoLeft.write(180);
servoRight.write(90);
delay (2);
// ir_check();
}
void left_reverse() {
servoLeft.write(0);
servoRight.write(90);
delay (2);
// ir_check();
}
void right_forward() {
servoLeft.write(90);
servoRight.write(0);
delay (2);
// ir_check();
}
void forward() {
servoLeft.write(180);
servoRight.write(0);
delay(2);
ir_check();
}
void counterclockwise() {
servoLeft.write(0);
servoRight.write(0);
delay(2);
// ir_check();
}
void right_reverse() {
servoLeft.write(90);
servoRight.write(180);
delay (2);
// ir_check();
}
void clockwise() {
servoLeft.write(180);
servoRight.write(180);
delay(2);
// ir_check();
}
void reverse() {
servoLeft.write(0);
servoRight.write(180);
delay(2);
// ir_check();
}
void ir_check() {
val6 = digitalRead(6);
// Serial.println(val6); // debug value
if (val6 == 1) {
x = 0;
while (x < 100) {
stop();
x++;
}
x = 0;
while (x < 100) {
reverse();
x++;
}
x = 0;
while (x < 200) {
clockwise();
x++;
}
}
val7 = digitalRead(7);
// Serial.println(val7); // debug value
if (val7 == 1) {
x = 0;
while (x < 100) {
stop();
x++;
}
x = 0;
while (x < 100) {
reverse();
x++;
}
x = 0;
while (x < 100) {
counterclockwise();
x++;
}
}
}
void goal() {
read_ping2();
if (raw_distance2 > 5000) {
x = 0;
while (x < 100) {
clockwise();
x++;
}
x = 0;
while (x < 100) {
stop();
x++;
}
y++;
if (y > 10) {
x = 0;
while (x < 200) {
forward();
x++;
y = 0;
}
}
}
if (raw_distance2 < 5000) {
forward();
}
if (raw_distance2 < 1500) {
dance();
}
loop();
}
void dance() {
x = 0;
while (x < 400) {
reverse();
x++;
}
x = 0;
while (x < 400) {
clockwise();
x++;
}
end();
}
void end() {
stop();
end();
}