Servo turning without ultrasonic command and IR not sensing line but proximity

I have built a line follower with collision avoidance but am experiencing some issues.
I have used a single motor with two free motion wheels. A servo is attached to the motor to turn either left o right if obstacle detected either left or right by the left or right ultrasonic sensor respectively.
The issue is the servo is continuously turning left or right without the input o the ultrasonic sensor.

Another issue is the two IR sensors aren't detecting line, but are detecting proximity and stopping the motor when they detect.

code is below

#include <Servo.h>

#define leftIr 2
#define rightIr 3

#define MotorSpeed 8
#define MotorP 4
#define MotorN 5
int potValue;
#define leftultrasonicTrigger 6
#define leftultrasonicEcho 7
#define rightultrasonicTrigger 11
#define rightultrasonicEcho 12
Servo myservo;

// motors speed (0 - 255)
const float FORWARD_SPEED = 150;
const float CURVE_SPEED = 120;

void setup() {
  Serial.begin(115200);
  myservo.attach(13);
  pinMode(leftIr, INPUT);
  pinMode(rightIr, INPUT);
  pinMode(MotorSpeed, OUTPUT);
  pinMode(MotorP, OUTPUT);
  pinMode(MotorN, OUTPUT);
  pinMode(leftultrasonicTrigger, OUTPUT);
  pinMode(leftultrasonicEcho, INPUT);
  pinMode(rightultrasonicTrigger, OUTPUT);
  pinMode(rightultrasonicEcho, INPUT);
}

void loop() {
  long leftduration, leftdistance;
  digitalWrite(leftultrasonicTrigger, HIGH);
  delayMicroseconds(1000);
  digitalWrite(leftultrasonicTrigger, LOW);
  leftduration = pulseIn(leftultrasonicEcho, HIGH);
  leftdistance = (leftduration / 2) / 29.1;
  Serial.print(leftdistance);
  Serial.println("CM");
  delay(10);
  potValue = leftdistance;
  potValue = map(potValue, 0, 1023, 0, 180);
  long rightduration, rightdistance;
  digitalWrite(rightultrasonicTrigger, HIGH);
  delayMicroseconds(1000);
  digitalWrite(rightultrasonicTrigger, LOW);
  rightduration = pulseIn(rightultrasonicEcho, HIGH);
  rightdistance = (rightduration / 2) / 29.1;
  Serial.print(rightdistance);
  Serial.println("CM");
  delay(10);
  potValue = rightdistance;
  potValue = map(potValue, 0, 1023, 0, 180);

    if ((leftdistance <= 50)) //&& !(rightdistance >= 50))
  {
     // PWM
  analogWrite(MotorSpeed, 0);
  // Direction
  digitalWrite(MotorP, LOW);
  digitalWrite(MotorN, LOW);
  myservo.write(87);
    delay(2000);
    
   // PWM
  analogWrite(MotorSpeed, CURVE_SPEED);
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(132);
    delay(3000);
    
    // PWM
  analogWrite(MotorSpeed, FORWARD_SPEED);

  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(87);
    delay(5000);
    
    // PWM
  analogWrite(MotorSpeed, CURVE_SPEED);
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(42);
     delay(3000);
    
      // PWM
  analogWrite(MotorSpeed, FORWARD_SPEED);

  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(87);
    delay(5000);
        Serial.println('leftdistance');
  }

  else if ((rightdistance <= 50))// && !(leftdistance >= 50))
  {
    // PWM
  analogWrite(MotorSpeed, 0);
  // Direction
  digitalWrite(MotorP, LOW);
  digitalWrite(MotorN, LOW);
  myservo.write(87);
    delay(2000);
    
           // PWM
  analogWrite(MotorSpeed, CURVE_SPEED);
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(42);
     delay(3000);
     
         //PMW
    analogWrite(MotorSpeed, FORWARD_SPEED);
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(87);
    delay(5000);
   
   analogWrite(MotorSpeed, CURVE_SPEED);
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(42);
     delay(3000);
     
      //PMW
    analogWrite(MotorSpeed, FORWARD_SPEED);
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(87);
    delay(5000);
        Serial.println('rightdistance');
  }
  if ((leftdistance <= 100)  && (rightdistance <= 100))
  {
    // PWM
  analogWrite(MotorSpeed, 0);
  // Direction
  digitalWrite(MotorP, LOW);
  digitalWrite(MotorN, LOW);
    myservo.write(87);
  }


if   ( digitalRead(leftIr)  && digitalRead(rightIr)){
  // PWM
  analogWrite(MotorSpeed, FORWARD_SPEED);

  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(87);

}

if   (digitalRead(leftIr) && !digitalRead(rightIr)){
  // PWM
  analogWrite(MotorSpeed, CURVE_SPEED);
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(132);

}

if  (!(digitalRead(leftIr)) && digitalRead(rightIr)){
  // PWM
  analogWrite(MotorSpeed, CURVE_SPEED);
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(42);

}

if  (!(digitalRead(leftIr)) && !digitalRead(rightIr)){
  // PWM
  analogWrite(MotorSpeed, 0);
  // Direction
  digitalWrite(MotorP, LOW);
  digitalWrite(MotorN, LOW);
  myservo.write(87);

}
}
Serial.println('rightdistance')

Oops.

What do your serial debug prints tell you?

Why does your code wander around the page, like a drunken line-follower?

delayMicroseconds(1000) Why so long?
10 us is usually sufficient.

Please explain "Another issue is the two IR sensors aren't detecting line, but are detecting proximity and stopping the motor when they detect.". How did you determine this?

Paul

Paul_KD7HB:
Please explain "Another issue is the two IR sensors aren't detecting line, but are detecting proximity and stopping the motor when they detect.". How did you determine this?

Paul

i have done the connection of the hardware and once i put it along the line it doesnt detect a line,instead it detects proximity to the floor

alexmunala:
i have done the connection of the hardware and once i put it along the line it doesnt detect a line,instead it detects proximity to the floor

Perhaps your -line- and the floor look the same to the IR sensors.

Paul

Paul_KD7HB:
Perhaps your -line- and the floor look the same to the IR sensors.

Paul

have tried a black tape on a white board but aint working out

alexmunala:
have tried a black tape on a white board but aint working out

But, did you look at them with an IR light and sensor?

Paul

Paul_KD7HB:
But, did you look at them with an IR light and sensor?

Paul

The IR light turns on when it detects proximity to the floor.

So i did some corrections and the IR now detects the line but only in stand alone test.When i add the code for collision avoidance the two codes seem the act at once ...So no obtacle avoidance and line detection.
Also the obstacle avoidance works fine in standalone test.

my line follower code

#include <Servo.h>

#define leftIr 2
#define rightIr 3

#define MotorSpeed 8
#define MotorP 4
#define MotorN 5

Servo myservo;

// motors speed (0 - 255)
const float FORWARD_SPEED = 150;
const float CURVE_SPEED = 120;

void setup() {
  Serial.begin(115200);
  myservo.attach(13);
  pinMode(leftIr, INPUT);
  pinMode(rightIr, INPUT);
  pinMode(MotorSpeed, OUTPUT);
  pinMode(MotorP, OUTPUT);
  pinMode(MotorN, OUTPUT);
  pinMode(TriggerPin1, OUTPUT);
  
  
}
long duration, distance, rightdistance, leftdistance;

void loop() {
  
if   ( !digitalRead(leftIr)  && !digitalRead(rightIr)){
  // PWM
  analogWrite(MotorSpeed, FORWARD_SPEED);

  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(87);

}

else if   (!(digitalRead(leftIr) && digitalRead(rightIr))){
  // PWM
  analogWrite(MotorSpeed, CURVE_SPEED);
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(132);
  delay(500);

}

else if  (digitalRead(leftIr) && (!digitalRead(rightIr))){
  // PWM
  analogWrite(MotorSpeed, CURVE_SPEED);
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(42);
  delay(500);

}

else if  ((digitalRead(leftIr) && (digitalRead(rightIr)))){
  // PWM
  analogWrite(MotorSpeed, 0);
  // Direction
  digitalWrite(MotorP, LOW);
  digitalWrite(MotorN, LOW);
  myservo.write(87);

}
}

The obstacle avoidance is below

#include <Servo.h>


#define MotorSpeed 8
#define MotorP 4
#define MotorN 5
int potValue;
int TriggerPin1 = 11;
int EchoPin1 = 12;
int TriggerPin2= 6;
int EchoPin2= 7;
Servo myservo;

// motors speed (0 - 255)
const float FORWARD_SPEED = 150;
const float CURVE_SPEED = 120;

void setup() {
  Serial.begin(115200);
  myservo.attach(13);
 
  pinMode(MotorSpeed, OUTPUT);
  pinMode(MotorP, OUTPUT);
  pinMode(MotorN, OUTPUT);
  pinMode(TriggerPin1, OUTPUT);
  pinMode(EchoPin1, INPUT);
   pinMode(TriggerPin2, OUTPUT);
  pinMode(EchoPin2, INPUT);
  
}
long duration, distance, rightdistance, leftdistance;
void loop() {
 potValue = distance;
  potValue = map(potValue, 0, 1023, 0, 180);
 SonarSensor(TriggerPin1, EchoPin1);
rightdistance= distance;
SonarSensor(TriggerPin2, EchoPin2);
leftdistance=distance;
Serial.print(rightdistance);
Serial.print("_");
Serial.println(leftdistance);
 
   if ((leftdistance < 50)) && !(rightdistance >= 50))
  {
     // PWM
  analogWrite(MotorSpeed, 0);
  // Direction
  digitalWrite(MotorP, LOW);
  digitalWrite(MotorN, LOW);
  myservo.write(87);
   // delay(500);
    
   // PWM
  analogWrite(MotorSpeed, CURVE_SPEED);
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(132);
    delay(2000);
    
    // PWM
  analogWrite(MotorSpeed, FORWARD_SPEED);

  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(87);
    delay(1500);
    
    // PWM
  analogWrite(MotorSpeed, CURVE_SPEED);
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(42);
     delay(2000);
    
      // PWM
  analogWrite(MotorSpeed, FORWARD_SPEED);

  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(87);
           Serial.println('leftdistance');
  }

   if ((rightdistance <50)) && !(leftdistance >= 50))
  {
    // PWM
  analogWrite(MotorSpeed, 0);
  // Direction
  digitalWrite(MotorP, LOW);
  digitalWrite(MotorN, LOW);
  myservo.write(87);
    //delay(500);
    
           // PWM
  analogWrite(MotorSpeed, CURVE_SPEED);
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(42);
     delay(2000);
     
         //PMW
    analogWrite(MotorSpeed, FORWARD_SPEED);
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(87);
    delay(1500);
   
   analogWrite(MotorSpeed, CURVE_SPEED);
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(132);
     delay(2000);
     
      //PMW
    analogWrite(MotorSpeed, FORWARD_SPEED);
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(87);
            Serial.println('rightdistance');
  }

  if ((leftdistance   && rightdistance )< 30)
  {
    // PWM
  analogWrite(MotorSpeed, 0);
  // Direction
  digitalWrite(MotorP, LOW);
  digitalWrite(MotorN, LOW);
    myservo.write(87);
  }


}

void SonarSensor(int TriggerPin, int EchoPin)
{
  digitalWrite(TriggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(TriggerPin, LOW);
  duration = pulseIn(EchoPin, HIGH);
  distance = (duration / 2) / 29.1;
  }

I will really appreciate your corrections and comments and directions.

Here is the code for both linefollower and obstacle avoidance.

#include <Servo.h>

#define leftIr 2
#define rightIr 3

#define MotorSpeed 8
#define MotorP 4
#define MotorN 5
int potValue;
int TriggerPin1 = 11;
int EchoPin1 = 12;
int TriggerPin2= 6;
int EchoPin2= 7;
Servo myservo;

// motors speed (0 - 255)
const float FORWARD_SPEED = 150;
const float CURVE_SPEED = 120;

void setup() {
  Serial.begin(115200);
  myservo.attach(13);
  pinMode(leftIr, INPUT);
  pinMode(rightIr, INPUT);
  pinMode(MotorSpeed, OUTPUT);
  pinMode(MotorP, OUTPUT);
  pinMode(MotorN, OUTPUT);
  pinMode(TriggerPin1, OUTPUT);
  pinMode(EchoPin1, INPUT);
   pinMode(TriggerPin2, OUTPUT);
  pinMode(EchoPin2, INPUT);
  
}
long duration, distance, rightdistance, leftdistance;

void loop() {
 potValue = distance;
  potValue = map(potValue, 0, 1023, 0, 180);
 SonarSensor(TriggerPin1, EchoPin1);
rightdistance= distance;
SonarSensor(TriggerPin2, EchoPin2);
leftdistance=distance;
Serial.print(rightdistance);
Serial.print("_");
Serial.println(leftdistance);
 
  
if   ( !digitalRead(leftIr)  && !digitalRead(rightIr)){
  // PWM
  analogWrite(MotorSpeed, FORWARD_SPEED);

  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(87);

}

else if   (!(digitalRead(leftIr) && digitalRead(rightIr))){
  // PWM
  analogWrite(MotorSpeed, CURVE_SPEED);
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(132);

}

else if  (digitalRead(leftIr) && (!digitalRead(rightIr))){
  // PWM
  analogWrite(MotorSpeed, CURVE_SPEED);
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(42);

}

else if  ((digitalRead(leftIr) && (digitalRead(rightIr)))){
  // PWM
  analogWrite(MotorSpeed, 0);
  // Direction
  digitalWrite(MotorP, LOW);
  digitalWrite(MotorN, LOW);
  myservo.write(87);

}
 
 
 
  if ((leftdistance < 50)) //&& !(rightdistance >= 50))
  {
     // PWM
  analogWrite(MotorSpeed, 0);
  // Direction
  digitalWrite(MotorP, LOW);
  digitalWrite(MotorN, LOW);
  myservo.write(87);
   // delay(500);
    
   // PWM
  analogWrite(MotorSpeed, CURVE_SPEED);
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(132);
    delay(2000);
    
    // PWM
  analogWrite(MotorSpeed, FORWARD_SPEED);

  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(87);
    delay(1500);
    
    // PWM
  analogWrite(MotorSpeed, CURVE_SPEED);
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(42);
     delay(2000);
    
      // PWM
  analogWrite(MotorSpeed, FORWARD_SPEED);

  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(87);
           Serial.println('leftdistance');
  }

   if ((rightdistance <50))// && !(leftdistance >= 50))
  {
    // PWM
  analogWrite(MotorSpeed, 0);
  // Direction
  digitalWrite(MotorP, LOW);
  digitalWrite(MotorN, LOW);
  myservo.write(87);
    //delay(500);
    
           // PWM
  analogWrite(MotorSpeed, CURVE_SPEED);
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(42);
     delay(2000);
     
         //PMW
    analogWrite(MotorSpeed, FORWARD_SPEED);
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(87);
    delay(1500);
   
   analogWrite(MotorSpeed, CURVE_SPEED);
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(132);
     delay(2000);
     
      //PMW
    analogWrite(MotorSpeed, FORWARD_SPEED);
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(87);
            Serial.println('rightdistance');
  }

  if ((leftdistance   && rightdistance )< 30)
  {
    // PWM
  analogWrite(MotorSpeed, 0);
  // Direction
  digitalWrite(MotorP, LOW);
  digitalWrite(MotorN, LOW);
    myservo.write(87);
  }


}
void SonarSensor(int TriggerPin, int EchoPin)
{
  digitalWrite(TriggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(TriggerPin, LOW);
  duration = pulseIn(EchoPin, HIGH);
  distance = (duration / 2) / 29.1;
  }

Will appreciate your advices

Why don't you get your code to tell you what it is doing?

I feel I've mentioned this before.

Why doesn't SonarSensor return a value?
I feel I've mentioned this too.

Serial.println('rightdistance')

And that nonsense.
I know I've mentioned that.

And get rid of the delays.
You don't drive down the road closing your eyes for two seconds at a time.

Hi,

Another issue is the two IR sensors aren't detecting line, but are detecting proximity and stopping the motor when they detect.

What are you using as IR detector?
Please post a picture of your IR detector.

Check that you haven't bought an IR distance sensor.

Tom... :slight_smile:

 if ((leftdistance   && rightdistance )< 30)

// YOU PROBABLY MEAN
  if (leftdistance < 30   && rightdistance < 30)

// then there's
           Serial.println('leftdistance');
// and  
           Serial.println('rightdistance');

// you either mean the value (so no ' ') or the text (so " " not ' ')

They're warnings rather than errors because they will all do something...just probably not what you wanted.

Steve

TomGeorge:
Hi,

What are you using as IR detector?
Please post a picture of your IR detector.

Check that you haven't bought an IR distance sensor.

Tom... :slight_smile:

i fixed the issue now with a stand alone test it can detect the line.
but when i add the sketch for obstacle avoidance nothing actually works the way its supposed to..

when i run this code with both ir and ultrasonic the both seem to intefere with each other.

#include <Servo.h>

#define leftIr 2
#define rightIr 3

#define MotorSpeed 8
#define MotorP 4
#define MotorN 5
int potValue;
int TriggerPin1 = 11;
int EchoPin1 = 12;
int TriggerPin2= 6;
int EchoPin2= 7;
Servo myservo;

// motors speed (0 - 255)
const float FORWARD_SPEED = 150;
const float CURVE_SPEED = 120;

void setup() {
  Serial.begin(115200);
  myservo.attach(13);
  pinMode(leftIr, INPUT);
  pinMode(rightIr, INPUT);
  pinMode(MotorSpeed, OUTPUT);
  pinMode(MotorP, OUTPUT);
  pinMode(MotorN, OUTPUT);
  pinMode(TriggerPin1, OUTPUT);
  pinMode(EchoPin1, INPUT);
   pinMode(TriggerPin2, OUTPUT);
  pinMode(EchoPin2, INPUT);
  
}
long duration, distance, rightdistance, leftdistance;

void loop() {
 /*potValue = distance;
  potValue = map(potValue, 0, 1023, 0, 180);
 SonarSensor(TriggerPin1, EchoPin1);
rightdistance= distance;
SonarSensor(TriggerPin2, EchoPin2);
leftdistance=distance;
Serial.print(rightdistance);
Serial.print("_");
Serial.println(leftdistance);
 */
  
if   ((digitalRead(leftIr)==LOW)  && (digitalRead(rightIr)==LOW)){
  // PWM
  analogWrite(MotorSpeed, FORWARD_SPEED);

  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(87);

}

else if   ((digitalRead(leftIr)==HIGH) && (digitalRead(rightIr))==LOW){
  // PWM
  analogWrite(MotorSpeed, CURVE_SPEED);
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(42);
   delay(200);
}

else if  ((digitalRead(leftIr)==LOW) && ((digitalRead(rightIr)==HIGH))){
  // PWM
  analogWrite(MotorSpeed, CURVE_SPEED);
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(132);
   delay(200);
}

else if  ((digitalRead(leftIr)==HIGH) && (digitalRead(rightIr)==HIGH)){
  // PWM
  analogWrite(MotorSpeed, 0);
  // Direction
  digitalWrite(MotorP, LOW);
  digitalWrite(MotorN, LOW);
  myservo.write(87);

}

  if ((leftdistance < 50)) //&& !(rightdistance >= 50))
  {
     // PWM
  analogWrite(MotorSpeed, 0);
  // Direction
  digitalWrite(MotorP, LOW);
  digitalWrite(MotorN, LOW);
  myservo.write(87);
   // delay(500);
    
   // PWM
  analogWrite(MotorSpeed, CURVE_SPEED);
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(132);
    delay(2000);
    
    // PWM
  analogWrite(MotorSpeed, FORWARD_SPEED);

  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(87);
    delay(1500);
    
    // PWM
  analogWrite(MotorSpeed, CURVE_SPEED);
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(42);
     delay(2000);
    
      // PWM
  analogWrite(MotorSpeed, FORWARD_SPEED);

  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(87);
           Serial.println('leftdistance');
  }

   if ((rightdistance <50))// && !(leftdistance >= 50))
  {
    // PWM
  analogWrite(MotorSpeed, 0);
  // Direction
  digitalWrite(MotorP, LOW);
  digitalWrite(MotorN, LOW);
  myservo.write(87);
    //delay(500);
    
           // PWM
  analogWrite(MotorSpeed, CURVE_SPEED);
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(42);
     delay(2000);
     
         //PMW
    analogWrite(MotorSpeed, FORWARD_SPEED);
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(87);
    delay(1500);
   
   analogWrite(MotorSpeed, CURVE_SPEED);
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(132);
     delay(2000);
     
      //PMW
    analogWrite(MotorSpeed, FORWARD_SPEED);
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(87);
            Serial.println('rightdistance');
  }

  if ((leftdistance   && rightdistance )< 30)
  {
    // PWM
  analogWrite(MotorSpeed, 0);
  // Direction
  digitalWrite(MotorP, LOW);
  digitalWrite(MotorN, LOW);
    myservo.write(87);
  }


}
void SonarSensor(int TriggerPin, int EchoPin)
{
  digitalWrite(TriggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(TriggerPin, LOW);
  duration = pulseIn(EchoPin, HIGH);
  distance = (duration / 2) / 29.1;
  }

  if ((leftdistance   && rightdistance )< 30)

It is hard to help those who don't seem to want to be helped.

TheMemberFormerlyKnownAsAWOL:

  if ((leftdistance   && rightdistance )< 30)

It is hard to help those who don't seem to want to be helped.

Hey buddy, i changed that but still the two codes put together are interfering with each other..such that the servo continuously keeps turning both angles.
here is the code

#include <Servo.h>

#define leftIr 2
#define rightIr 3

#define MotorSpeed 8
#define MotorP 4
#define MotorN 5
int potValue;
int TriggerPin1 = 11;
int EchoPin1 = 12;
int TriggerPin2= 6;
int EchoPin2= 7;
Servo myservo;

// motors speed (0 - 255)
const float FORWARD_SPEED = 150;
const float CURVE_SPEED = 120;

void setup() {
  Serial.begin(115200);
  myservo.attach(13);
  pinMode(leftIr, INPUT);
  pinMode(rightIr, INPUT);
  pinMode(MotorSpeed, OUTPUT);
  pinMode(MotorP, OUTPUT);
  pinMode(MotorN, OUTPUT);
  pinMode(TriggerPin1, OUTPUT);
  pinMode(EchoPin1, INPUT);
   pinMode(TriggerPin2, OUTPUT);
  pinMode(EchoPin2, INPUT);
  
}
long duration, distance, rightdistance, leftdistance;

void loop() {
 potValue = distance;
  potValue = map(potValue, 0, 1023, 0, 180);
 SonarSensor(TriggerPin1, EchoPin1);
rightdistance= distance;
SonarSensor(TriggerPin2, EchoPin2);
leftdistance=distance;
Serial.print(rightdistance);
Serial.print("_");
Serial.println(leftdistance);
 
  
if   ((digitalRead(leftIr)==LOW)  && (digitalRead(rightIr)==LOW)){
  // PWM
  analogWrite(MotorSpeed, FORWARD_SPEED);

  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(87);

}

else if   ((digitalRead(leftIr)==HIGH) && (digitalRead(rightIr))==LOW){
  // PWM
  analogWrite(MotorSpeed, CURVE_SPEED);
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(42);
   delay(200);
}

else if  ((digitalRead(leftIr)==LOW) && ((digitalRead(rightIr)==HIGH))){
  // PWM
  analogWrite(MotorSpeed, CURVE_SPEED);
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(132);
   delay(200);
}

else if  ((digitalRead(leftIr)==HIGH) && (digitalRead(rightIr)==HIGH)){
  // PWM
  analogWrite(MotorSpeed, 0);
  // Direction
  digitalWrite(MotorP, LOW);
  digitalWrite(MotorN, LOW);
  myservo.write(87);

}

 
  if ((leftdistance <= 50)) //&& (rightdistance >80))
  {
     // PWM
  analogWrite(MotorSpeed, 0);
  // Direction
  digitalWrite(MotorP, LOW);
  digitalWrite(MotorN, LOW);
  myservo.write(87);
   // delay(500);
    
   // PWM
  analogWrite(MotorSpeed, CURVE_SPEED);
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(132);
    delay(2000);
    
    // PWM
  analogWrite(MotorSpeed, FORWARD_SPEED);

  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(87);
    delay(1500);
    
    // PWM
  analogWrite(MotorSpeed, CURVE_SPEED);
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(42);
     delay(2000);
    
      // PWM
  analogWrite(MotorSpeed, FORWARD_SPEED);

  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(87);
           Serial.println('leftdistance');
  }

   if ((rightdistance <=50)) //&& (leftdistance > 80))
  {
    // PWM
  analogWrite(MotorSpeed, 0);
  // Direction
  digitalWrite(MotorP, LOW);
  digitalWrite(MotorN, LOW);
  myservo.write(87);
    //delay(500);
    
           // PWM
  analogWrite(MotorSpeed, CURVE_SPEED);
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(42);
     delay(2000);
     
         //PMW
    analogWrite(MotorSpeed, FORWARD_SPEED);
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(87);
    delay(1500);
   
   analogWrite(MotorSpeed, CURVE_SPEED);
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(132);
     delay(2000);
     
      //PMW
    analogWrite(MotorSpeed, FORWARD_SPEED);
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(87);
            Serial.println('rightdistance');
  }

  if ((leftdistance <30)   && (rightdistance < 30))
  {
    // PWM
  analogWrite(MotorSpeed, 0);
  // Direction
  digitalWrite(MotorP, LOW);
  digitalWrite(MotorN, LOW);
    myservo.write(87);
  }


}
void SonarSensor(int TriggerPin, int EchoPin)
{
  digitalWrite(TriggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(TriggerPin, LOW);
  duration = pulseIn(EchoPin, HIGH);
  distance = (duration / 2) / 29.1;
  }

Can we get something clear here, please?

You are the one with the hardware.
You are the one with the hardware and software in front of you.
You are (probably the only) one with any interest in getting this thing working.

You are the one best-placed to debug your hardware and your software.

So, start making the debugging easier.

With some debug prints.
Read all the sensors ONCE at the top of loop.
Everywhere you make a decision, print the inputs to the decision-making process.
Double-check with a print for each branch.

And getting rid of the junk that doesn't do anything (I'm looking at you, "potValue")
Fix SonarSensor so that it returns a value,

And use the ctrl-T tool to format your code.

 Serial.print(rightdistance);
  Serial.print("_");
  Serial.println(leftdistance);

I don't know how your mind works, but printing the left value on the left, and the right value on the right seems to me to make more sense.

Serial.println('leftdistance'); Reminding you about this is getting tedious.

Hi,
Can I suggest in your code you don't keep doing digitalReads in if statements.
I suggest you read ALL your digital inputs at the beginning of the void loop(), store their value in bool variables.
Like you do the sonar distances and pot readings.

Then use those variables in your if statements.
That way you are taking a snapshot of ALL your inputs all at ONE time and acting apon it.
You can then Serial.print the IR values like you do the two distance values to help debug.

Tom.... :slight_smile: