-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect_white.ino
301 lines (245 loc) · 6.4 KB
/
detect_white.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
const int LEYE = A4;
const int REYE = A3;
const int LED_PIN = 13;
const int TPIN = 9;
const int EPIN = 8;
const int IR_WHEEL_SENSOR_PIN = 2;
unsigned long us_last_poll = 0;
unsigned long ir_last_poll = 0;
bool last_saw_left;
bool last_saw_right;
int move_right_analog = 130;
int move_left_analog = 120;
int thresholdTime = 10;
long duration;
int distance;
int changes = 0;
int revolutions = 0;
char theChar = 's';
// more sensive C less sensitive AC
// 1.5-2mms
const unsigned long us_polling_time = 500;
const unsigned long ir_polling_time = 10;
const int stopDistance = 25;
const int left_neg = 4;
const int left_pos = 5;
const int right_pos = 6;
const int right_neg = 7;
const int interval = 1000;
void move_forward();
void move_reverse();
bool mssg = false;
void set_right_positive_high();
void set_left_positive_high();
void set_left_negative_high();
void set_right_negative_high();
void set_right_positive_low();
void set_left_positive_low();
void set_left_negative_low();
void set_right_negative_low();
void move_reverse_left();
void move_reverse_right();
void move_forward_left();
void move_forward_right();
void move_forward_slowly();
void move_forward_right_analog();
void move_forward_left_analog();
void move_stop();
int get_distance();
void trigger();
void send_distance_travelled();
void setup() {
Serial.begin(9600);
pinMode( LEYE, INPUT );
pinMode( REYE, INPUT );
pinMode( TPIN, OUTPUT );
pinMode( EPIN, INPUT );
pinMode( LED_PIN, OUTPUT);
pinMode(IR_WHEEL_SENSOR_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(IR_WHEEL_SENSOR_PIN), trigger, CHANGE);
delay(1000);
Serial.print("+++");
delay(1000);
Serial.println("ATID 3305, CH C, CN");
delay(1000);
while( Serial.read() != -1 );
}
void loop(){
if( Serial.available() > 0 ){
theChar = Serial.read();
Serial.print(" Okay boss: ");
Serial.println( theChar );
if(theChar == 's'){
Serial.println(" Stopping");
}else{
Serial.println(" Going");
}
}
if( theChar == 's'){
move_stop();
return;
}
unsigned long us_timeDifference = (millis() - us_last_poll);
unsigned long ir_timeDifference = (millis() - ir_last_poll);
if(us_last_poll == 0 || us_timeDifference > us_polling_time ){
if( get_distance() < stopDistance){
if( mssg == false){
Serial.print("-Object was detected ");
Serial.print((get_distance()/100));
Serial.println("m away. Stop");
mssg = true;
}
move_stop();
return;
}else if(mssg == true){
mssg = false;
}
send_distance_travelled();
}
if(ir_last_poll == 0 || ir_timeDifference > ir_polling_time ){
bool all_clear_left = digitalRead( LEYE );
bool all_clear_right = digitalRead( REYE );
if(all_clear_right == false && all_clear_left == false){
// move_right_analog = 180;
// move_left_analog = 180;
move_forward_slowly();
last_saw_left = all_clear_left;
last_saw_right = all_clear_right;
}
if(all_clear_right == true && all_clear_left == true){
if(last_saw_right == false && last_saw_left == false){
// move_right_analog = 60;
// move_left_analog = 60;
move_forward_slowly();
// set_left_positive_low();
// move_stop();
}else if(last_saw_right == true){
// move_right_analog = 60;
move_forward_slowly();
set_left_positive_low();
}else if(last_saw_left == true){
// move_left_analog = 60;
move_forward_slowly();
set_right_positive_low();
}
}
if(all_clear_right == true && all_clear_left == false){
// move_right_analog = 60;
move_forward_slowly();
set_left_positive_low();
last_saw_left = all_clear_left;
last_saw_right = all_clear_right;
}
if(all_clear_left == true && all_clear_right == false){
// move_left_analog = 60;
move_forward_slowly();
set_right_positive_low();
last_saw_left = all_clear_left;
last_saw_right = all_clear_right;
}
}
}
void move_forward(){
set_right_negative_low();
set_left_negative_low();
set_right_positive_high();
set_left_positive_high();
}
void move_reverse(){
set_right_positive_low();
set_left_positive_low();
set_right_negative_high();
set_left_negative_high();
}
void move_stop(){
// Serial.println("Attempting to stop");
set_right_positive_low();
set_left_positive_low();
set_right_negative_low();
set_left_negative_low();
}
void move_forward_right(){
set_left_positive_low();
set_left_negative_low();
set_right_negative_low();
set_right_positive_high();
}
void move_forward_right_analog(){
analogWrite(right_pos, move_right_analog);
}
void move_forward_left_analog(){
analogWrite(left_pos, move_left_analog);
}
void move_forward_slowly(){
move_forward_right_analog();
move_forward_left_analog();
}
void move_reverse_right(){
set_left_positive_low();
set_left_negative_low();
set_right_positive_low();
set_right_negative_high();
}
void move_forward_left(){
set_right_negative_low();
set_left_negative_low();
set_right_positive_low();
set_left_positive_high();
}
void move_reverse_left(){
set_right_negative_low();
set_left_positive_low();
set_right_positive_low();
set_left_negative_high();
}
void set_right_positive_high(){
digitalWrite(right_pos, HIGH);
}
void set_left_positive_high(){
digitalWrite(left_pos, HIGH);
}
void set_left_negative_high(){
digitalWrite(left_neg, HIGH);
}
void set_right_negative_high(){
digitalWrite(right_neg, HIGH);
}
void set_right_positive_low(){
digitalWrite(right_pos, LOW);
}
void set_left_positive_low(){
digitalWrite(left_pos, LOW);
}
void set_left_negative_low(){
digitalWrite(left_neg, LOW);
}
void set_right_negative_low(){
digitalWrite(right_neg, LOW);
}
int get_distance(){
digitalWrite(TPIN, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for thresholdTime micro seconds
digitalWrite(TPIN, HIGH);
delayMicroseconds(thresholdTime);
digitalWrite(TPIN, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(EPIN, HIGH);
// Calculating the distance
distance= (duration*0.034)/2;
// Serial.print("Distance: ");
// Serial.println(distance);
return distance;
}
void send_distance_travelled(){
Serial.print(" Distance Travelled: ");
Serial.print(revolutions * 0.0635*3.14);
Serial.println("m");
}
void trigger(){
changes++;
if(changes == 10){
revolutions++;
changes = 0;
}
}