joystick controlled obstacle avoiding robot

hello, i am doing project of obstacle avoiding robot in which dc motors are controlled by using joystick. i had completed the controlling part of dc motors using joystick but when i used ultrasonic sensor with it, it is not operating in all direction(left, right, forward, backward) when obstacle is not detected, it should move in all direction but it moves in only one axis, for example it either moves in forward and backward or left and right direction. also there is one more thing when obstacle is detected, it should not move forward but it should move in backward, here also it only moves in only left and right direction. i think i am missing some logic in code. i would appreciate your advice.

the code i am using is mentioned below

#define echopin 2 // echo pin
#define trigpin 3 // Trigger pin
int maximumRange = 30;
long duration, distance;
const byte joyStickPin1 = A1;
const byte joyStickPin2 = A0;
const byte motorSpeedPin1 = 11;
const byte motorDirPin1 = 13;
const byte motorSpeedPin2 = 5;
const byte motorDirPin2 = 12;
//Joystick input variables
int joyValue = 0;
int joyValueMax = 1023;
int joyValueMin = 0;
int joyValueMid = 512;
int joyValueMidUpper = joyValueMid + 20;
int joyValueMidLower = joyValueMid - 20;

//DC motor variables
byte motorSpeed = 0;
byte motorSpeedMax = 255;
byte motorSpeedMin = 0; //set to smallest value that make motor move (default 0)
                        // DC motor that I use start to move at 90 pwm value

void setup()
{
   Serial.begin (9600);
   pinMode(trigpin, OUTPUT);
   pinMode(echopin, INPUT );
   pinMode(joyStickPin1, INPUT);
   pinMode(motorSpeedPin1, OUTPUT);
   pinMode(motorDirPin1, OUTPUT);
   pinMode(joyStickPin2, INPUT);
   pinMode(motorSpeedPin2, OUTPUT);
   pinMode(motorDirPin2, OUTPUT);
}

void loop()
{
 {
     digitalWrite(trigpin,LOW);
     delayMicroseconds(2);
     digitalWrite(trigpin,HIGH);
     delayMicroseconds(10);
     duration=pulseIn (echopin,HIGH);
     distance= duration/58.2;
     delay (50);
     Serial.println(distance);
 }


 if (distance >= 20)
 {

   
{
   joyValue = analogRead(joyStickPin2);
 
   if(joyValue > joyValueMidUpper) //left
   {
       motorSpeed = map(joyValue, joyValueMidUpper, joyValueMax, motorSpeedMin, motorSpeedMax);
       MotorLeft(motorSpeed);
   }
   else if(joyValue < joyValueMidLower) //front
   {
       motorSpeed = map(joyValue, joyValueMidLower, joyValueMin, motorSpeedMin, motorSpeedMax);
       MotorRight(motorSpeed);
   }

    else
   {
      MotorStop();
   }
}

{
  joyValue = analogRead(joyStickPin1);
 
   if(joyValue > joyValueMidUpper) //Forward
   {
       motorSpeed = map(joyValue, joyValueMidUpper, joyValueMax, motorSpeedMin, motorSpeedMax);
       MotorForward(motorSpeed);
   }
   else if(joyValue < joyValueMidLower) //Backward
   {
       motorSpeed = map(joyValue, joyValueMidLower, joyValueMin, motorSpeedMin, motorSpeedMax);
       MotorBackward(motorSpeed);
   }
   
   else
   {
      MotorStop();
 }
}
 
 }
 else if (distance < 20)
{
   joyValue = analogRead(joyStickPin2);
 
   if(joyValue > joyValueMidUpper) //left
   {
       motorSpeed = map(joyValue, joyValueMidUpper, joyValueMax, motorSpeedMin, motorSpeedMax);
       MotorLeft(motorSpeed);
   }
   else if(joyValue < joyValueMidLower) //right
   {
       motorSpeed = map(joyValue, joyValueMidLower, joyValueMin, motorSpeedMin, motorSpeedMax);
       MotorRight(motorSpeed);
   }
   
   else
   {
      MotorStop();
   }
   joyValue = analogRead(joyStickPin1);
   if(joyValue < joyValueMidLower) //Backward
   {
       motorSpeed = map(joyValue, joyValueMidLower, joyValueMin, motorSpeedMin, motorSpeedMax);
       MotorBackward(motorSpeed);
   }
    else
   {
      MotorStop();
   }


   
}
 
   
}



void MotorForward( byte Spd)
{
   digitalWrite(motorDirPin1, HIGH);
   analogWrite(motorSpeedPin1, Spd);
   digitalWrite(motorDirPin2, HIGH);
   analogWrite(motorSpeedPin2, Spd);
}

void MotorBackward( byte Spd)
{
   digitalWrite(motorDirPin1, LOW);
   analogWrite(motorSpeedPin1, Spd);
   digitalWrite(motorDirPin2, LOW);
   analogWrite(motorSpeedPin2, Spd);
}
void MotorLeft( byte Spd)
{
   analogWrite(motorSpeedPin2, 0);
   digitalWrite(motorDirPin1, LOW);
   analogWrite(motorSpeedPin1, Spd);
}

void MotorRight( byte Spd)
{
   analogWrite(motorSpeedPin1, 0);
   digitalWrite(motorDirPin2, LOW);
   analogWrite(motorSpeedPin2, Spd);
   
}

void MotorStop()
{
   analogWrite(motorSpeedPin1, 0);
   analogWrite(motorSpeedPin2, 0);  
}

Your code is littered with useless curly braces. Learn which ones are needed. Keep them. Get rid of the rest.

Analog pins are input only. You do not need to, and should not, set the mode of analog pins.

What do your Serial.print() statements tell you is happening?

Yes I will change the curly braces and statement but I think something is wrong in code of these statements if (distance >= 20) and else if (distance<20) as it only reads one either joyValue = analogRead(joyStickPin1); or joyValue = analogRead(joyStickPin2);

When you've eliminated the superfluous braces, post your code (use code tags this time) and your observations.

Hint: if you've tested a variable to see if is greater than or equal to 20, and it turns out not to be, you don't need to test it again to see if it is less than 20.

#define echopin 2 // echo pin
#define trigpin 3 // Trigger pin
int maximumRange = 30;
long duration, distance;
const byte joyStickPin1 = A1;
const byte joyStickPin2 = A0;
const byte motorSpeedPin1 = 11;
const byte motorDirPin1 = 13;
const byte motorSpeedPin2 = 5;
const byte motorDirPin2 = 12;
//Joystick input variables
int joyValue = 0;
int joyValueMax = 1023;
int joyValueMin = 0;
int joyValueMid = 512;
int joyValueMidUpper = joyValueMid + 20;
int joyValueMidLower = joyValueMid - 20;

//DC motor variables
byte motorSpeed = 0;
byte motorSpeedMax = 255;
byte motorSpeedMin = 0;

void setup()
{
   Serial.begin (9600);
   pinMode(trigpin, OUTPUT);
   pinMode(echopin, INPUT );
   pinMode(joyStickPin1, INPUT);
   pinMode(motorSpeedPin1, OUTPUT);
   pinMode(motorDirPin1, OUTPUT);
   pinMode(joyStickPin2, INPUT);
   pinMode(motorSpeedPin2, OUTPUT);
   pinMode(motorDirPin2, OUTPUT);
}

void loop()
{
 {
     digitalWrite(trigpin,LOW);
     delayMicroseconds(2);
     digitalWrite(trigpin,HIGH);
     delayMicroseconds(10);
     duration=pulseIn (echopin,HIGH);
     distance= duration/58.2;
     delay (50);
     Serial.println(distance);
 }


 if (distance >= 20)
//i am facing problem here, it only works for either x-axis(left, right) or y-axis(forward, backward) but i want my robot to operate in all direction in this case as no obstacle is detected here.
 
{

 
   joyValue = analogRead(joyStickPin2);  //x-axis of joystick
 
   if(joyValue > joyValueMidUpper) //left
   {
       motorSpeed = map(joyValue, joyValueMidUpper, joyValueMax, motorSpeedMin, motorSpeedMax);
       MotorLeft(motorSpeed);
   }
   else if(joyValue < joyValueMidLower) //right
   {
       motorSpeed = map(joyValue, joyValueMidLower, joyValueMin, motorSpeedMin, motorSpeedMax);
       MotorRight(motorSpeed);
   }

    else
   {
      MotorStop();
   }



  joyValue = analogRead(joyStickPin1);  //y-axis of joystick
 
   if(joyValue > joyValueMidUpper) //Forward
   {
       motorSpeed = map(joyValue, joyValueMidUpper, joyValueMax, motorSpeedMin, motorSpeedMax);
       MotorForward(motorSpeed);
   }
   else if(joyValue < joyValueMidLower) //Backward
   {
       motorSpeed = map(joyValue, joyValueMidLower, joyValueMin, motorSpeedMin, motorSpeedMax);
       MotorBackward(motorSpeed);
   }
 
   else
   {
      MotorStop();
  }


 
 }
 


else if (distance < 20)      

//this condition works perfectly fine, whenever obstacle comes in less than 20 distance, it only moves in left or right. this condition works as it should be.


{
   

   joyValue = analogRead(joyStickPin2);
 
   if(joyValue > joyValueMidUpper) //left
   {
       motorSpeed = map(joyValue, joyValueMidUpper, joyValueMax, motorSpeedMin, motorSpeedMax);
       MotorLeft(motorSpeed);
   }
   else if(joyValue < joyValueMidLower) //right
   {
       motorSpeed = map(joyValue, joyValueMidLower, joyValueMin, motorSpeedMin, motorSpeedMax);
       MotorRight(motorSpeed);
   }
 
   else
   {
      MotorStop();
   }
 

 
}
 
 
}



void MotorForward( byte Spd)
{
   digitalWrite(motorDirPin1, HIGH);
   analogWrite(motorSpeedPin1, Spd);
   digitalWrite(motorDirPin2, HIGH);
   analogWrite(motorSpeedPin2, Spd);
}

void MotorBackward( byte Spd)
{
   digitalWrite(motorDirPin1, LOW);
   analogWrite(motorSpeedPin1, Spd);
   digitalWrite(motorDirPin2, LOW);
   analogWrite(motorSpeedPin2, Spd);
}
void MotorLeft( byte Spd)
{
   analogWrite(motorSpeedPin2, 0);
   digitalWrite(motorDirPin1, LOW);
   analogWrite(motorSpeedPin1, Spd);
}

void MotorRight( byte Spd)
{
   analogWrite(motorSpeedPin1, 0);
   digitalWrite(motorDirPin2, LOW);
   analogWrite(motorSpeedPin2, Spd);
 
}

void MotorStop()
{
   analogWrite(motorSpeedPin1, 0);
   analogWrite(motorSpeedPin2, 0);
}
[

(Get with the program! Use the code tags button </> when posting)

void loop()
{
  {

Still with the useless curly braces...

Still no code tags...

i want my robot to operate in all direction in this case as no obstacle is detected here.

You read the x axis and start the robot doing something. Then, you read the y axis, and change what the robot is doing. Does that make sense?

You need to read both axes first, and decide what to do based on BOTH values.

   digitalWrite(trigpin,LOW);
     delayMicroseconds(2);
     digitalWrite(trigpin,HIGH);
     delayMicroseconds(10);
     duration=pulseIn (echopin,HIGH);
     distance= duration/58.2;
     delay (50);
     Serial.println(distance);

Why the delay before the pulseIn?
Did you miss something there?
I'd set trigpin LOW in setup(), after setting the pinMode.
(Actually, I'd put the ranging stuff in its own function)

Dear Pauls, I got the point now but how to code if I want to read both axis first and then perform the action by robot accordingly.
Sir, I am new here in this forum as well as in programming Arduino, I am just trying hard to get things done. I apologize if my question disappointed you in any way.

how to code if I want to read both axis first and then perform the action by robot accordingly.

   int xVal = analogRead(xPin);
   int yVal = analogRead(yPin);

   if(xVal > 518)
   {
      // Stick was pushed forward
      if(yVal > 518)
      {
         // and right
         MoveForwardRight();
      }
      else if(yVal < 506)
      {
         // and left
         MoveForwardLeft();
      }
      else
      {
         MoveForward();
      }
   }

You need to handle the additional cases where the stick is pulled back and to the left, the right, or straight back, as well as the cases where the stick is pushed straight left or right and where the stick is centered.

still not working..

#define echopin 2 // echo pin
#define trigpin 3 // Trigger pin
int maximumRange = 30;
long duration, distance;
const byte joyStickPin1 = A1;
const byte joyStickPin2 = A0;
const byte motorSpeedPin1 = 11;
const byte motorDirPin1 = 13;
const byte motorSpeedPin2 = 5;
const byte motorDirPin2 = 12;
//Joystick input variables
int joyValue1 = 0;
int joyValue2 = 0;
int joyValueMax = 1023;
int joyValueMin = 0;
int joyValueMid = 512;
int joyValueMidUpper = joyValueMid + 20;
int joyValueMidLower = joyValueMid - 20;

//DC motor variables
byte motorSpeed = 0;
byte motorSpeedMax = 255;
byte motorSpeedMin = 0;

void setup()
{
Serial.begin (9600);
pinMode(trigpin, OUTPUT);
pinMode(echopin, INPUT );
pinMode(joyStickPin1, INPUT);
pinMode(motorSpeedPin1, OUTPUT);
pinMode(motorDirPin1, OUTPUT);
pinMode(joyStickPin2, INPUT);
pinMode(motorSpeedPin2, OUTPUT);
pinMode(motorDirPin2, OUTPUT);
}

void loop()
{
{
digitalWrite(trigpin,LOW);
delayMicroseconds(2);
digitalWrite(trigpin,HIGH);
delayMicroseconds(10);
duration=pulseIn (echopin,HIGH);
distance= duration/58.2;
delay (50);

joyValue1 = analogRead(joyStickPin1); //y-axis of joystick
joyValue2 = analogRead(joyStickPin2); //x-axis of joystick
}
if (distance >= 20)
{

if(joyValue2 > joyValueMidUpper) //left
{
motorSpeed = map(joyValue2, joyValueMidUpper, joyValueMax, motorSpeedMin, motorSpeedMax);
MotorLeft(motorSpeed);
}

else if(joyValue2 < joyValueMidLower) //right
{
motorSpeed = map(joyValue2, joyValueMidLower, joyValueMin, motorSpeedMin, motorSpeedMax);
MotorRight(motorSpeed);
}

else
{
MotorStop();
}

if(joyValue1 > joyValueMidUpper) //Forward
{
motorSpeed = map(joyValue1, joyValueMidUpper, joyValueMax, motorSpeedMin, motorSpeedMax);
MotorForward(motorSpeed);
}
else if(joyValue1 < joyValueMidLower) //Backward
{
motorSpeed = map(joyValue1, joyValueMidLower, joyValueMin, motorSpeedMin, motorSpeedMax);
MotorBackward(motorSpeed);
}

else
{
MotorStop();
}
}

else if (distance < 20)

{
if(joyValue2 > joyValueMidUpper) //left
{
motorSpeed = map(joyValue2, joyValueMidUpper, joyValueMax, motorSpeedMin, motorSpeedMax);
MotorLeft(motorSpeed);
}
else if(joyValue2 < joyValueMidLower) //right
{
motorSpeed = map(joyValue2, joyValueMidLower, joyValueMin, motorSpeedMin, motorSpeedMax);
MotorRight(motorSpeed);
}

else
{
MotorStop();
}
}
}

void MotorForward( byte Spd)
{
digitalWrite(motorDirPin1, HIGH);
analogWrite(motorSpeedPin1, Spd);
digitalWrite(motorDirPin2, HIGH);
analogWrite(motorSpeedPin2, Spd);
}

void MotorBackward( byte Spd)
{
digitalWrite(motorDirPin1, LOW);
analogWrite(motorSpeedPin1, Spd);
digitalWrite(motorDirPin2, LOW);
analogWrite(motorSpeedPin2, Spd);
}
void MotorLeft( byte Spd)
{
analogWrite(motorSpeedPin2, 0);
digitalWrite(motorDirPin1, LOW);
analogWrite(motorSpeedPin1, Spd);
}

void MotorRight( byte Spd)
{
analogWrite(motorSpeedPin1, 0);
digitalWrite(motorDirPin2, LOW);
analogWrite(motorSpeedPin2, Spd);

}

void MotorStop()
{
analogWrite(motorSpeedPin1, 0);
analogWrite(motorSpeedPin2, 0);
}

Still no code tags.
Still no explanation of what "not working" means.
Still too many braces.

else if (distance < 20)  Still ignoring what you've already been told.

dear amol, i have mentioned few things. please do have a look once

(Please start using the Code Tab button, </>, on the menu. Thanks, Moderator)

#define echopin 2 // echo pin
#define trigpin 3 // Trigger pin
int maximumRange = 30;
long duration, distance;
const byte joyStickPin1 = A1;
const byte joyStickPin2 = A0;
const byte motorSpeedPin1 = 11;
const byte motorDirPin1 = 13;
const byte motorSpeedPin2 = 5;
const byte motorDirPin2 = 12;
//Joystick input variables
int joyValue1 = 0;
int joyValue2 = 0;
int joyValueMax = 1023;
int joyValueMin = 0;
int joyValueMid = 512;
int joyValueMidUpper = joyValueMid + 20;
int joyValueMidLower = joyValueMid - 20;

//DC motor variables
byte motorSpeed = 0;
byte motorSpeedMax = 255;
byte motorSpeedMin = 0;

void setup()
{
  Serial.begin (9600);
  pinMode(trigpin, OUTPUT);
  pinMode(echopin, INPUT );
  pinMode(joyStickPin1, INPUT);
  pinMode(motorSpeedPin1, OUTPUT);
  pinMode(motorDirPin1, OUTPUT);
  pinMode(joyStickPin2, INPUT);
  pinMode(motorSpeedPin2, OUTPUT);
  pinMode(motorDirPin2, OUTPUT);
}

void loop()
{
{
    digitalWrite(trigpin,LOW);
    delayMicroseconds(2);
    digitalWrite(trigpin,HIGH);
    delayMicroseconds(10);
    duration=pulseIn (echopin,HIGH);
    distance= duration/58.2;
    delay (50);
   
    joyValue1 = analogRead(joyStickPin1);  //y-axis of joystick
    joyValue2 = analogRead(joyStickPin2);  //x-axis of joystick
}
if (distance >= 20)
{
  //here sir, only this doesn't work robot is not operating in left and right direction, in this condition if (distance >= 20) i want robot in move in all 4 direction but it only goes forward and backward, when i remove the code of forward and backward then only it moves in left and right. it only reads either joystickpin1 or 1 but not both simultaneously. this is the only prob i am facing AWOL


  if(joyValue2 > joyValueMidUpper) //left
   {
       motorSpeed = map(joyValue2, joyValueMidUpper, joyValueMax, motorSpeedMin, motorSpeedMax);
       MotorLeft(motorSpeed);
   }
 
   else if(joyValue2 < joyValueMidLower) //right
   {
       motorSpeed = map(joyValue2, joyValueMidLower, joyValueMin, motorSpeedMin, motorSpeedMax);
       MotorRight(motorSpeed);
   }
 
   else
   {
      MotorStop();
   }

//here, this part is also working when obstacle is not detected at the distance of 20, it moves in forward and backward direction but the above code doesn't work.
 
  if(joyValue1 > joyValueMidUpper) //Forward
   {
       motorSpeed = map(joyValue1, joyValueMidUpper, joyValueMax, motorSpeedMin, motorSpeedMax);
       MotorForward(motorSpeed);
   }
   else if(joyValue1 < joyValueMidLower) //Backward
   {
       motorSpeed = map(joyValue1, joyValueMidLower, joyValueMin, motorSpeedMin, motorSpeedMax);
       MotorBackward(motorSpeed);
   }
 
   else
   {
     MotorStop();
   }
 }


else if (distance < 20)     //sir, this works perfectly fine when distance is less than 20, obstacle is detected and robot operate same as the mentioned code below.  

{
  if(joyValue2 > joyValueMidUpper) //left
  {
      motorSpeed = map(joyValue2, joyValueMidUpper, joyValueMax, motorSpeedMin, motorSpeedMax);
      MotorLeft(motorSpeed);
  }
  else if(joyValue2 < joyValueMidLower) //right
  {
      motorSpeed = map(joyValue2, joyValueMidLower, joyValueMin, motorSpeedMin, motorSpeedMax);
      MotorRight(motorSpeed);
  }

  else
  {
     MotorStop();
  }
}
}

void MotorForward( byte Spd)
{
  digitalWrite(motorDirPin1, HIGH);
  analogWrite(motorSpeedPin1, Spd);
  digitalWrite(motorDirPin2, HIGH);
  analogWrite(motorSpeedPin2, Spd);
}

void MotorBackward( byte Spd)
{
  digitalWrite(motorDirPin1, LOW);
  analogWrite(motorSpeedPin1, Spd);
  digitalWrite(motorDirPin2, LOW);
  analogWrite(motorSpeedPin2, Spd);
}
void MotorLeft( byte Spd)
{
  analogWrite(motorSpeedPin2, 0);
  digitalWrite(motorDirPin1, LOW);
  analogWrite(motorSpeedPin1, Spd);
}

void MotorRight( byte Spd)
{
  analogWrite(motorSpeedPin1, 0);
  digitalWrite(motorDirPin2, LOW);
  analogWrite(motorSpeedPin2, Spd);

}

void MotorStop()
{
  analogWrite(motorSpeedPin1, 0);
  analogWrite(motorSpeedPin2, 0);
}

Use CTRL-T in the IDE to format your code, will help with seeing the problems you seem to have with matching up the {s and }s.

Auto format done, still the same output. :frowning:

But you haven't posted your auto-formatted code.
It strikes me as quite sad that I need to point this out.

#define echopin 2 // echo pin
#define trigpin 3 // Trigger pin
int maximumRange = 30;
long duration, distance;
const byte joyStickPin1 = A1;
const byte joyStickPin2 = A0;
const byte motorSpeedPin1 = 11;
const byte motorDirPin1 = 13;
const byte motorSpeedPin2 = 5;
const byte motorDirPin2 = 12;
//Joystick input variables
int joyValue1 = 0;
int joyValue2 = 0;
int joyValueMax = 1023;
int joyValueMin = 0;
int joyValueMid = 512;
int joyValueMidUpper = joyValueMid + 20;
int joyValueMidLower = joyValueMid - 20;

//DC motor variables
byte motorSpeed = 0;
byte motorSpeedMax = 255;
byte motorSpeedMin = 0;

void setup()
{
  Serial.begin (9600);
  pinMode(trigpin, OUTPUT);
  pinMode(echopin, INPUT );
  pinMode(joyStickPin1, INPUT);
  pinMode(motorSpeedPin1, OUTPUT);
  pinMode(motorDirPin1, OUTPUT);
  pinMode(joyStickPin2, INPUT);
  pinMode(motorSpeedPin2, OUTPUT);
  pinMode(motorDirPin2, OUTPUT);
}

void loop()
{
  {
    digitalWrite(trigpin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigpin, HIGH);
    delayMicroseconds(10);
    duration = pulseIn (echopin, HIGH);
    distance = duration / 58.2;
    delay (50);

    joyValue1 = analogRead(joyStickPin1);  //y-axis of joystick
    joyValue2 = analogRead(joyStickPin2);  //x-axis of joystick
  }
  if (distance >= 20)
  {

    if (joyValue1 > joyValueMidUpper) //Forward
    {
      motorSpeed = map(joyValue1, joyValueMidUpper, joyValueMax, motorSpeedMin, motorSpeedMax);
      MotorForward(motorSpeed);
    }
    else if (joyValue1 < joyValueMidLower) //Backward
    {
      motorSpeed = map(joyValue1, joyValueMidLower, joyValueMin, motorSpeedMin, motorSpeedMax);
      MotorBackward(motorSpeed);
    }

    else
    {
      MotorStop();
    }
  }



  else if (distance < 20)

  {
    if (joyValue2 > joyValueMidUpper) //left
    {
      motorSpeed = map(joyValue2, joyValueMidUpper, joyValueMax, motorSpeedMin, motorSpeedMax);
      MotorLeft(motorSpeed);
    }
    else if (joyValue2 < joyValueMidLower) //right
    {
      motorSpeed = map(joyValue2, joyValueMidLower, joyValueMin, motorSpeedMin, motorSpeedMax);
      MotorRight(motorSpeed);
    }

    else
    {
      MotorStop();
    }
  }

}



void MotorForward( byte Spd)
{
  digitalWrite(motorDirPin1, HIGH);
  analogWrite(motorSpeedPin1, Spd);
  digitalWrite(motorDirPin2, HIGH);
  analogWrite(motorSpeedPin2, Spd);
}

void MotorBackward( byte Spd)
{
  digitalWrite(motorDirPin1, LOW);
  analogWrite(motorSpeedPin1, Spd);
  digitalWrite(motorDirPin2, LOW);
  analogWrite(motorSpeedPin2, Spd);
}
void MotorLeft( byte Spd)
{
  analogWrite(motorSpeedPin2, 0);
  digitalWrite(motorDirPin1, LOW);
  analogWrite(motorSpeedPin1, Spd);
}

void MotorRight( byte Spd)
{
  analogWrite(motorSpeedPin1, 0);
  digitalWrite(motorDirPin2, LOW);
  analogWrite(motorSpeedPin2, Spd);

}

void MotorStop()
{
  analogWrite(motorSpeedPin1, 0);
  analogWrite(motorSpeedPin2, 0);
}

hello, i am doing project of obstacle avoiding robot in which dc motors are controlled by using joystick. i had completed the controlling part of dc motors using joystick but when i used ultrasonic sensor with it, it is not operating in all direction(left, right, forward, backward) when obstacle is not detected, it should move in all direction but it moves in only one axis, for example it either moves in forward and backward or left and right direction however it works perfectly fine when obstacle is detected, i think i am missing some logic in code. i would appreciate your advice.

#define echopin 2 // echo pin
#define trigpin 3 // Trigger pin
int maximumRange = 30;
long duration, distance;
const byte joyStickPin1 = A1;
const byte joyStickPin2 = A0;
const byte motorSpeedPin1 = 11;
const byte motorDirPin1 = 13;
const byte motorSpeedPin2 = 5;
const byte motorDirPin2 = 12;
//Joystick input variables
int joyValue1 = 0;
int joyValue2 = 0;
int joyValueMax = 1023;
int joyValueMin = 0;
int joyValueMid = 512;
int joyValueMidUpper = joyValueMid + 20;
int joyValueMidLower = joyValueMid - 20;

//DC motor variables
byte motorSpeed = 0;
byte motorSpeedMax = 255;
byte motorSpeedMin = 0;

void setup()
{
  Serial.begin (9600);
  pinMode(trigpin, OUTPUT);
  pinMode(echopin, INPUT );
  pinMode(joyStickPin1, INPUT);
  pinMode(motorSpeedPin1, OUTPUT);
  pinMode(motorDirPin1, OUTPUT);
  pinMode(joyStickPin2, INPUT);
  pinMode(motorSpeedPin2, OUTPUT);
  pinMode(motorDirPin2, OUTPUT);
}

void loop()
{
  {
    digitalWrite(trigpin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigpin, HIGH);
    delayMicroseconds(10);
    duration = pulseIn (echopin, HIGH);
    distance = duration / 58.2;
    delay (50);

    joyValue1 = analogRead(joyStickPin1);  //y-axis of joystick
    joyValue2 = analogRead(joyStickPin2);  //x-axis of joystick
  }
  if (distance >= 20)
  {
    //only this doesn't work, robot is not operating in left and right direction, in this condition if (distance >= 20) i want robot in move in all 4 directions but it only moves forward and backward direction, when i remove the code of forward and backward, then only it moves in left and right. it only reads either joystickpin1 or joystickpin2 but not both simultaneously. this is the only prob i am facing.


    if (joyValue2 > joyValueMidUpper) //left
    {
      motorSpeed = map(joyValue2, joyValueMidUpper, joyValueMax, motorSpeedMin, motorSpeedMax);
      MotorLeft(motorSpeed);
    }

    else if (joyValue2 < joyValueMidLower) //right
    {
      motorSpeed = map(joyValue2, joyValueMidLower, joyValueMin, motorSpeedMin, motorSpeedMax);
      MotorRight(motorSpeed);
    }

    else
    {
      MotorStop();
    }

    //here, this part is also working when obstacle is not detected at the distance of 20, it moves in forward and backward direction but the above code doesn't work.

    if (joyValue1 > joyValueMidUpper) //Forward
    {
      motorSpeed = map(joyValue1, joyValueMidUpper, joyValueMax, motorSpeedMin, motorSpeedMax);
      MotorForward(motorSpeed);
    }
    else if (joyValue1 < joyValueMidLower) //Backward
    {
      motorSpeed = map(joyValue1, joyValueMidLower, joyValueMin, motorSpeedMin, motorSpeedMax);
      MotorBackward(motorSpeed);
    }

    else
    {
      MotorStop();
    }
  }


  else if (distance < 20)     //sir, this works perfectly fine when distance is less than 20, obstacle is detected and robot operate same as the mentioned code below.

  {
    if (joyValue2 > joyValueMidUpper) //left
    {
      motorSpeed = map(joyValue2, joyValueMidUpper, joyValueMax, motorSpeedMin, motorSpeedMax);
      MotorLeft(motorSpeed);
    }
    else if (joyValue2 < joyValueMidLower) //right
    {
      motorSpeed = map(joyValue2, joyValueMidLower, joyValueMin, motorSpeedMin, motorSpeedMax);
      MotorRight(motorSpeed);
    }

    else
    {
      MotorStop();
    }
  }
}

void MotorForward( byte Spd)
{
  digitalWrite(motorDirPin1, HIGH);
  analogWrite(motorSpeedPin1, Spd);
  digitalWrite(motorDirPin2, HIGH);
  analogWrite(motorSpeedPin2, Spd);
}

void MotorBackward( byte Spd)
{
  digitalWrite(motorDirPin1, LOW);
  analogWrite(motorSpeedPin1, Spd);
  digitalWrite(motorDirPin2, LOW);
  analogWrite(motorSpeedPin2, Spd);
}
void MotorLeft( byte Spd)
{
  analogWrite(motorSpeedPin2, 0);
  digitalWrite(motorDirPin1, LOW);
  analogWrite(motorSpeedPin1, Spd);
}

void MotorRight( byte Spd)
{
  analogWrite(motorSpeedPin1, 0);
  digitalWrite(motorDirPin2, LOW);
  analogWrite(motorSpeedPin2, Spd);

}

void MotorStop()
{
  analogWrite(motorSpeedPin1, 0);
  analogWrite(motorSpeedPin2, 0);
}

I would recommend you use Serial.println() to print out the value of distance and joyValue1&2 and and also in each "leg" of all the ifs and elses to print out a message to tell you which "leg" you're in.

Right now you have no way of telling a) where the logic should be going and b) where it actually went.

thing is this code works as it supposed tobe, but previous code doesn't. why?

#define echopin 2 // echo pin
#define trigpin 3 // Trigger pin
int maximumRange = 30;
long duration, distance;
const byte joyStickPin1 = A0;
const byte joyStickPin2 = A1;
const byte motorSpeedPin1 = 11;
const byte motorDirPin1 = 13;
const byte motorSpeedPin2 = 5;
const byte motorDirPin2 = 12;
//Joystick input variables
int joyValue1 = 0;
int joyValue2 = 0;
int joyValueMax = 1023;
int joyValueMin = 0;
int joyValueMid = 512;
int joyValueMidUpper = joyValueMid + 20;
int joyValueMidLower = joyValueMid - 20;

//DC motor variables
byte motorSpeed = 0;
byte motorSpeedMax = 255;
byte motorSpeedMin = 0;

void setup()
{
  Serial.begin (9600);
  pinMode(trigpin, OUTPUT);
  pinMode(echopin, INPUT );
  pinMode(joyStickPin1, INPUT);
  pinMode(motorSpeedPin1, OUTPUT);
  pinMode(motorDirPin1, OUTPUT);
  pinMode(joyStickPin2, INPUT);
  pinMode(motorSpeedPin2, OUTPUT);
  pinMode(motorDirPin2, OUTPUT);
}

void loop()
{
  {
    digitalWrite(trigpin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigpin, HIGH);
    delayMicroseconds(10);
    duration = pulseIn (echopin, HIGH);
    distance = duration / 58.2;
    delay (50);

    joyValue1 = analogRead(joyStickPin1);  //y-axis of joystick
    joyValue2 = analogRead(joyStickPin2);  //x-axis of joystick
  }
  if (distance >= 20)
  {
    
    if (joyValue1 > joyValueMidUpper) //Forward
    {
      motorSpeed = map(joyValue1, joyValueMidUpper, joyValueMax, motorSpeedMin, motorSpeedMax);
      MotorForward(motorSpeed);
    }
    else if (joyValue1 < joyValueMidLower) //Backward
    {
      motorSpeed = map(joyValue1, joyValueMidLower, joyValueMin, motorSpeedMin, motorSpeedMax);
      MotorBackward(motorSpeed);
    }

    else
    {
      MotorStop();
    }


  }


  else if (distance < 20)     

  {
    if (joyValue2 > joyValueMidUpper) //left
    {
      motorSpeed = map(joyValue2, joyValueMidUpper, joyValueMax, motorSpeedMin, motorSpeedMax);
      MotorLeft(motorSpeed);
    }
    else if (joyValue2 < joyValueMidLower) //right
    {
      motorSpeed = map(joyValue2, joyValueMidLower, joyValueMin, motorSpeedMin, motorSpeedMax);
      MotorRight(motorSpeed);
    }

    else
    {
      MotorStop();
    }
  }




}

void MotorForward( byte Spd)
{
  digitalWrite(motorDirPin1, HIGH);
  analogWrite(motorSpeedPin1, Spd);
  digitalWrite(motorDirPin2, HIGH);
  analogWrite(motorSpeedPin2, Spd);
}

void MotorBackward( byte Spd)
{
  digitalWrite(motorDirPin1, LOW);
  analogWrite(motorSpeedPin1, Spd);
  digitalWrite(motorDirPin2, LOW);
  analogWrite(motorSpeedPin2, Spd);
}
void MotorLeft( byte Spd)
{
  analogWrite(motorSpeedPin2, 0);
  digitalWrite(motorDirPin1, LOW);
  analogWrite(motorSpeedPin1, Spd);
}

void MotorRight( byte Spd)
{
  analogWrite(motorSpeedPin1, 0);
  digitalWrite(motorDirPin2, LOW);
  analogWrite(motorSpeedPin2, Spd);

}

void MotorStop()
{
  analogWrite(motorSpeedPin1, 0);
  analogWrite(motorSpeedPin2, 0);
}

anyone would suggest something?