-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdistance.ino
246 lines (212 loc) · 4.74 KB
/
distance.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
#include <math.h>
// Setup Motor A (front and rear) pins
#define PIN_R1 5
#define PIN_R2 6
#define PIN_R_enable 3
// Setup Motor B (front and rear) pins
#define PIN_L1 9
#define PIN_L2 10
#define PIN_L_enable 11
// Setup Bluetooth
#define TX 1
#define RX 0
unsigned long timer0 = 2000; //Stores the time (in millis since execution started)
unsigned long timer1 = 0; //Stores the time when the last command was received from the phone
// Define Attributes
#define NO_COMMAND 'X'
#define RESET 'C'
#define FORWARD 'F'
#define FORWARD_RIGHT 'I'
#define RIGHT 'R'
#define BACKWORD_RIGHT 'J'
#define BACKWORD 'B'
#define BACKWORD_LEFT 'H'
#define LEFT 'L'
#define FORWARD_LEFT 'G'
#define BREAK ' '
#define FOLLOWLINE 'W'
#define STOPFOLLOWING 'w'
#define STOP 'S'
#define GEAR_1 '0'
#define GEAR_2 '3'
#define GEAR_3 '5'
#define GEAR_4 '8'
#define GEAR_5 'q'
char prevCommand = NO_COMMAND;
char command = RESET;
int speed = 0;
char state = STOP;
void setup() {
// The setup code goes here and runs once only
Serial.begin(9600);
// Configure the pin modes for each drive motor
pinMode (PIN_R1, OUTPUT);
pinMode (PIN_R2, OUTPUT);
pinMode (PIN_L1, OUTPUT);
pinMode (PIN_L2, OUTPUT);
pinMode (PIN_R_enable, OUTPUT);
pinMode (PIN_L_enable, OUTPUT);
digitalWrite (PIN_L_enable, HIGH);
digitalWrite (PIN_R_enable, HIGH);
}
int number=0;String fullno="";String str;
void loop() {breakRobot(10);
while(1){if(Serial.available()){
command=Serial.read();
str=String(command);
if (isDigit(command)==0)break;
Serial.println(str);
fullno=fullno+str;
Serial.println(fullno);
}
}number=fullno.toInt();
Serial.println(number);fullno="";
while(1){forward(1000*number,100);number=0;break;}
}
void setSpeed(char gear) {
if (gear == GEAR_1) {
speed = 100;
} else if (gear == GEAR_2) {
speed = 140;
} else if (gear == GEAR_3) {
speed = 180;
} else if (gear == GEAR_4) {
speed = 220;
} else if (gear == GEAR_5) {
speed = 255;
}
}
void setState(char command) {
switch (command) {
case RESET:
breakRobot(1);
coast(0);
speed = 0;
state = STOP;
break;
case BREAK:
state = BREAK;
breakRobot(1);
break;
case STOP:
state = STOP;
coast(0);
break;
case GEAR_1:
setSpeed(GEAR_1);
setState(state);
break;
case GEAR_2:
setSpeed(GEAR_2);
setState(state);
break;
case GEAR_3:
setSpeed(GEAR_3);
setState(state);
break;
case GEAR_4:
setSpeed(GEAR_4);
setState(state);
break;
case GEAR_5:
setSpeed(GEAR_5);
setState(state);
break;
case FORWARD:
state = FORWARD;
forward(1, speed);
break;
case FORWARD_RIGHT:
state = FORWARD_RIGHT;
right(1, 0.5 * speed, speed);
break;
case RIGHT:
state = RIGHT;
right(1, speed, speed);
break;
case BACKWORD_RIGHT:
state = BACKWORD_RIGHT;
left(1, 0.5 * speed, speed);
break;
case BACKWORD:
state = BACKWORD;
backward(1, speed);
break;
case BACKWORD_LEFT:
state = BACKWORD_LEFT;
right(1, speed, 0.5 * speed);
break;
case LEFT:
state = LEFT;
left(1, speed, speed);
break;
case FORWARD_LEFT:
state = FORWARD_LEFT;
left(1, speed, 0.5 * speed);
}
prevCommand = command;
}
// Create motor functions
void motorAforward(int speed) {
analogWrite (PIN_R1, speed);
digitalWrite (PIN_R2, LOW);
}
void motorBforward(int speed) {
digitalWrite (PIN_L1, LOW);
analogWrite (PIN_L2, speed);
}
void motorAbackward(int speed) {
digitalWrite (PIN_R1, LOW);
analogWrite (PIN_R2, speed);
}
void motorBbackward(int speed) {
analogWrite (PIN_L1, speed);
digitalWrite (PIN_L2, LOW);
}
void motorAstop() {
digitalWrite (PIN_R1, HIGH);
digitalWrite (PIN_R2, HIGH);
}
void motorBstop() {
digitalWrite (PIN_L1, HIGH);
digitalWrite (PIN_L2, HIGH);
}
void motorAcoast() {
digitalWrite (PIN_R1, LOW);
digitalWrite (PIN_R2, LOW);
}
void motorBcoast() {
digitalWrite (PIN_L1, LOW);
digitalWrite (PIN_L2, LOW);
}
// Setup movement functions
void forward (int duration, int speed) {
motorAforward(speed);
motorBforward(speed);
delay (duration);
}
void backward (int duration, int speed) {
motorAbackward(speed);
motorBbackward(speed);
delay (duration);
}
void left (int duration, int speedA, int speedB) {
motorAbackward(speedA);
motorBforward(speedB);
delay (duration);
}
void right (int duration, int speedA, int speedB) {
motorAforward(speedA);
motorBbackward(speedB);
delay (duration);
}
void coast (int duration) {
motorAcoast();
motorBcoast();
delay (duration);
}
void breakRobot (int duration) {
motorAstop();
motorBstop();
delay (duration);
}