Line Follower
The above video shows how SajiloBot follows a black line using the help of the Infrared Sensor on the bottom of the robot. Below is the Arduino code to make the robot work in line following mode.
// Black Line Follower
int IR1=A0; // Right sensor
int IR2=A1; // Left Sensor
// motor one
int enA = 9; //Right motor
int MotorAip1=2;
int MotorAip2=3;
// motor two
int enB = 10; //Left motor
int MotorBip1=7;
int MotorBip2=8;
void setup(){
// put your setup code here, to run once:
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(IR1,INPUT);
pinMode(IR2,INPUT);
pinMode(MotorAip1,OUTPUT);
pinMode(MotorAip2,OUTPUT);
pinMode(MotorBip1,OUTPUT);
pinMode(MotorBip2,OUTPUT);
}
void loop(){
if(digitalRead(IR1)==HIGH && digitalRead(IR2)==HIGH) //IR will not glow on black line
{
//Stop both Motors
digitalWrite(MotorAip1,LOW);
digitalWrite(MotorAip2,LOW);
digitalWrite(MotorBip1,LOW);
digitalWrite(MotorBip2,LOW);
analogWrite (enA, 0);
analogWrite (enB, 0);
}
//IR not on black line
else if(digitalRead(IR1)==LOW && digitalRead(IR2)==LOW) {
//Move both the Motors
digitalWrite(MotorAip1,HIGH);
digitalWrite(MotorAip2,LOW);
digitalWrite(MotorBip1,HIGH);
digitalWrite(MotorBip2,LOW);
analogWrite (enA, 200);
analogWrite (enB, 200);
}
//Tilt robot towards left by stopping the left wheel and moving the right one
else if(digitalRead(IR1)==LOW && digitalRead(IR2)==HIGH) {
digitalWrite(MotorAip1,HIGH);
digitalWrite(MotorAip2,LOW);
digitalWrite(MotorBip1,LOW);
digitalWrite(MotorBip2,HIGH);
analogWrite (enA, 200);
analogWrite (enB, 0);
delay(100);
}
//Tilt robot towards right by stopping the right wheel and moving the left one
else if(digitalRead(IR1)==HIGH && digitalRead(IR2)==LOW) {
digitalWrite(MotorAip1,LOW); // If I want to turn right then the speed of the right wheel should be
// less than that of the left wheel, here, let a be the right wheel
digitalWrite(MotorAip2,HIGH);
digitalWrite(MotorBip1,HIGH);
digitalWrite(MotorBip2,LOW);
analogWrite (enA, 0);
analogWrite (enB, 200);
delay(100);
}
else {
//Stop both the motors
digitalWrite(MotorAip1,LOW);
digitalWrite(MotorAip2,LOW);
digitalWrite(MotorBip1,LOW);
digitalWrite(MotorBip2,LOW);
analogWrite (enA, 0);
analogWrite (enB, 0);
}
}