How do I do nested IF statement

Greetings to everyone! I'll say sorry in advance for my bad English but I hope you can understand me.

I'm new here in this forum and I'm having a problem with my project. I wanted to do a nested ifs in my code. My code compiled and uploads fine. But the problem is my nested ifs. The conditions in my 1st if met and the statements worked fine but an if statement inside won't work. I hope you can help me solve this problem. By the way, here's the code:

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C  lcd(0x27,2,1,0,4,5,6,7,3,POSITIVE); // 0x27 is the I2C bus address for an unmodified backpack

#define IN1 22
#define IN2 24
#define IN3 26
#define IN4 28
#define ENA 2
#define ENB 3
int echo_left = 37;
int trig_left = 39;
int echo_right = 41;
int trig_right = 43;
int echo_front = 45;
int trig_front = 47;
long duration, SensorLeft, SensorRight, SensorFront;
volatile float maxDistance = 3;
int LineTrackRight;
int LineTrackLeft;
int LineTrackCenter;

void MotorForward() {
  analogWrite (ENA, 150);
  analogWrite (ENB, 150);
  digitalWrite (IN1, HIGH);
  digitalWrite (IN2, LOW);
  digitalWrite (IN3, HIGH);
  digitalWrite (IN4, LOW);
}

void MotorBackward() {
  analogWrite (ENA, 150);
  analogWrite (ENB, 150);
  digitalWrite (IN1, LOW);
  digitalWrite (IN2, HIGH);
  digitalWrite (IN3, LOW);
  digitalWrite (IN4, HIGH);
}

void MotorRadialLeftTurn() {
  analogWrite (ENA, 150);
  analogWrite (ENB, 0);
  digitalWrite (IN1, HIGH);
  digitalWrite (IN2, LOW);
  digitalWrite (IN3, LOW);
  digitalWrite (IN4, LOW);
}

void MotorAxialRightTurn() {
  analogWrite (ENA, 150);
  analogWrite (ENB, 150);
  digitalWrite (IN1, LOW);
  digitalWrite (IN2, HIGH);
  digitalWrite (IN3, HIGH);
  digitalWrite (IN4, LOW);
}

void MotorAxialRightTurnLow() {
  analogWrite (ENA, 90);
  analogWrite (ENB, 90);
  digitalWrite (IN1, LOW);
  digitalWrite (IN2, HIGH);
  digitalWrite (IN3, HIGH);
  digitalWrite (IN4, LOW);
}

void MotorAxialLeftTurn() {
  analogWrite (ENA, 150);
  analogWrite (ENB, 150);
  digitalWrite (IN1, HIGH);
  digitalWrite (IN2, LOW);
  digitalWrite (IN3, LOW);
  digitalWrite (IN4, HIGH);
}

void MotorAxialLeftTurnLow() {
  analogWrite (ENA, 90);
  analogWrite (ENB, 90);
  digitalWrite (IN1, HIGH);
  digitalWrite (IN2, LOW);
  digitalWrite (IN3, LOW);
  digitalWrite (IN4, HIGH);
}

void MotorRadialRightTurn() {
  analogWrite (ENA, 0);
  analogWrite (ENB, 150);
  digitalWrite (IN1, LOW);
  digitalWrite (IN2, LOW);
  digitalWrite (IN3, HIGH);
  digitalWrite (IN4, LOW);
}

void MotorStop() {
  analogWrite (ENA, 0);
  analogWrite (ENB, 0);
  digitalWrite (IN1, HIGH);
  digitalWrite (IN2, HIGH);
  digitalWrite (IN3, HIGH);
  digitalWrite (IN4, HIGH);
}

void CheckSensorLeft() {
  digitalWrite(trig_left, LOW);
  delayMicroseconds(5);
  digitalWrite(trig_left, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig_left, LOW);
  duration = pulseIn(echo_left, HIGH);
  SensorLeft = (duration/2) / 74;
}

void CheckSensorFront() {
  digitalWrite(trig_front, LOW);
  delayMicroseconds(5);
  digitalWrite(trig_front, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig_front, LOW);
  duration = pulseIn(echo_front, HIGH);
  SensorFront = (duration/2) / 74;
}

void setup(){
  Serial.begin(9600);
  lcd.begin (20, 4);
  pinMode (31, INPUT);
  pinMode (33, INPUT);
  pinMode (35, INPUT);
  pinMode (IN1, OUTPUT);
  pinMode (IN2, OUTPUT);
  pinMode (IN3, OUTPUT);
  pinMode (IN4, OUTPUT);
  pinMode (ENA, OUTPUT);
  pinMode (ENB, OUTPUT);
  pinMode (echo_left, INPUT);
  pinMode (trig_left, OUTPUT);
  pinMode (echo_right, INPUT);
  pinMode (trig_right, OUTPUT);
  pinMode (echo_front, INPUT);
  pinMode (trig_front, OUTPUT);
}

void loop(){
  CheckSensorFront();
  LineTrackLeft = digitalRead(35);
  LineTrackCenter = digitalRead(33);
  LineTrackRight = digitalRead(31);
  
  if ((LineTrackLeft == 0 && LineTrackRight == 0 && LineTrackCenter == 1)){
    MotorForward();
  }
  
  else if ((LineTrackLeft == 1 && LineTrackRight == 0 && LineTrackCenter == 0) || (LineTrackLeft == 1 && LineTrackRight == 0 && LineTrackCenter == 1)){
    MotorRadialLeftTurn();
  }
  
  else if ((LineTrackLeft == 0 && LineTrackRight == 1 && LineTrackCenter == 0) || (LineTrackLeft == 0 && LineTrackRight == 1 && LineTrackCenter == 1)){
    MotorRadialRightTurn();
  }

  CheckSensorLeft();
  
  if(SensorLeft <= maxDistance){
    MotorAxialLeftTurnLow();
    if (SensorFront <= maxDistance){
      MotorStop();
      delay(5000);
    }
  }
}

Please explain what you mean by "an if statement inside won't work".

This code

if(SensorLeft <= maxDistance){
    MotorAxialLeftTurnLow();
    if (SensorFront <= maxDistance){
      MotorStop();
      delay(5000);
    }
  }

the if (SensorLeft <= maxDistance) met and worked so the MotorAxialLeftTurnLow() executed fine but when the inside if met the conditions, the MotorStop() won't work. It keeps executing the MotorAxialLeftTurnLow() even if the (SensorFront <= maxDistance) condition met.

My advice is to add some Serial.println() statements through your code to provide debug output and then run the code with the Serial Monitor running so that you can get some idea of what's actually happening.

So, either the condition for the second 'if' is not actually being met, or the MotorStop() function isn't working. Some strategically placed debug prints to Serial would probably help you determine what's happening.

Doooh, @pert beat me to it.

Audiph:
the if (SensorLeft <= maxDistance) met and worked so the MotorAxialLeftTurnLow() executed fine but when the inside if met the conditions, the MotorStop() won't work.

Maybe those IF tests should not be nested?

As others have said, debugging prints will allow you to see what is happening

...R