-
Notifications
You must be signed in to change notification settings - Fork 0
/
S2E7_ultrasonic_drive_to_wall_upgrade.ino
249 lines (163 loc) · 4.01 KB
/
S2E7_ultrasonic_drive_to_wall_upgrade.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
/*
Challenges:
1) create ultrasonic function that returns distance in cm
2) make sure that function works
3) create moveToWall function
4) when you stop, back up and turn around
5) once you see a wall, stop, back up and turn around.. and keep going looking for walls
*/
int motb_pin1 = 3;
int motb_pin2 = 11;
int mota_pin1 = 9;
int mota_pin2 = 10;
int button_pin = 2;
int trig_pin = 4;
int echo_pin = 5;
void setup() {
//-Control Motor B
pinMode(motb_pin1,OUTPUT);
pinMode(motb_pin2,OUTPUT);
//-Control Motor A
pinMode(mota_pin1,OUTPUT);
pinMode(mota_pin2,OUTPUT);
//- button
pinMode(button_pin,INPUT_PULLUP);
//-ultrasonic pin
pinMode(trig_pin,OUTPUT);
pinMode(echo_pin,INPUT);
Serial.begin(9600);
}
//-sends sound out and receives sound
//-returns the distance in centimeters
int ultrasonic() {
long time;
float distance;
//-trigger a sound
// send out trigger signal
digitalWrite(trig_pin, LOW);
delayMicroseconds(2);
digitalWrite(trig_pin, HIGH);
delayMicroseconds(20);
digitalWrite(trig_pin, LOW);
//- a sound has gone out!!
//- wait for a sound to come back
time = pulseIn(echo_pin, HIGH);
//- calculate the distance in centimeters
distance = 0.01715 * time;
return distance;
}
//- turn 90 degrees
void turnRight() {
//- motor b is stopped
analogWrite(motb_pin1,0);
analogWrite(motb_pin2,0);
//- motor a moves forward
analogWrite(mota_pin1,255);
analogWrite(mota_pin2,0);
delay(600);
//- stop motor a
analogWrite(mota_pin1,0);
analogWrite(mota_pin2,0);
}
void tankTurn(int speeda, int speedb) {
//- motor a moves ... backwards
analogWrite(mota_pin1,0);
analogWrite(mota_pin2,speeda);
//- motor b moves forward
analogWrite(motb_pin1,0);
analogWrite(motb_pin2,speedb);
delay(1650);
//- stop both motors
stop();
}
void turnAround(int speeda, int speedb) {
//- motor a is stopped
analogWrite(mota_pin1,0);
analogWrite(mota_pin2,0);
//- motor b moves forward
analogWrite(motb_pin1,0);
analogWrite(motb_pin2,speedb);
delay(1650);
//- stop motor b
analogWrite(motb_pin1,0);
analogWrite(motb_pin2,0);
}
void turnLeft() {
//- motor a is stopped
analogWrite(mota_pin1,0);
analogWrite(mota_pin2,0);
//- motor b moves forward
analogWrite(motb_pin1,0);
analogWrite(motb_pin2,255);
delay(800);
//- stop motor b
analogWrite(motb_pin1,0);
analogWrite(motb_pin2,0);
}
void stop() {
//- motor a
analogWrite(mota_pin1,255);
analogWrite(mota_pin2,255);
//- motor b
analogWrite(motb_pin1,255);
analogWrite(motb_pin2,255);
}
void moveToWall(int speeda, int speedb) {
//- move forward!
//- motor a
analogWrite(mota_pin1,speeda);
analogWrite(mota_pin2,0);
//- motor b
analogWrite(motb_pin1,0);
analogWrite(motb_pin2,speedb);
//-stop when you hit a wall!!
int distance = ultrasonic();
while (distance > 5) {
//-do nothing except check distance
distance = ultrasonic();
}
//-stop!!!
stop();
}
void moveForward(int speeda, int speedb, int inches) {
int myDelay;
//- motor a
analogWrite(mota_pin1,speeda);
analogWrite(mota_pin2,0);
//- motor b
analogWrite(motb_pin1,0);
analogWrite(motb_pin2,speedb);
//- move forward the distance in inches
myDelay = inches*125;
delay(myDelay);
//- stop
stop();
}
void moveBackward(int speeda, int speedb) {
//- motor a
analogWrite(mota_pin1,0);
analogWrite(mota_pin2,speeda);
//- motor b
analogWrite(motb_pin1,speedb);
analogWrite(motb_pin2,0);
}
void loop() {
Serial.println(ultrasonic());
delay(100);
if (digitalRead(button_pin)==LOW) {
//- move to the wall first
moveToWall(120,120);
delay(1000);
//- back up a little
moveBackward(120,120);
delay(400);
//- stop
stop();
delay(1000);
//- turn around
tankTurn(120,120);
}
else {
stop();
}
}