[Help] Servo going mad when i add these command.

#include <Ultrasonic.h>
#include <Servo.h>
Ultrasonic ultrafront(12,11);   // (Trig PIN,Echo PIN)
Ultrasonic ultraleft(10,9);   // (Trig PIN,Echo PIN)
Ultrasonic ultraright(7,6);  // (Trig PIN,Echo PIN)
Servo myservo;

//motor one
const int DIR1 = 4;
const int EN1  = 5;
//motor two
const int DIR2 = 7;
const int EN2  = 6;

void setup() {


Serial.begin(9600);

pinMode (DIR2,OUTPUT);
pinMode (DIR1,OUTPUT);
pinMode (EN1,OUTPUT);
pinMode (EN2,OUTPUT);
myservo.attach(4);

}

void loop(){
 int duration, distance,pos=0,i;
 
 Serial.print("Front: ");
 Serial.print(ultrafront.Ranging(CM)); // CM or INC
 Serial.print(" cm     " );
 delay(100);
 Serial.print("Left: ");
 Serial.print(ultraleft.Ranging(CM)); // CM or INC
 Serial.print(" cm     " );
 delay(100);
 Serial.print("Right: ");
 Serial.print(ultraright.Ranging(CM)); // CM or INC
 Serial.print(" cm     " );
 delay(100);

int cm_L = ultraleft.Ranging(CM);
int cm_F = ultrafront.Ranging(CM);
int cm_R = ultraright.Ranging(CM);

//middle sensor (turning right)
//1
 if(cm_L >=45 && cm_F >=45 && cm_R >=45) {
   digitalWrite(DIR2, LOW);
   analogWrite(EN2, 100);
   digitalWrite(DIR1, LOW);
   analogWrite(EN1, 100);
   myservo.write(60);
 }
 //2
 else if (cm_L <45&&cm_F >=45&&cm_R  >=45){
   digitalWrite(DIR2, LOW); 
   analogWrite(EN2, 100);
   digitalWrite(DIR1, LOW);
   analogWrite(EN1, 0);
   myservo.write(105);
 }
 //3
 else if (cm_L <45&&cm_F <45&&cm_R >=45){
   digitalWrite(DIR2, LOW); 
   analogWrite(EN2, 80);
   digitalWrite(DIR1, HIGH);
   analogWrite(EN1, 100);
   myservo.write(120);
 
 }
 //4
 else if (cm_L>=45&&cm_F <45&&cm_R  <45){
   digitalWrite(DIR2, HIGH); 
   analogWrite(EN2, 100);
   digitalWrite(DIR1, LOW);
   analogWrite(EN1,80);
   myservo.write(0);
   

 }
 //5,servo-45
 else if (cm_L >=45&&cm_F>=45&&cm_R  <15){
   digitalWrite(DIR2, HIGH); 
   analogWrite(EN2, 100);
   digitalWrite(DIR1, LOW);
   analogWrite(EN1, 80);
   [color=red]myservo.write(0);[/color]
   
  
   

 }
 //6...
 else if (cm_L <45&&cm_F>=45&&cm_R <45){
   digitalWrite(DIR2, LOW); 
   analogWrite(EN2, 100);
   digitalWrite(DIR1, LOW);
   analogWrite(EN1, 100);

 }
 //7
 else if(cm_L <45&&cm_F <45&&cm_R  <45){
   digitalWrite(DIR2, HIGH); 
   analogWrite(EN2, 100);
   digitalWrite(DIR1, HIGH);
   analogWrite(EN1, 100);

 }
 //8
 else if (cm_L>=45&&cm_F <25&&cm_R  >=45){
   digitalWrite(DIR2, HIGH); 
   analogWrite(EN2, 100);
   digitalWrite(DIR1, HIGH);
   analogWrite(EN1, 100);
   

   
 }
   
 delay(100);



}

Please edit your post, select the program text and click on the code button (</>) top/left of the editor to put code tags on it to make it easier to deal with.

Which command(s) have you added and what does “Servo going mad” mean exactly ? What should it do ?

When The red-color highlighted sentence is added and uploaded. The servo horn goes right and left continuously(rocking), regardless of other servo commands.

I was expecting the servo rudder would move to the specific position.

Without being added. The servo will reacts just fine, accordingly to the input distance of the other servo command.

const int DIR1 = 4;
...

myservo.attach(4);

I haven't worked with servos for a while, but this looks wrong to me:

const int DIR1 = 4;

myservo.attach(4);

digitalWrite(DIR1, LOW);

[color=red]myservo.write(0);[/color]

DIR1 is pin 4, and servo is attached to pin 4, so a digitalWrite to pin 4, as well as myservo.write(0) sounds like a mistake to me.

DIR 4 is a fixed configuration for motor shield. and pin 4 is on the motor shield too, so i think there wont be any interference. How i know this is because the servo is functioning properly only if without adding the command that i've mentioned. However , i will try to change other pin. Any other suggestion?

Nsk55: DIR 4 is a fixed configuration for motor shield. and pin 4 is on the motor shield too

digitalWrite(pin, value)

writes to an Ardunio pin. So your "digitalWrite(DIR1, LOW);" writes 0V to Arduino pin 4. It is presumably the Arduino pin some component of the motor shield is attached to.

You can't also attach a servo to the same pin, so you need to check the pins on your shield. What is your shield model number, and how do you have your motors attached to your shield?

see:

https://www.arduino.cc/en/Reference/DigitalWrite