A simple robot with IR sensor(Line follower)

I am trying to make a line follower
Parts -4 IR sensors(Signals 4-5V as high & 0V as low) &, 2 DC motors ,Power supply-3 9V cell in parallel to Adafruit motor Shield, using Arduino uno board

I wanted to write just a testing code for my robot

what this code is expected to do is- to take the input from 2(out of the 4) IR sensors
if the input is high run motor in forward direction
if the input is low run motor in backward direction

what it does is - keeps running the motor in one direction only, it doesn’t changes it direction on changing the input

code

#include <AFMotor.h>
AF_DCMotor motor1(1, MOTOR12_64KHZ); // create motor #1, 64KHz pwm
AF_DCMotor motor2(2, MOTOR12_64KHZ);

int IR1=A1,IR2=A2,IR3=A3,IR4=A4;
int Ir1=0,Ir2=0,Ir3=0,Ir4=0;

void setup()
{
pinMode(IR1,INPUT);
pinMode(IR2,INPUT);
pinMode(IR3,INPUT);
pinMode (IR4,INPUT);

motor1.setSpeed(240);
motor2.setSpeed(240);
}

void loop()
{
Ir1=analogRead(IR1);
Ir2=analogRead(IR2);
Ir3=analogRead(IR3);
Ir4=analogRead(IR4);

if (Ir2>400&&Ir3>400)
{motor1.run(FORWARD);
motor2.run(FORWARD);}
else
{
motor1.run(BACKWARD);
motor2.run(BACKWARD);
}
delay(500);
}

P.S. I new to robotic & tech stuff I can & I may have done what may appear to u as horrific/stupid

How do you know what the values of Ir2 and Ir3 are? Presumably you did some testing before you arrived at 400 as some threshold?

What I’d do, is use Serial.print() to print those values out so you can see what they really are. Then also print some kind of “I am here…” from each part of the “if” and see where your logic actually takes you. Then when you upload the sketch run it with the monitor open and you should see what’s going on.

See the lines I marked <<<<<<<<<<<<<< in the code below.

Note also that my code’s in a nice format… for future reference, when you post code, select it and hit the </> format icon.

#include <AFMotor.h>
AF_DCMotor motor1(1, MOTOR12_64KHZ); // create motor #1, 64KHz pwm
AF_DCMotor motor2(2, MOTOR12_64KHZ);

int IR1=A1,IR2=A2,IR3=A3,IR4=A4;
int Ir1=0,Ir2=0,Ir3=0,Ir4=0;

void setup()
{
pinMode(IR1,INPUT);
pinMode(IR2,INPUT);
pinMode(IR3,INPUT);
pinMode (IR4,INPUT);
  
motor1.setSpeed(240);
motor2.setSpeed(240);  

Serial.begin(9600); //<<<<<<<<<<<<<<<<<
}


void loop()
{
 Ir1=analogRead(IR1); 
 Ir2=analogRead(IR2);
 Ir3=analogRead(IR3);
 Ir4=analogRead(IR4);

Serial.print(Ir2);   //<<<<<<<<<<<<<<<<<
Serial.print("    ");   //<<<<<<<<<<<<<<<<<
Serial.println(Ir3);   //<<<<<<<<<<<<<<<<<

 if (Ir2>400&&Ir3>400)  
 { Serial.println("going forward");  //<<<<<<<<<<<<<<<<<
motor1.run(FORWARD);
  motor2.run(FORWARD);}
 else
 {
Serial.println("going backward");  //<<<<<<<<<<<<<<<<<
  motor1.run(BACKWARD);
  motor2.run(BACKWARD);
 }
delay(500);
}