Line Follower and Obstacle Avoiding Robot - Ino
Line Follower and Obstacle Avoiding Robot - Ino
#define servo A5
int Set=15;
int distance_L, distance_F, distance_R;
analogWrite(enA, 200); // Write The Duty Cycle 0 to 255 Enable Pin A for Motor1
Speed
analogWrite(enB, 200); // Write The Duty Cycle 0 to 255 Enable Pin B for Motor2
Speed
pinMode(servo, OUTPUT);
distance_F = Ultrasonic_read();
delay(500);
}
void loop(){
//==============================================
// Line Follower and Obstacle Avoiding
//==============================================
distance_F = Ultrasonic_read();
Serial.print("D F=");Serial.println(distance_F);
//if Right Sensor and Left Sensor are at White color then it will call forword
function
if((digitalRead(R_S) == 0)&&(digitalRead(L_S) == 0)){
if(distance_F > Set){forword();}
else{Check_side();}
}
//if Right Sensor is Black and Left Sensor is White then it will call turn Right
function
else if((digitalRead(R_S) == 1)&&(digitalRead(L_S) == 0)){turnRight();}
//if Right Sensor is White and Left Sensor is Black then it will call turn Left
function
else if((digitalRead(R_S) == 0)&&(digitalRead(L_S) == 1)){turnLeft();}
delay(10);
}
//**********************Ultrasonic_read****************************
long Ultrasonic_read(){
digitalWrite(trigger, LOW);
delayMicroseconds(2);
digitalWrite(trigger, HIGH);
delayMicroseconds(10);
long time = pulseIn (echo, HIGH);
return time / 29 / 2;
}
void compareDistance(){
if(distance_L > distance_R){
turnLeft();
delay(500);
forword();
delay(600);
turnRight();
delay(500);
forword();
delay(600);
turnRight();
delay(400);
}
else{
turnRight();
delay(500);
forword();
delay(600);
turnLeft();
delay(500);
forword();
delay(600);
turnLeft();
delay(400);
}
}
void Check_side(){
Stop();
delay(100);
for (int angle = 70; angle <= 140; angle += 5) {
servoPulse(servo, angle); }
delay(300);
distance_R = Ultrasonic_read();
Serial.print("D R=");Serial.println(distance_R);
delay(100);
for (int angle = 140; angle >= 0; angle -= 5) {
servoPulse(servo, angle); }
delay(500);
distance_L = Ultrasonic_read();
Serial.print("D L=");Serial.println(distance_L);
delay(100);
for (int angle = 0; angle <= 70; angle += 5) {
servoPulse(servo, angle); }
delay(300);
compareDistance();
}