-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmaze-e.ino
252 lines (226 loc) · 5.38 KB
/
maze-e.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
//Maze-E v3.1
//Define pin outputs
#define GuideL 8 //Left Guide IR
#define GuideR 11 //Right Guide IR
#define IRR 7 //Far Right IR
#define IRC 6 //Center IR
#define IRL 5 //Far Left IR
#define StartButton 1
//Left Motor pins
#define MotorL_F A3
#define MotorL_R A2
#define ENA 9 //Enabling and Speed Control
#define LeftSteps 3 //LM393 Optical Sensor
//Right Motor pins
#define MotorR_R 13
#define MotorR_F 12
#define ENB 10 //Enabling and Speed Control
#define RightSteps 2 //LM393 Optical Sensor
//constant values
#define LeftSpeed 90
#define RightSpeed 90
//variables
int l, c, r, gl, gr; //store sensor readings
int buttonState = 0;
bool isleft = false;
bool isright = false;
bool isforward = false;
bool check;
//step counter
volatile int Lcount = 0;
volatile int Rcount = 0;
void setup() {
pinMode(GuideL, INPUT);
pinMode(GuideR, INPUT);
pinMode(IRL, INPUT);
pinMode(IRR, INPUT);
pinMode(IRC, INPUT);
pinMode(MotorL_R, OUTPUT);
pinMode(MotorL_F, OUTPUT);
pinMode(MotorR_R, OUTPUT);
pinMode(MotorR_F, OUTPUT);
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(LeftSteps, INPUT_PULLUP);
pinMode(RightSteps, INPUT_PULLUP);
pinMode(StartButton, INPUT_PULLUP);
analogWrite(ENA, LeftSpeed);
analogWrite(ENB, RightSpeed);
digitalWrite(MotorR_F, LOW);
digitalWrite(MotorL_F, LOW);
attachInterrupt(digitalPinToInterrupt(LeftSteps), LISR, RISING);
attachInterrupt(digitalPinToInterrupt(RightSteps), RISR, RISING);
holdForPush(); //wait for push button press
}
void loop() {
l = digitalRead(IRL);
c = digitalRead(IRC);
r = digitalRead(IRR);
gl = digitalRead(GuideL);
gr = digitalRead(GuideR);
if((l == 0 && r == 0) && (c == 1 || c == 0 && (gl == 1 || gr == 1))) //follow path
{
check = true; //a check to make sure line following procedure was executed
if(gl == 0 && gr == 0)
{
forward();
}
else if(gl == 1 && gr == 0)
{
left();
}
else if(gr == 1 && gl == 0)
{
right();
}
}
else if(c == 0 && ((l == 1 && r == 0)||(r == 1 && l == 0 ))) //minor aligning
{
stopBot();
align();
}
else if((r == 1 || l == 1) && c == 1 && check == true) //junction
{
stopBot(); //stop bot when far left or right sensors detect a path
check = false;
//check if both left and right are available
if(digitalRead(IRL) == 1){isleft = true;}
if(digitalRead(IRR) == 1){isright = true;}
nudge(1); //nudge a bit forward to make sure both sensors are within range to detect a path
delay(500);
//recheck if both left and right are available
if(digitalRead(IRL) == 1){isleft = true;}
if(digitalRead(IRR) == 1){isright = true;}
//v3.1
if(isleft == false && isright == false)//incase line was missed due to speed
{
//use previous readings to determine available path
if(r == 1){isright == true;}
if(l == 1){isleft == true;}
}
nudge(5); //align wheels with path to prepare for left or right 90 turn
delay(200);
//check if a center path is available
if(digitalRead(IRL) == 1 || digitalRead(GuideL) == 1 || digitalRead(IRC) == 1 || digitalRead(GuideR) == 1 || digitalRead(IRR) == 1)
{
align(); //if center is available, use it to align
isforward = true;
}
Rcount = 0;
Lcount = 0;
if(isleft) //left priority
{
left90(); //turn left by 90
delay(500);
if(nopath()) //if no path is detected nudge further
{
leftNudge(3);
delay(300);
if(nopath())
{
leftNudge(3);
delay(300);
if(nopath())
{
rightNudge(15);
}
}
}
align(); //align before proceeding
check = false;
isleft = false;
isright = false;
isforward = false;
// previousMillis = millis(); //reset junction timer
return;
}
else if(isforward)
{
check = false;
isleft = false;
isright = false;
isforward = false;
// previousMillis = millis(); //reset junction timer
return;
}
else if(isright)
{
right90(); //turn right by 90
delay(500);
if(nopath()) //if no path is detected nudge further
{
rightNudge(3);
delay(300);
if(nopath())
{
rightNudge(3);
delay(30);
if(nopath())
{
leftNudge(15);
}
}
}
align(); //align before proceeding
check = false;
isleft = false;
isright = false;
isforward = false;
// previousMillis = millis();
return;
}
else
{
//nopath();
}
}
else if(c == 1 && check == false && (r == 1 || l == 1)) //major aligning
{
stopBot();
if(l == 1)
{
onlyRight();
}
else if(r == 1)
{
onlyLeft();
}
}
else if(c == 0 && gl == 0 && gr == 0 && l == 0 && r == 0) //deadend
{
stopBot();
delay(1000);
uTurn();
delay(500);
if(nopath()) //to make sure line wasn't missed
{
leftNudge(5);
}
else
{
align();
}
check = false;
}
}
void holdForPush()
{
while(buttonState == 0)
{
if(digitalRead(StartButton) == 0)
{
buttonState = 1;
}
}
}
bool nopath()
{
if(digitalRead(IRL) == 0 && digitalRead(GuideL) == 0 && digitalRead(IRC) == 0 && digitalRead(GuideR) == 0 && digitalRead(IRR) == 0)
{
return true;
}
else
{
return false;
}
}