-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLine Follower
More file actions
155 lines (139 loc) · 4.78 KB
/
Copy pathLine Follower
File metadata and controls
155 lines (139 loc) · 4.78 KB
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
#define m1 4 //Right Motor MA1
#define m2 5 //Right Motor MA2
#define m3 2 //Left Motor MB1
#define m4 3 //Left Motor MB2
#define e1 9 //Right Motor Enable Pin EA
#define e2 10 //Left Motor Enable Pin EB
#define ir1 A0
#define ir2 A1
#define ir3 A2
#define ir4 A3
#define ir5 A4
void setup() {
pinMode(m1, OUTPUT);
pinMode(m2, OUTPUT);
pinMode(m3, OUTPUT);
pinMode(m4, OUTPUT);
pinMode(e1, OUTPUT);
pinMode(e2, OUTPUT);
pinMode(ir1, INPUT);
pinMode(ir2, INPUT);
pinMode(ir3, INPUT);
pinMode(ir4, INPUT);
pinMode(ir5, INPUT);
}
void loop() {
int s1 = digitalRead(ir1); //Left Most Sensor
int s2 = digitalRead(ir2); //Left Sensor
int s3 = digitalRead(ir3); //Middle Sensor
int s4 = digitalRead(ir4); //Right Sensor
int s5 = digitalRead(ir5); //Right Most Sensor
//if only middle sensor detects black line
if((s1 == 1) && (s2 == 1) && (s3 == 0) && (s4 == 1) && (s5 == 1))
{
//going forward with full speed
analogWrite(e1, 255); //you can adjust the speed of the motors from 0-255
analogWrite(e2, 255); //you can adjust the speed of the motors from 0-255
digitalWrite(m1, HIGH);
digitalWrite(m2, LOW);
digitalWrite(m3, HIGH);
digitalWrite(m4, LOW);
}
//if only left sensor detects black line
if((s1 == 1) && (s2 == 0) && (s3 == 1) && (s4 == 1) && (s5 == 1))
{
//going right with full speed
analogWrite(e1, 255); //you can adjust the speed of the motors from 0-255
analogWrite(e2, 255); //you can adjust the speed of the motors from 0-255
digitalWrite(m1, HIGH);
digitalWrite(m2, LOW);
digitalWrite(m3, LOW);
digitalWrite(m4, LOW);
}
//if only left most sensor detects black line
if((s1 == 0) && (s2 == 1) && (s3 == 1) && (s4 == 1) && (s5 == 1))
{
//going right with full speed
analogWrite(e1, 255); //you can adjust the speed of the motors from 0-255
analogWrite(e2, 255); //you can adjust the speed of the motors from 0-255
digitalWrite(m1, HIGH);
digitalWrite(m2, LOW);
digitalWrite(m3, LOW);
digitalWrite(m4, HIGH);
}
//if only right sensor detects black line
if((s1 == 1) && (s2 == 1) && (s3 == 1) && (s4 == 0) && (s5 == 1))
{
//going left with full speed
analogWrite(e1, 255); //you can adjust the speed of the motors from 0-255
analogWrite(e2, 255); //you can adjust the speed of the motors from 0-255
digitalWrite(m1, LOW);
digitalWrite(m2, LOW);
digitalWrite(m3, HIGH);
digitalWrite(m4, LOW);
}
//if only right most sensor detects black line
if((s1 == 1) && (s2 == 1) && (s3 == 1) && (s4 == 1) && (s5 == 0))
{
//going left with full speed
analogWrite(e1, 255); //you can adjust the speed of the motors from 0-255
analogWrite(e2, 255); //you can adjust the speed of the motors from 0-255
digitalWrite(m1, LOW);
digitalWrite(m2, HIGH);
digitalWrite(m3, HIGH);
digitalWrite(m4, LOW);
}
//if middle and right sensor detects black line
if((s1 == 1) && (s2 == 1) && (s3 == 0) && (s4 == 0) && (s5 == 1))
{
//going left with full speed
analogWrite(e1, 255); //you can adjust the speed of the motors from 0-255
analogWrite(e2, 255); //you can adjust the speed of the motors from 0-255
digitalWrite(m1, LOW);
digitalWrite(m2, LOW);
digitalWrite(m3, HIGH);
digitalWrite(m4, LOW);
}
//if middle and left sensor detects black line
if((s1 == 1) && (s2 == 0) && (s3 == 0) && (s4 == 1) && (s5 == 1))
{
//going right with full speed
analogWrite(e1, 255); //you can adjust the speed of the motors from 0-255
analogWrite(e2, 255); //you can adjust the speed of the motors from 0-255
digitalWrite(m1, HIGH);
digitalWrite(m2, LOW);
digitalWrite(m3, LOW);
digitalWrite(m4, LOW);
}
//if middle, left and left most sensor detects black line
if((s1 == 0) && (s2 == 0) && (s3 == 0) && (s4 == 1) && (s5 == 1))
{
//going right with full speed
analogWrite(e1, 255); //you can adjust the speed of the motors from 0-255
analogWrite(e2, 255); //you can adjust the speed of the motors from 0-255
digitalWrite(m1, HIGH);
digitalWrite(m2, LOW);
digitalWrite(m3, LOW);
digitalWrite(m4, LOW);
}
//if middle, right and right most sensor detects black line
if((s1 == 1) && (s2 == 1) && (s3 == 0) && (s4 == 0) && (s5 == 0))
{
//going left with full speed
analogWrite(e1, 255); //you can adjust the speed of the motors from 0-255
analogWrite(e2, 255); //you can adjust the speed of the motors from 0-255
digitalWrite(m1, LOW);
digitalWrite(m2, LOW);
digitalWrite(m3, HIGH);
digitalWrite(m4, LOW);
}
//if all sensors are on a black line
if((s1 == 0) && (s2 == 0) && (s3 == 0) && (s4 == 0) && (s5 == 0))
{
//stop
digitalWrite(m1, LOW);
digitalWrite(m2, LOW);
digitalWrite(m3, LOW);
digitalWrite(m4, LOW);
}
}