Avoidance sensor - don't understand code

Hi all,

Just started programming with Arduino today. Loving it :).

I'm building a simple obstacle avoiding rover.
Parts:

  • Arduino Uno
  • Tracked chassis with 2 motors
  • IR sensor

I've combined code from a simple "if obstacle sensor detects something, then turn off the light" with motor controls. The sketch doesn't run properly though. It only executes the standard "move forward" order, even though the LED on the obstacle sensor is lighting up, indicating problem.
So, only the ELSE condition is firing, not the IF.

Can you help please troubleshoot the code?

//L293D
//Motor A
const int motorPin1  = 9;  // Pin 14 of L293
const int motorPin2  = 10;  // Pin 10 of L293
//Motor B
const int motorPin3  = 6; // Pin  7 of L293
const int motorPin4  = 5;  // Pin  2 of L293

//IR Obstacle avoidance sensor
int isObstaclePin = 7;  // This is our input pin
int isObstacle = HIGH;  // HIGH MEANS NO OBSTACLE



//This will run only one time.
void setup(){
  pinMode(isObstaclePin, INPUT);
  Serial.begin(57600);
  
    //Set pins as outputs
    pinMode(motorPin1, OUTPUT);
    pinMode(motorPin2, OUTPUT);
    pinMode(motorPin3, OUTPUT);
    pinMode(motorPin4, OUTPUT);  
}

void loop () {
 
isObstacle = digitalRead(isObstaclePin);
  if (isObstacle == LOW)
  {
    Serial.println("OBSTACLE!!, OBSTACLE!!");
    
    //All stop
    analogWrite(motorPin1, 0);
    analogWrite(motorPin2, 0);
    analogWrite(motorPin3, 0);
    analogWrite(motorPin4, 0);
    delay(1000);
    // Back up
    analogWrite(motorPin2, 255);
    analogWrite(motorPin4, 255);
    delay(1000);
    // Turn right
    analogWrite(motorPin1,255);
    analogWrite(motorPin4,255);
    delay(2000);
   
  }
  else {
 
    //Motor Control - Motor A: motorPin1,motorpin2 & Motor B: motorpin3,motorpin4

    //Ahead full
    analogWrite(motorPin1, 1000);
    analogWrite(motorPin2, 0);
    analogWrite(motorPin3, 1000);
    analogWrite(motorPin4, 0);
    delay(5000); 
    
}


}

Thanks for any help you can offer! :slight_smile: