forked from Suvink/linefollower
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathline_follow.ino
More file actions
170 lines (125 loc) · 2.9 KB
/
line_follow.ino
File metadata and controls
170 lines (125 loc) · 2.9 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
Line following robot for 3 sensors
work for both digital and analog inputs.
The code is written to follow a black line on a white background.
works best with L298D Motor Driver
Author - suvin nimnaka
www.suvink9.wordpress.com
*/
int s1_in = 0;//your sensor pin;
int s2_in = 0;//your sensor pin;
int s3_in = 0;//your sensor pin;
int rme = 00; //enable pin for right motor
int lme = 00; //enable pin for left motor
int rmf = 0; //Right motor foreward pin
int rmr = 0; //Right motor reverse pin
int lmf = 0; //Left motor foreward pin
int lmr = 0; //Left motor reverse pin
//comment below 7 lines if you are using digital inputs for infrared sensors.
int s1_val;
int s2_val;
int s3_val;
int s1;
int s2;
int s3
void setup() {
pinMode (s1_in, INPUT);
pinMode (s2_in, INPUT);
pinMode (s3_in, INPUT);
pinMode(rme,OUTPUT);
pinMode(lme,OUTPUT);
pinMode(lmf,OUTPUT);
pinMode(lmr,OUTPUT);
pinMode(rmf,OUTPUT);
pinMode(rmr,OUTPUT);
}
void loop() {
analogreadings(); //Comment this if using digital inputs for sensors
linefollow();
}
void analogreadings()
s1_val = analogRead(s1_pin);
s2_val = analogRead(s2_pin);
s2_val = analogRead(s2_pin);
if (s1_val<640){
s1 == 1;
}
else {
s1 = 0;
}
if (s2_val<640){
s2 == 1;
}
else {
s2 = 0;
}
if (s3_val<640){
s3 == 1;
}
else {
s3 = 0;
}
}
void linefollow(){
if(s1== 0 && s2 == 0 && s3 == 0){
movefwd();
}
else if(s1== 0 && s2 == 0 && s3 == 1){
turnright();
}
else if(s1== 0 && s2 == 1 && s3 == 0){
movefwd();
}
else if(s1== 0 && s2 == 1 && s3 == 1){
turnright();
}
else if(s1== 1 && s2 == 0 && s3 == 0){
turnleft();
}
else if(s1== 1 && s2 == 0 && s3 == 1){
movefwd();
}
else if(s1== 1 && s2 == 1 && s3 == 0){
turnleft();
}
else if(s1== 1 && s2 == 1 && s3 == 1){
movestop();
}
}
void movefwd(){
digitalWrite(rme, HIGH);
digitalWrite(lme, HIGH);
digitalWrite(rmf,HIGH);
digitalWrite(rmr,LOW);
digitalWrite(lmf,HIGH);
digitalWrite(lmr,LOW);
}
void turnleft(){
digitalWrite(rme, HIGH);
digitalWrite(lme, LOW);
digitalWrite(rmf,HIGH);
digitalWrite(rmr,LOW);
}
void turnright(){
digitalWrite(lme, HIGH);
digitalWrite(rme, LOW);
digitalWrite(lmf,HIGH);
digitalWrite(lmr,LOW);
}
void reverse(){
digitalWrite(rme, HIGH);
digitalWrite(lme, HIGH);
digitalWrite(rmr,HIGH);
digitalWrite(rmf,LOW);
digitalWrite(lmr,HIGH);
digitalWrite(lmf,LOW);
}
void movestop(){
digitalWrite(rme, LOW);
digitalWrite(lme, LOW);
}
void dispara(){
Serial.prinln ("Sensor 1 value"+);
Serial.prinln ("Sensor 1 value"+);
Serial.prinln ("Sensor 1 value"+);
}