-
Notifications
You must be signed in to change notification settings - Fork 0
/
BUTTONSANDLINE.ino
365 lines (326 loc) · 10.2 KB
/
BUTTONSANDLINE.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
//Code based on: https://www.hackster.io/jason_rubadue/ti-rslk-build-workshop-instructions-ff82b7#code
//==================================================================
//Below shows how the line sensor pins are mapped to the MSP-EXP432P401R
#define LS_PIN_IR 61 // <- Energia Pin # Launchpad Pin -> P5.3
#define LS_PIN_1 65 // <- Energia Pin # Launchpad Pin -> P7.0
#define LS_PIN_2 48 // <- Energia Pin # Launchpad Pin -> P7.1
#define LS_PIN_3 64 // <- Energia Pin # Launchpad Pin -> P7.2
#define LS_PIN_4 47 // <- Energia Pin # Launchpad Pin -> P7.3
#define LS_PIN_5 52 // <- Energia Pin # Launchpad Pin -> P7.4
#define LS_PIN_6 68 // <- Energia Pin # Launchpad Pin -> P7.5
#define LS_PIN_7 53 // <- Energia Pin # Launchpad Pin -> P7.6
#define LS_PIN_8 69 // <- Energia Pin # Launchpad Pin -> P7.7
//Below shows Motor Driver pins
#define MOTOR_L_SLP_PIN 31 // <- Energia Pin # Launchpad Pin -> P3.7
#define MOTOR_L_DIR_PIN 29 // <- Energia Pin # Launchpad Pin -> P5.4
#define MOTOR_L_PWM_PIN 40 // <- Energia Pin # Launchpad Pin -> P2.7
#define MOTOR_R_SLP_PIN 11 // <- Energia Pin # Launchpad Pin -> P3.6
#define MOTOR_R_DIR_PIN 30 // <- Energia Pin # Launchpad Pin -> P5.5
#define MOTOR_R_PWM_PIN 39 // <- Energia Pin # Launchpad Pin -> P2.6
const int bump_sw_pin1 = 24; // P4.0
const int bump_sw_pin2 = 25; // P4.2
const int bump_sw_pin3 = 6; // P4.3
const int bump_sw_pin4 = 27; // P4.5
const int bump_sw_pin5 = 8; // P4.6
const int bump_sw_pin6 = 28; // P4.7
const int BPUSH1 = 34;
const int BPUSH2 = 19;
const int BPUSH3 = 18;
const int BPUSH4 = 17;
int rangeL;
int rangeR;
const int RLED = 77;
const int buzz = 35;
void setup() {
//Initialize Line Sensors:
pinMode(BPUSH1, INPUT_PULLUP); //Right button on LaunchPad
//setup() From Romi_Motor_Power.cpp
//this setup() should be transferred to Main Example Code
//Initialize Motors:
//setup()
pinMode(bump_sw_pin1, INPUT_PULLUP);
pinMode(bump_sw_pin2, INPUT_PULLUP);
pinMode(bump_sw_pin3, INPUT_PULLUP);
pinMode(bump_sw_pin4, INPUT_PULLUP);
pinMode(bump_sw_pin5, INPUT_PULLUP);
pinMode(bump_sw_pin6, INPUT_PULLUP);
pinMode(MOTOR_L_SLP_PIN, OUTPUT);
pinMode(MOTOR_L_DIR_PIN, OUTPUT);
pinMode(MOTOR_L_PWM_PIN, OUTPUT);
pinMode(MOTOR_R_SLP_PIN, OUTPUT);
pinMode(MOTOR_R_DIR_PIN, OUTPUT);
pinMode(MOTOR_R_PWM_PIN, OUTPUT);
//pinMode(DIR_L, OUTPUT);
//pinMode(DIR_R, OUTPUT);
analogWrite(MOTOR_L_PWM_PIN, 0); //speed = 0
analogWrite(MOTOR_R_PWM_PIN, 0); //speed = 0
digitalWrite(MOTOR_L_SLP_PIN, LOW); //put motor to sleep
digitalWrite(MOTOR_R_SLP_PIN, LOW); //put motor to sleep
digitalWrite(buzz, LOW);
//Initialize Line Following Sensor:
pinMode(LS_PIN_IR, OUTPUT);
pinMode(LS_PIN_1, OUTPUT);
pinMode(LS_PIN_2, OUTPUT);
pinMode(LS_PIN_3, OUTPUT);
pinMode(LS_PIN_4, OUTPUT);
pinMode(LS_PIN_5, OUTPUT);
pinMode(LS_PIN_6, OUTPUT);
pinMode(LS_PIN_7, OUTPUT);
pinMode(LS_PIN_8, OUTPUT);
pinMode (BPUSH1, INPUT);
pinMode (BPUSH2, INPUT);
pinMode (BPUSH3, INPUT);
pinMode (BPUSH4, INPUT);
pinMode (buzz, OUTPUT);
Serial.begin(9600);
Serial.println("starting...");
}
void loop() {
//=====================================================================
//motor pin variables
uint8_t slp_pin_L = MOTOR_L_SLP_PIN;
uint8_t dir_pin_L = MOTOR_L_DIR_PIN;
uint8_t pwm_pin_L = MOTOR_L_PWM_PIN;
uint8_t motor_dir_L = 0;
uint8_t slp_pin_R = MOTOR_R_SLP_PIN;
uint8_t dir_pin_R = MOTOR_R_DIR_PIN;
uint8_t pwm_pin_R = MOTOR_R_PWM_PIN;
uint8_t motor_dir_R = 0;
//line following sensor variables
digitalWrite(LS_PIN_IR, HIGH);
digitalWrite(LS_PIN_1, HIGH);
digitalWrite(LS_PIN_2, HIGH);
digitalWrite(LS_PIN_3, HIGH);
digitalWrite(LS_PIN_4, HIGH);
digitalWrite(LS_PIN_5, HIGH);
digitalWrite(LS_PIN_6, HIGH);
digitalWrite(LS_PIN_7, HIGH);
digitalWrite(LS_PIN_8, HIGH);
//wait until button is pressed to start robot motors
//while(digitalRead(BPUSH1));
//NEWWWW
//enable motors
//digitalWrite(slp_pin_L, HIGH);
//digitalWrite(slp_pin_R, HIGH);
//NEWWW
//set motors to go forward
//digitalWrite(dir_pin_L, LOW);
//digitalWrite(dir_pin_R, LOW);
//test code for motors
/*for(int i=20; i<255; i++){
analogWrite(pwm_pin_L, i); //ramp motors from 0 to full speed
analogWrite(pwm_pin_R, i); //ramp motors from 0 to full speed
delay(50);
}*/
//======================================================================
//THIS AREA IS JUST FOR TESTING THE LINE SENSOR SENSITIVITY AND LIGHTING
//======================================================================
/*
while(true)
{
Serial.println("Testing time for line following sensor sensitivity:");
for(int i=0; i<100; i+=1){ //set how much time to test for
digitalWrite(LS_PIN_IR, LOW);
pinMode(LS_PIN_1, INPUT);
pinMode(LS_PIN_2, INPUT);
pinMode(LS_PIN_3, INPUT);
pinMode(LS_PIN_4, INPUT);
pinMode(LS_PIN_5, INPUT);
pinMode(LS_PIN_6, INPUT);
pinMode(LS_PIN_7, INPUT);
pinMode(LS_PIN_8, INPUT);
delay(i);
pin1 = digitalRead(LS_PIN_1);
pin2 = digitalRead(LS_PIN_2);
pin3 = digitalRead(LS_PIN_3);
pin4 = digitalRead(LS_PIN_4);
pin5 = digitalRead(LS_PIN_5);
pin6 = digitalRead(LS_PIN_6);
pin7 = digitalRead(LS_PIN_7);
pin8 = digitalRead(LS_PIN_8);
delay(10);
Serial.print("Time = ");
Serial.print(i);
Serial.println("ms");
Serial.print(pin8);
Serial.print(pin7);
Serial.print(pin6);
Serial.print(pin5);
Serial.print(pin4);
Serial.print(pin3);
Serial.print(pin2);
Serial.println(pin1);
//CHARGE BACK UP AND GET READY TO SENSE AGAIN
pinMode(LS_PIN_1, OUTPUT);
pinMode(LS_PIN_2, OUTPUT);
pinMode(LS_PIN_3, OUTPUT);
pinMode(LS_PIN_4, OUTPUT);
pinMode(LS_PIN_5, OUTPUT);
pinMode(LS_PIN_6, OUTPUT);
pinMode(LS_PIN_7, OUTPUT);
pinMode(LS_PIN_8, OUTPUT);
digitalWrite(LS_PIN_IR, HIGH);
digitalWrite(LS_PIN_1, HIGH);
digitalWrite(LS_PIN_2, HIGH);
digitalWrite(LS_PIN_3, HIGH);
digitalWrite(LS_PIN_4, HIGH);
digitalWrite(LS_PIN_5, HIGH);
digitalWrite(LS_PIN_6, HIGH);
digitalWrite(LS_PIN_7, HIGH);
digitalWrite(LS_PIN_8, HIGH);
delay(100);
}
}
*/
//======================================================================
//THIS AREA IS FOR LINE FOLLOWING APPLICATION
//======================================================================
int time = 10; //ENTER VALUE HERE FROM YOUR SENSITIVITY TESTS
Serial.println("Entering line following mode:");
if(digitalRead(BPUSH1)==HIGH){
digitalWrite(slp_pin_L, HIGH);
digitalWrite(slp_pin_R, HIGH);
digitalWrite(dir_pin_L, LOW);
digitalWrite(dir_pin_R, LOW);
bool pin1 = 1;
bool pin2 = 1;
bool pin3 = 1;
bool pin4 = 1;
bool pin5 = 1;
bool pin6 = 1;
bool pin7 = 1;
bool pin8 = 1;
int steering = 128;
digitalWrite(LS_PIN_IR, LOW); //turn off LED
pinMode(LS_PIN_1, INPUT); //turn on sensors
pinMode(LS_PIN_2, INPUT);
pinMode(LS_PIN_3, INPUT);
pinMode(LS_PIN_4, INPUT);
pinMode(LS_PIN_5, INPUT);
pinMode(LS_PIN_6, INPUT);
pinMode(LS_PIN_7, INPUT);
pinMode(LS_PIN_8, INPUT);
delay(time); //USE YOUR SENSITIVITY TEST TO FIGURE OUT THIS VALUE
pin1 = digitalRead(LS_PIN_1);
pin2 = digitalRead(LS_PIN_2);
pin3 = digitalRead(LS_PIN_3);
pin4 = digitalRead(LS_PIN_4);
pin5 = digitalRead(LS_PIN_5);
pin6 = digitalRead(LS_PIN_6);
pin7 = digitalRead(LS_PIN_7);
pin8 = digitalRead(LS_PIN_8);
if (pin1) steering += 64;
if (pin2) steering += 32;
if (pin3) steering += 16;
if (pin4) steering += 8;
if (pin5) steering -= 8;
if (pin6) steering -= 16;
if (pin7) steering -= 32;
if (pin8) steering -= 64;
//CHARGE BACK UP AND GET READY TO SENSE AGAIN
pinMode(LS_PIN_1, OUTPUT);
pinMode(LS_PIN_2, OUTPUT);
pinMode(LS_PIN_3, OUTPUT);
pinMode(LS_PIN_4, OUTPUT);
pinMode(LS_PIN_5, OUTPUT);
pinMode(LS_PIN_6, OUTPUT);
pinMode(LS_PIN_7, OUTPUT);
pinMode(LS_PIN_8, OUTPUT);
digitalWrite(LS_PIN_IR, HIGH);
digitalWrite(LS_PIN_1, HIGH);
digitalWrite(LS_PIN_2, HIGH);
digitalWrite(LS_PIN_3, HIGH);
digitalWrite(LS_PIN_4, HIGH);
digitalWrite(LS_PIN_5, HIGH);
digitalWrite(LS_PIN_6, HIGH);
digitalWrite(LS_PIN_7, HIGH);
digitalWrite(LS_PIN_8, HIGH);
//Serial.println(steering);
if (steering > 205) analogWrite(pwm_pin_L, 60);
else if (steering > 175) analogWrite(pwm_pin_L, 40);
else if (steering > 150) analogWrite(pwm_pin_L, 20);
else analogWrite(pwm_pin_L, 0);
if (steering < 50) analogWrite(pwm_pin_R, 60); //ramp motors from 0 to full speed
else if (steering < 80) analogWrite(pwm_pin_R, 40);
else if (steering < 105) analogWrite(pwm_pin_R, 20);
else analogWrite(pwm_pin_R, 0);
if (steering >= 106 && steering <= 149){
analogWrite(pwm_pin_L, 40);
analogWrite(pwm_pin_R, 40);
}
} //END OF WHILE() LOOP
if (digitalRead(BPUSH2) == HIGH)
{
right();
speedLeft(15);
speedRight(0);
}
if (digitalRead(BPUSH3) == HIGH)
{
forward();
speedLeft(30);
speedRight(30);
}
if (digitalRead(BPUSH4) == HIGH)
{
backward();
speedLeft(30);
speedRight(30);
}
if ((digitalRead(BPUSH1) == LOW) && (digitalRead(BPUSH2) == LOW) && (digitalRead(BPUSH3) == LOW) && (digitalRead(BPUSH4) == LOW))
{
sleepmode();
}
if ((digitalRead(bump_sw_pin1) == 0) || (digitalRead(bump_sw_pin2) == 0) || (digitalRead(bump_sw_pin3) == 0) || (digitalRead(bump_sw_pin4) == 0) || (digitalRead(bump_sw_pin5) == 0) || (digitalRead(bump_sw_pin6) == 0))
{
digitalWrite (buzz, HIGH);
}
if ((digitalRead(bump_sw_pin1) == 1) && (digitalRead(bump_sw_pin2) == 1) && (digitalRead(bump_sw_pin3) == 1) && (digitalRead(bump_sw_pin4) == 1) && (digitalRead(bump_sw_pin5) == 1) && (digitalRead(bump_sw_pin6) == 1))
{
digitalWrite (buzz, LOW);
}
}
//END OF LOOP()
void sleepmode()
{
digitalWrite(MOTOR_L_SLP_PIN, LOW);
digitalWrite(MOTOR_R_SLP_PIN, LOW);
}
void forward()
{
digitalWrite(MOTOR_L_SLP_PIN, HIGH);
digitalWrite(MOTOR_R_SLP_PIN, HIGH);
digitalWrite(MOTOR_L_DIR_PIN, LOW);
digitalWrite(MOTOR_R_DIR_PIN, LOW);
}
void backward()
{
digitalWrite(MOTOR_L_SLP_PIN, HIGH);
digitalWrite(MOTOR_R_SLP_PIN, HIGH);
digitalWrite(MOTOR_L_DIR_PIN, HIGH);
digitalWrite(MOTOR_R_DIR_PIN, HIGH);
}
void right()
{
digitalWrite(MOTOR_L_SLP_PIN, HIGH);
digitalWrite(MOTOR_R_SLP_PIN, HIGH);
digitalWrite(MOTOR_L_DIR_PIN, LOW);
digitalWrite(MOTOR_R_DIR_PIN, HIGH);
}
void left()
{
digitalWrite(MOTOR_L_SLP_PIN, HIGH);
digitalWrite(MOTOR_R_SLP_PIN, HIGH);
digitalWrite(MOTOR_L_DIR_PIN, HIGH);
digitalWrite(MOTOR_R_DIR_PIN, LOW);
}
void speedLeft(int speedL)
{
rangeL = map (speedL, 0, 100, 0, 255);
analogWrite(MOTOR_L_PWM_PIN, rangeL);
}
void speedRight(int speedR)
{
rangeR = map (speedR, 0, 100, 0, 255);
analogWrite(MOTOR_R_PWM_PIN, rangeR);
}