HC-SR04 ultrasound module with servo motor... ?

Hello, I am trying to make robot to move 5 seconds forward directions. before leaves it give a tone. and it check for obstacle in front and print in serial monitor. if there is no obstacle it continue. but and obstacle is there the robot will check for left side. for that I am using servo motor. and up to here all are working

but if it detect any obstacle in left side also then the system needs to check for wright side. it is not working and all system gets upset. I means it starts showing wrong value as distance.

please help me to make this....

and here is the codes I have used....

#define ECHOPIN 12 //Pin to receive echo pulse
#define TRIGPIN 13 //Pin to send trigger pulse

// Controlling a servo position using a potentiometer (variable resistor) 
// by Michal Rinott www.miracle.lk 

#include <Servo.h>
Servo servo_01;  // create servo object to control a servo(for Arm)
Servo servo_02;  // create servo object to control a servo(for mini sub)

int pos_01 = 0;
int pos_02 = 0;

int M1A=2; // for notors...
int M1B=3;
int M2A=4;
int M2B=5;
int M3A=6;
int M3B=7;
int M4A=8;
int M4B=9;

int siren_pin=A1; // for gives a tone 

int Magnet_pin=A2; // for make magnet..


void setup()
{
  
  Serial.begin(9600);
  
pinMode(ECHOPIN,INPUT);
pinMode(TRIGPIN,OUTPUT);
  
pinMode(M1A,OUTPUT);
pinMode(M1B,OUTPUT);
pinMode(M2A,OUTPUT);
pinMode(M2B,OUTPUT);
pinMode(M3A,OUTPUT);
pinMode(M3B,OUTPUT);
pinMode(M4A,OUTPUT);
pinMode(M4B,OUTPUT);
  
  servo_01.attach(10);// attaches the servo on pin 9 to the servo object
  servo_02.attach(11);// attaches the servo on pin 9 to the servo object
  
  
}

void loop()
{
  
  //Starting Ranging - Generating a trigger of 10us burst
  digitalWrite(TRIGPIN,LOW);
  delay(2);
  digitalWrite(TRIGPIN,HIGH);
  delay(10);
  digitalWrite(TRIGPIN,LOW);
  
  //Distance Calculation
  
  float distance = pulseIn(ECHOPIN,HIGH);
  distance = distance/58;
  Serial.print(distance);
  Serial.println("cm distance 01");
  delay(20);
 
  if(distance>50){
  
  for(int i=0;i<2;i++){
  for(int k=20;k<100;k++)
               {
                  tone(siren_pin,10*k,300);
                  delay(50);                          // wait for a second
                  
               }
               
            for(int j=120;j>20;j--)
               {
                  tone(siren_pin,10*j,300);
                  delay(50);                          // wait for a second
                  
               }   
  }
        digitalWrite(M1A,HIGH);  // move forward...
        digitalWrite(M1B,LOW);
        digitalWrite(M2A,LOW);
        digitalWrite(M2B,HIGH);
        delay(5000);            //2 seconds...
        
        digitalWrite(M1A,LOW);    // stop ...
        digitalWrite(M1B,LOW);
        digitalWrite(M2A,LOW);
        digitalWrite(M2B,LOW);
        delay(1000);
        
    for(pos_01 = 90; pos_01 < 180; pos_01 += 1)   // goes from 90 degrees to 180 degrees 
  {                                            // in steps of 1 degree 
    servo_01.write(pos_01);                    // tell servo to go to position in variable 'pos' 
    delay(15);                                 // waits 15ms for the servo to reach the position 
  } 
        
        analogWrite(Magnet_pin,220);   //turn magnet on...
        
        digitalWrite(M3A,HIGH);    // megnet down...
        digitalWrite(M3B,LOW);
        delay(3000);
  
        digitalWrite(M3A,LOW);    // megnet down...
        digitalWrite(M3B,LOW);
        delay(5000);
      
        
        digitalWrite(M3A,LOW);    //megnet up...
        digitalWrite(M3B,HIGH);
        delay(3000);
        
        digitalWrite(M3A,LOW);
        digitalWrite(M3B,LOW);
        
    for(pos_01 = 180; pos_01>=90; pos_01-=1)     // goes from 180 degrees to 90 degrees 
  {                                
    servo_01.write(pos_01);                  // tell servo to go to position in variable 'pos' 
    delay(15);                               // waits 15ms for the servo to reach the position 
  } 
    
     delay(1000);
     
analogWrite(Magnet_pin,0);   //turn magnet off.       
   
   delay(1000);
   
     for(pos_01 = 90; pos_01>=1; pos_01-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    servo_01.write(pos_01);                  // tell servo to go to position in variable 'pos' 
    delay(15);                               // waits 15ms for the servo to reach the position 
  } 
  delay(1000);
  
        digitalWrite(M3A,HIGH);    // megnet down...
        digitalWrite(M3B,LOW);
        delay(3000);
  
        digitalWrite(M3A,LOW);    // megnet down...
        digitalWrite(M3B,LOW);
        delay(5000);
      
        
        digitalWrite(M3A,LOW);    //megnet up...
        digitalWrite(M3B,HIGH);
        delay(3000);
        
        digitalWrite(M3A,LOW);
        digitalWrite(M3B,LOW);
        
    for(pos_01 = 1; pos_01 < 90; pos_01 += 1)   // goes from 0 degrees to 180 degrees 
  {                                            // in steps of 1 degree 
    servo_01.write(pos_01);                    // tell servo to go to position in variable 'pos' 
    delay(15);                                 // waits 15ms for the servo to reach the position 
  } 
        
        delay(1000);
        analogWrite(Magnet_pin,0);   //turn magnet off        
        delay(1000);  
  }
  
  
  
  
  
  else {
    if(distance<50)
    {
    tone(siren_pin,512,300);
    delay(200);
    
    for(pos_02 = 90; pos_02>=0; pos_02-=10)     // goes from 180 degrees to 0 degrees 
  {                                
    servo_02.write(pos_02);                  // tell servo to go to position in variable 'pos' 
    delay(15);                               // waits 15ms for the servo to reach the position 
  } 
    
    //Starting Ranging - Generating a trigger of 10us burst
  digitalWrite(TRIGPIN,LOW);
  delay(2);
  digitalWrite(TRIGPIN,HIGH);
  delay(10);
  digitalWrite(TRIGPIN,LOW);
  
  float distance02 = pulseIn(ECHOPIN,HIGH);
  distance02 = distance02/58;
  Serial.print(distance02);
  Serial.println("cm distance 02");
  delay(20);
  
  
  if(distance02>50)
    {
        digitalWrite(M1A,HIGH);  // move forward...
        digitalWrite(M1B,LOW);
        digitalWrite(M2A,HIGH);
        digitalWrite(M2B,LOW);
        delay(2500);            //2 seconds...
        
        digitalWrite(M1A,LOW);    // stop ...
        digitalWrite(M1B,LOW);
        digitalWrite(M2A,LOW);
        digitalWrite(M2B,LOW);
        delay(1000);
    
    }
  
    
    else
    {
      if(distance02<50)
      {
        
        for(pos_02 = 90; pos_02<=180; pos_02+=10)     // goes from 90 degrees to 180 degrees 
          {                                
            servo_02.write(pos_02);                  // tell servo to go to position in variable 'pos_02' 
            delay(15);                               // waits 15ms for the servo to reach the position 
          } 
        
      }
    }
  
  }  
  }
 
 
}
or(pos_02 = 90; pos_02<=180; pos_02+=10)     // goes from 180 degrees to 0 degrees

No, it clearly doesn't.

Any good reason for not using code tags?

thank you for replying me...
sorry, I am new to this. I don't know how to use it . can you tell me. or give some example ...

Your code would be easier to read if you used auto format on it before posting it between code tags.
You've got nested for loops both controlled by variables called 'i'. This is often a recipe for disaster.
Lose the comments that are clearly incorrect.

oh! next time I will post code in tags only. :roll_eyes:

if I use difference variable instead of i will it solve my problem ??

mmm yes , comments are wrong. I just copy it from above lines only. sorry for that..

next time I will post code in tags only

It's not too late to go back and fix the post. Running auto format on it first, of course.

I have no idea if fixing the loop indices will fix things, but then I have no idea what "all system gets upset" means either

I just corrected code tag.

all system gets upset - I means after the servo motor turns left, the distance readings gives some random value or some values that are not correct.

and it will not comes back to initial loop again, I have to reset the Arduino board to starts from beginning.

I just corrected code tag.

But you didn't correct the issues that have been pointed out in your code. Why not? It makes it seem like you don't really want help.

sorry, I corrected it. but still it doesn't solve my problem ...

, I corrected it.

Apart from the silly comments and the Dadaist indentation.

Why not try some debug prints, to find what your code is really doing?

Lose the comments that are clearly incorrect.

You couldn't be bothered.

You couldn't be bothered using Tools + Auto Format. You couldn't be bothered to remove the useless random blank lines in the code.

You could not be bothered to add meaningful comments, like

// If the distance is less then 50, turn left

anywhere.

You couldn't be bothered to CONSISTENTLY place { on new lines (my preference) or on the same line as the statement (ugh!).

    analogWrite(Magnet_pin,220);   //turn magnet on...

No Arduino supports PWM on analog pins.

if(distance>50)
{
}
else
{
if(distance<50)
{
So, what happens if distance equals 50?

      //Starting Ranging - Generating a trigger of 10us burst
      digitalWrite(TRIGPIN,LOW);
      delay(2);
      digitalWrite(TRIGPIN,HIGH);
      delay(10);
      digitalWrite(TRIGPIN,LOW);

That's not what this code does.

Where is your serial output?