# Line follower and obstacle avoider robot

Hi am making a line follower with the ability to detect and avoid an obstacle in the line by turning around it (right side) without getting lost and returning to follow the black line but i think something is wrong because the robot doesn’t start moving at all. Am using arduino Uno+2 HC ultrasonic sensors and 3 (LDR+LED) to detect the black line. This is my code

int LDR1=A0, LDR2=A1, LDR3=A2; // sensor values…note:we will add 2 extra sensors for more accurecy and LEDs to impove light dependance

#define trigPin 4
#define echoPin 3
#define trigPin2 7
#define echoPin2 11

boolean obstacle,left_detect, line;

int PMW1 = 6;
int M1 = 13;
int M1_2=2;
int PMW2 = 5;
int M2 = 12;
int M2_2=8;

// calibration offsets
int leftOffset = 0, rightOffset = 0, centre = 0;
// pins for motor speed and direction

// starting speed and rotation offset
int startSpeed = 150, rotate = 30; /* CHANGE START SPEED IF YOUR ROBOT IS TOO SLOW, AND CHANGE THE ROTATE VALUE ACCORDING TO YOUR
TURNING ANGLE OF BLACK LINE.*/

// sensor threshold
int threshhold = 5;
/Threshold is the difference in values required between the center sensor and the left or right sensors
before the robot decides to make a turn. In my case, a setting of 5 worked well. This means that the left and right
sensors would need to detect a value greater than the value read from the center sensor plus the
threshold value before action is taken. In other words, if the center sensor is reading a value of 600 and
the left sensor is reading 603, then the robot will keep going straight. However, a left sensor value of 612
(which is higher than the center value plus threshold) means that the left sensor is detecting the back
line, indicating that the robot is too far over to the left. So the motors would adjust to make the robot
turn to the right to compensate.
The threshold value will vary depending on the contrast between your floor (or whatever surface
you use) and the black line. This may need to be adjusted to ensure the robot only turns when it has
detected enough of a difference between floor and line to ascertain it had moved too far left or right.
/

// initial speeds of left and right motors
int left = startSpeed, right = startSpeed;
// Sensor calibration routine
void calibrate() {
for (int x=0; x<10; x++) { // run this 10 times to obtain average
leftOffset = leftOffset + LDR1; // add value of left sensor to total
centre = centre + LDR2; // add value of centre sensor to total
rightOffset = rightOffset + LDR3; // add value of right sensor to total
delay(100);
}
// obtain average for each sensor
leftOffset = leftOffset / 10;
rightOffset = rightOffset / 10;
centre = centre /10;
// calculate offsets for left and right sensors
leftOffset = centre - leftOffset;
rightOffset = centre - rightOffset;
}
void setup()
{
//ultrasonor detector initials
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(trigPin2, OUTPUT);

pinMode(echoPin, INPUT);
pinMode(echoPin2, INPUT);

// set the motor pins to outputs
pinMode(M1, OUTPUT);
pinMode(M2, OUTPUT);
pinMode(PMW1,OUTPUT);
pinMode(PMW2,OUTPUT);
pinMode(M1_2,OUTPUT);
pinMode(M2_2,OUTPUT);

//initial states

// calibrate the sensors
calibrate();
delay(3000);
// set motor direction to forward
// set speed of both motors
analogWrite(PMW1,left);
analogWrite(PMW2,right);
}
void loop() {
//detect distance
long duration, distance;
digitalWrite(trigPin, LOW); // Added this line
digitalWrite(trigPin, HIGH);
// delayMicroseconds(1000); - Removed this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance < 4) { // This is where the LED On/Off happens
}

if (distance >= 200 || distance <= 0){
Serial.println(“Out of range”);
}
else {
Serial.print(distance);
Serial.println(" cm");
}
delay(500);
//detect line,obstacle and left obstacle
if (distance<=11)
obstacle=true;
else
obstacle=false;
if(left_detect<=13)
left_detect=true;

else
(left_detect=false);

//line detecting
if (LDR2-LDR1<=70||LDR2-LDR3<=70)
line=false;

else
line=true;

//emergency avoiding in sudden turns with obstacle
if (distance<=5) {
void STOP();
while (distance<=11) {
void back();
if (distance>=10){
void STOP();
obstacle=false;
}
}

}

if (obstacle==false)
void line_follow();
//if obstacle detected avoid until no more obstacle
else if (obstacle==true)
void avoid();
}

void avoid() {
//while obstacle or left obstace detected, avoid until line is detected again
while((obstacle=true)||(left_detect=true)&&(line=false)) {

//avoid obstacle by turning around it while conserving distance from it using ultrasonic detector:
//turn right until object detected by left sensor and robot off line
while ((left_detect=false)&&(obstacle=true)&&(line=true)) {
void right2() ;
if ((left_detect==true)&&(line==false)&&(obstacle==false)){
void STOP();
delay(30);
break;
}
}
if (line==true) {
void line_follow();
obstacle=false ;

break; //break from avoid while funtion and return to the line following function
}
}

}

void line_follow() {
// make both motors same speed
left = startSpeed;
right = startSpeed;
// if LDR1 is greater than the centre sensor + threshold turn right
if (LDR1 > (LDR2+threshhold)) {
left = startSpeed + rotate;
right = startSpeed - rotate;
}
// if LDR3 is greater than the centre sensor + threshold turn left
if (LDR3 > (LDR2+threshhold)) {
left = startSpeed - rotate;
right = startSpeed + rotate;
}
// send the speed values to the motors
analogWrite(PMW1,left);
digitalWrite(M1,HIGH);
digitalWrite(M1_2,LOW);

analogWrite(PMW2,right);
digitalWrite(M2,HIGH);
digitalWrite(M2_2,LOW);
}

void GO(){
int value;
for(value = 0 ; value <= 220; value+=10)
{
digitalWrite(M1,HIGH);
digitalWrite(M2, HIGH);
digitalWrite(M1_2,LOW);
digitalWrite(M2_2, LOW);
analogWrite(PMW1, value); //PWM Speed Control
analogWrite(PMW2, value); //PWM Speed Control
}

}

void back() {
int value;
for(value = 0 ; value <= 220; value+=10)
{
digitalWrite(M1,LOW);
digitalWrite(M2, LOW);
digitalWrite(M1_2,HIGH);
digitalWrite(M2_2, HIGH);
analogWrite(PMW1, value); //PWM Speed Control
analogWrite(PMW2, value); //PWM Speed Control
}
}
void right2() {
int value;
for(value = 0 ; value <= 220; value+=10)
{
digitalWrite(M1,LOW);
digitalWrite(M2, LOW);
digitalWrite(M1_2,HIGH);
digitalWrite(M2_2, LOW);
analogWrite(PMW1, value); //PWM Speed Control
analogWrite(PMW2, value); //PWM Speed Control
}
}
void GO_M1() {
int value;
for(value = 0 ; value <= 220; value+=10)
{
digitalWrite(M1,HIGH);
digitalWrite(M2, LOW);
digitalWrite(M1_2,LOW);
digitalWrite(M2_2, LOW);
analogWrite(PMW1, value); //PWM Speed Control
analogWrite(PMW2, value); //PWM Speed Control
}
}//right
void GO_M2() {
int value;
for(value = 0 ; value <= 220; value+=10)
{
digitalWrite(M1,LOW);
digitalWrite(M2, HIGH);
digitalWrite(M1_2,LOW);
digitalWrite(M2_2, LOW);
analogWrite(PMW1, value); //PWM Speed Control
analogWrite(PMW2, value); //PWM Speed Control
}
}

`````` pinMode(trigPin, OUTPUT);
pinMode(trigPin2, OUTPUT);

pinMode(echoPin, INPUT);
pinMode(echoPin2, INPUT);

// set the motor pins to outputs
pinMode(M1, OUTPUT);
pinMode(M2, OUTPUT);
pinMode(PMW1,OUTPUT);
pinMode(PMW2,OUTPUT);
pinMode(M1_2,OUTPUT);
pinMode(M2_2,OUTPUT);
``````

So, the M pins both get numbers, the PMW (pulse mangled width? What does PMW mean) pins both get numbers, the M_2 pins both get numbers (but in a different place. WTF is up with that?), but the trigger and echo pins are not both numbered. Why? Consistency is a good thing.

``````  if (distance < 4) {  // This is where the LED On/Off happens
}
``````

No, it isn’t. NOTHING happens there.

``````if(left_detect<=13)
``````

left_detect is a boolean. Why would you be comparing true or false to 13?

``````(left_detect=false);
``````

(The) (parentheses) (are) (useless) (and) (look) (as) (silly) (as) (mine).

``````if (LDR2-LDR1<=70||LDR2-LDR3<=70)
``````

Lookslikeyourspacebarneedsfixing.

``````if (distance<=5) {
void STOP();
while (distance<=11)   {
void back();
if (distance>=10){
void STOP();
obstacle=false;
``````

What’s with all the function prototypes? You are not calling functions there.

After that, I gave up reading the code.

:o well.....thanks for the sarcastic reply i guess ...PS: i recently began programming arduino and C. same goes to robotics (my first robot) if you're annoyed of correcting codes just don't bother to read in first place but again, i appreciate the useful reply

Hi,
To help us with you sketch can you please read this link and go back to your first post and MODIFY it to format the sketch properly.

Thanks Tom… 