forked from Rawkush/Line_Following_robot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Line_Following_robot.ino
145 lines (114 loc) · 2.72 KB
/
Line_Following_robot.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
const int lMotFwd=9;
const int rMotFwd=10;
const int lMotRev=11;
const int rMotRev=12;
const int onblack= 300; // reading for black line from sensor is less than this
const int onWhite= 700; // reading for white line is greater than this
void setup()
{
Serial.begin(9600); //beginning the serial communication(UART) at a baud rate of 9600.
//declaring the input pins for the 5 infra-red sensors...
//format pinMode(A0,INPUT);
pinMode(A1,INPUT); //extreme left sensor
pinMode(A2,INPUT); //left
pinMode(A3,INPUT); // middle sensor
pinMode(A4,INPUT); // right
pinMode(A5,INPUT); // extreme right sensor
//declaring the output pins for the motor driver.
pinMode(lMotFwd,OUTPUT);
pinMode(rMotFwd,OUTPUT);
pinMode(lMotRev,OUTPUT);
pinMode(rMotRev,OUTPUT);
}
//function to check
bool isOnBlack(int sensor)
{
if( sensor<=onblack)
return true;
else
return false;
}
bool isOnWhite(int sensor)
{
if(sensor>=onWhite)
return true;
else
return false;
}
void moveForward()
{
digitalWrite(lMotFwd,HIGH);
digitalWrite(rMotFwd,HIGH);
digitalWrite(rMotRev,LOW);
digitalWrite(lMotRev,LOW);
}
void turnLeft()
{
digitalWrite(lMotFwd,LOW);
digitalWrite(rMotFwd,HIGH);
digitalWrite(rMotRev,LOW);
digitalWrite(lMotRev,LOW);
}
void turnRight()
{
digitalWrite(lMotFwd,HIGH);
digitalWrite(rMotFwd,LOW);
digitalWrite(rMotRev,LOW);
digitalWrite(lMotRev,LOW);
}
void turnCircle()
{
digitalWrite(lMotFwd,LOW);
digitalWrite(rMotRev,LOW);
digitalWrite(lMotRev,HIGH);
digitalWrite(rMotFwd,HIGH);
}
void Stop()
{
digitalWrite(lMotFwd,LOW);
digitalWrite(rMotFwd,LOW);
digitalWrite(lMotRev,LOW);
digitalWrite(rMotRev,LOW);
}
void loop()
{
// put your main code here, to run repeatedly:
int sensor1=analogRead(A1);//sensor1 Extreme Left
int sensor2=analogRead(A2);//sensor2
int sensor3=analogRead(A3);//sensor3 middle
int sensor4=analogRead(A4);//sensor4
int sensor5=analogRead(A5);//sensor5 Extreme right
Serial.print("extreme Left: ");
Serial.print(sensor1);
Serial.print("\t lower left: ");
Serial.print(sensor2);
Serial.print("\t middle: ");
Serial.print(sensor3);
Serial.print("\t lowerRight: ");
Serial.print(sensor4);
Serial.print("\t Extreme Right: ");
Serial.println(sensor5);
if( isOnBlack(sensor1))
{
turnLeft();
}
else
{
if(isOnBlack(sensor3)&&isOnWhite(sensor1)){
moveForward();
}
else
if( isOnWhite(sensor1)&& isOnWhite(sensor3)&& isOnBlack(sensor5) ){
turnRight();
}else
if(isOnBlack(sensor2)){
turnLeft();
}else
if(isOnBlack(sensor4)){
turnRight();
}else
if( isOnWhite(sensor1) && isOnWhite(sensor2) && isOnWhite(sensor3) && isOnWhite(sensor4) && isOnWhite(sensor5) ){
turnCircle(); // replace with left if gap is present
}
}
}