Use the IDE autoformat tool (ctrl-t or Tools, Auto format)to indent the code so that the logic shows up. You need to mind the placement of your curly brackets.
Here is the loop() function formatted (see the comments).
void loop()
{
// Before we declared that robot’s ultrasonic sensors would look forward in the beginning, that is why left, and right distance is equal to zero
int RightDistance = 0;
int LeftDistance = 0;
int FrontDistance = readPing(); //this line asks our robot to measure the distance in front of the robot
delay(100);
if (FrontDistance > 20 && FrontDistance < 60)
{
TurnMotorA();
TurnMotorB();
}
else if (FrontDistance < 20 )
{
myservo.write(170);
delay(1000);
int RightDistance = readPing();
delay(100);
myservo.write(115);
// the next if will never execute because FrontDistance
// must be < 20 to get here
if (RightDistance > 20 && RightDistance < 60)
{
TurnOFFB();
TurnMotorA();
}
else
{
myservo.write(60);
delay(1000);
int LeftDistance = readPing();
delay(100);
myservo.write(115);
} if (LeftDistance > 20 && LeftDistance < 60)
{
TurnOFFA();
TurnMotorB();
}
else
{
TurnBackB();
TurnBackA();
}
}
else if (FrontDistance > 60)
{
myservo.write(170);
delay(1000);
int RightDistance = readPing();
delay(100);
myservo.write(115);
// The next if will never execute because FrontDistance
// must be > than 60 to get here
if (RightDistance > 20 && RightDistance < 60)
{
TurnOFFB();
TurnMotorA();
}
else
{
myservo.write(60);
delay(1000);
int LeftDistance = readPing();
delay(100);
myservo.write(115);
} if (LeftDistance > 20 && LeftDistance < 60)
{
TurnOFFA();
TurnMotorB();
}
else
{
TurnOFFA();
TurnOFFB();
}
}
}