-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLineFollower.ino
More file actions
143 lines (77 loc) · 2.38 KB
/
LineFollower.ino
File metadata and controls
143 lines (77 loc) · 2.38 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
//This is code for Line follower using 6 channel sensor array.
//The code is self explanatory with proper commenting showing pins and functionality
#define Kp 20 // experiment to determine this, start by something small that just makes your bot follow the line at a slow speed
#define Kd 5 // experiment to determine this, slowly increase the speeds and adjust this value. ( Note: Kp < Kd)
#define MaxSpeed 100// max speed of the robot
#define BaseSpeed 65 // this is the speed at which the motors should spin when the robot is perfectly on the line
int prevpos;
bool flag=false;
#define speedturn 120
#define rightMotor1 5
#define rightMotor2 4
#define rightMotorPWM 8
#define leftMotor1 6
#define leftMotor2 7
#define leftMotorPWM 9
int error,motorSpeed,pos,preverr,rspeed,lspeed,correction;
int sense[]={A0,A1,A2,A3,A4,A5};
int sread[6];
int lastError = 0;
void setup() {
// put your setup code here, to run once:
for(int i=0;i<6;i++)
pinMode(sense[i],INPUT);
pinMode(rightMotor1, OUTPUT);
pinMode(rightMotor2, OUTPUT);
pinMode(rightMotorPWM, OUTPUT);
pinMode(leftMotor1, OUTPUT);
pinMode(leftMotor2, OUTPUT);
pinMode(leftMotorPWM, OUTPUT);
Serial.begin(9600);
}
void loop() {
for(int i=0;i<6;i++)
{
sread[i]=digitalRead(sense[i]);
//Serial.print(sread[i]);
}
pos=32*sread[0]+16*sread[1]+8*sread[2]+4*sread[3]+2*sread[4]+1*sread[5];
if(pos==60||pos==62)//r turn
{
analogWrite(leftMotor1,speedturn);
digitalWrite(leftMotor2,LOW);
digitalWrite(rightMotor1,LOW);
analogWrite(rightMotor2,speedturn);
}
if(pos==31||pos==15)//l turn {
{
analogWrite(leftMotor2,speedturn);
digitalWrite(leftMotor1,LOW);
digitalWrite(rightMotor2,LOW);
analogWrite(rightMotor1,speedturn);
}
if(pos==12 || pos==14 ||pos==28)
error=0;
if(pos==6)
error=1; // left charivu
if(pos==52 || pos==60 )
error=-1; // right charivu
if(pos==3 || pos==2)
error=2; // left charivu
if(pos==48)
error=-2; // left charivu
if(pos==32)
error=-3; // right charivu
if(pos==1)
error=3; // right charivu
correction = Kp*error +Kd*(error-preverr);
preverr=error;
rspeed=constrain(BaseSpeed-correction,0,MaxSpeed);
lspeed=constrain(BaseSpeed+correction,0,MaxSpeed);
Serial.println();
Serial.println(pos);
analogWrite(leftMotor1,lspeed);
digitalWrite(leftMotor2,LOW);
analogWrite(rightMotor1,rspeed);
digitalWrite(rightMotor2,LOW);
}