-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLineTrack2.ino
174 lines (154 loc) · 4.36 KB
/
LineTrack2.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
/*
Robot Car Motor Test
*/
#define LED LED_BUILTIN
// IR proximity sensors (active low)
#define LEFT 4
#define RIGHT 2
#define FOLLOW_BLACK 0
#define FOLLOW_WHITE 1
#define FOLLOW_SAME 2
#define FOLLOW_NONE 3
#define FOLLOW_MODE FOLLOW_BLACK
#define LEFT_TRACK 13
#define RIGHT_TRACK 12
#define MAX_RETRY 4 // course corrections till we give up
// L298N motor controller
#define ENA 10 // left motor
#define IN1 9
#define IN2 8
#define ENB 5 // right motor
#define IN3 7
#define IN4 6
#define FASTEST 255
#define FAST 254
#define SLOW (FAST/2)
#define SLOWER (FAST/3)
#define STOP 0
#define DEFAULT_SPEED SLOWER
#define DIR_STOP 0
#define DIR_FORWARD 1
#define DIR_LEFT 2
#define DIR_RIGHT 3
#define DIR_BACK 4
#define DIR_CW 5
#define DIR_CCW 6
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED, OUTPUT);
pinMode(LEFT, INPUT);
pinMode(RIGHT, INPUT);
pinMode(LEFT_TRACK, INPUT);
pinMode(RIGHT_TRACK, INPUT);
pinMode(ENA, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
}
void runMotors(int dir, int spd) {
switch (dir) {
case DIR_BACK:
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
break;
case DIR_FORWARD:
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
break;
case DIR_CW:
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
break;
case DIR_RIGHT:
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
break;
case DIR_CCW:
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
break;
case DIR_LEFT:
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
break;
case DIR_STOP:
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
break;
}
if (spd == FASTEST) {
digitalWrite(ENA, HIGH);
digitalWrite(ENB, HIGH);
}
else {
analogWrite(ENB, spd);
analogWrite(ENA, spd);
}
}
// the loop function runs over and over again forever
void loop() {
static int last_dir = DIR_STOP;
static int retry = 0;
int dir;
// proximity sensors are LOW when triggered
bool right = !digitalRead(RIGHT);
bool left = !digitalRead(LEFT);
bool left_track = !digitalRead(LEFT_TRACK);
bool right_track = !digitalRead(RIGHT_TRACK);
if (left && right) dir = DIR_BACK; // both sensors triggered
else if (left) dir = DIR_RIGHT;
else if (right) dir = DIR_LEFT;
else {
// nothing is blocking the front, check the line
switch (FOLLOW_MODE) {
case FOLLOW_BLACK:
if (left_track == right_track) {
if (!left_track) dir = DIR_FORWARD; // neither triggered == on course
else dir = DIR_STOP; // both triggered, we're way off track
}
else if (left_track) dir = DIR_RIGHT;
else dir = DIR_LEFT;
break;
case FOLLOW_WHITE:
if (left_track == right_track) {
if (left_track) dir = DIR_FORWARD; // both triggered == on course
else dir = DIR_STOP; // neither triggered, we're way off track
}
else if (left_track) dir = DIR_LEFT;
else dir = DIR_RIGHT;
break;
case FOLLOW_SAME:
if (left_track == right_track) dir = DIR_FORWARD; // same == on course
else if (left_track) dir = DIR_RIGHT; // TODO: random left or right move?
else dir = DIR_LEFT;
break;
case FOLLOW_NONE:
default:
dir = DIR_STOP;
}
}
if (dir == DIR_STOP) {
digitalWrite(LED, HIGH); // signal that we're lost
dir = last_dir; // let's try the previous maneuver one more time
}
else digitalWrite(LED, LOW);
runMotors(dir, DEFAULT_SPEED);
last_dir = dir; // remember our last maneuver for next time in case we get really lost
}