Please help me out with the code on obstacle detecting robo-car!!

Hi,

I am trying to build an obstacle detecting robocar with the help of arduino uno. The other components used are an H-bridge with L298-N motor driver, a servo motor and HC SR-04(UV sensor). On powering the system, The car keeps on rotating in a circle and am failing to detect the fault in the code. This is the code

#include <Servo.h>
int motorpin1=7;
int motorpin2=6;
int motorpin3=4;
int motorpin4=5;
int servopin=11;
int trigpin=12;
int echopin=3;
int ena=9;
int enb=10;
long duration;
int distance=0;
int leftdist=0;
int rightdist=0;
int object=10;
Servo myservo;
void setup()
{
  myservo.attach(11);
  pinMode(motorpin1,OUTPUT);
  pinMode(motorpin2,OUTPUT);
  pinMode(motorpin3,OUTPUT);
  pinMode(motorpin4,OUTPUT);
  pinMode(trigpin,OUTPUT);
  pinMode(echopin,OUTPUT); 
  pinMode(ena,OUTPUT);
  pinMode(enb,OUTPUT); 
}

void loop()
{
  myservo.write(115);
  delay(700);
  analogWrite(ena,255);
  analogWrite(enb,255);
  delay(50);
  distance=readping();
  if(distance<=20)
  {
    movestop();
    delay(300);
    backward();
    delay(400);
    movestop();
    delay(300);
    rightdist=lookright();
    delay(300);
    leftdist=lookleft();
     delay(300);
     if(distance>leftdist)
     {
      turnright();
      movestop(); 
     }
     else
     {
      turnleft();
      movestop(); 
     }
    
  }
  else
  {
    forward();  
  }
  distance=readping();
}

void forward()
{
  digitalWrite(motorpin1,HIGH);
  digitalWrite(motorpin2,LOW);
  digitalWrite(motorpin3,HIGH);
  digitalWrite(motorpin4,LOW);  
}
void movestop()
{
  digitalWrite(motorpin1,LOW);
  digitalWrite(motorpin2,LOW);
  digitalWrite(motorpin3,LOW);
  digitalWrite(motorpin4,LOW);  
}
void backward()
{
  digitalWrite(motorpin1,LOW);
  digitalWrite(motorpin2,LOW);
  digitalWrite(motorpin3,HIGH);
  digitalWrite(motorpin4,HIGH);
}
void turnright()
{
  digitalWrite(motorpin1,LOW);
  digitalWrite(motorpin2,HIGH);
  digitalWrite(motorpin3,LOW);
  digitalWrite(motorpin4,HIGH);
  delay(500);
  digitalWrite(motorpin1,HIGH);
  digitalWrite(motorpin2,LOW);
  digitalWrite(motorpin3,HIGH);
  digitalWrite(motorpin4,LOW);  
}
void turnleft()
{
  digitalWrite(motorpin1,HIGH);
  digitalWrite(motorpin2,LOW);
  digitalWrite(motorpin3,LOW);
  digitalWrite(motorpin4,HIGH);
  delay(500);
  digitalWrite(motorpin1,HIGH);
  digitalWrite(motorpin2,LOW);
  digitalWrite(motorpin3,HIGH);
  digitalWrite(motorpin4,LOW);    
}
int lookright()
{
  myservo.write(50);
  delay(500);
  int distance=readping();
  delay(100);
  myservo.write(115);
  return distance;
  delay(100);  
}
int lookleft()
{
  myservo.write(170);
  delay(500);
  int distance=readping();
  delay(100);
  myservo.write(115);
  return distance;
  delay(100);  
}
int readping()
{
  digitalWrite(trigpin,LOW);
  delayMicroseconds(2);
  digitalWrite(trigpin,HIGH);
  delayMicroseconds(10);
  digitalWrite(trigpin,LOW);
  duration=pulseIn(echopin,HIGH);
  distance=duration*.034/2;
  return distance;
}

Please help me in detecting any sort of fault so that i can proceed with my project.
Any kind of help would be appreciated
Thank You!!

What do your debug prints tell you?
Oh, wait . . .

Please remember to use code tags when posting code

putting any kind of code after the return statement does nothing because the function has... well... returned to the place that called it.

 return distance;
  delay(100);

probably not your actual problem, just FYI

I’m not going to study your code until you put it in code tags using the code </> button

However I see widespread use of delay() and that will prevent your program from being responsive. Have a look at how millis() is used to manage timing without blocking in Several things at a time

…R