// ============================================
// LINE FOLLOWING ROBOT — Arduino Uno
// 3 IR Sensors + L298N Motor Driver
// Sensors: D2 (Left), D3 (Centre), D4 (Right)
// Motor A (Left): IN1=D5, IN2=D6, ENA=D9
// Motor B (Right): IN3=D7, IN4=D8, ENB=D10
// ============================================
const int IR_LEFT = 2;
const int IR_CENTRE = 3;
const int IR_RIGHT = 4;
const int IN1 = 5;
const int IN2 = 6;
const int ENA = 9;
const int IN3 = 7;
const int IN4 = 8;
const int ENB = 10;
const int BASE_SPEED = 150;
const int TURN_SPEED = 100;
const int SHARP_SPEED = 60;
void setup() {
pinMode(IR_LEFT, INPUT);
pinMode(IR_CENTRE, INPUT);
pinMode(IR_RIGHT, INPUT);
pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT);
pinMode(ENA, OUTPUT); pinMode(ENB, OUTPUT);
Serial.begin(9600);
delay(1000);
}
void loop() {
bool L = (digitalRead(IR_LEFT) == LOW);
bool C = (digitalRead(IR_CENTRE) == LOW);
bool R = (digitalRead(IR_RIGHT) == LOW);
if (!L && C && !R) moveForward(BASE_SPEED, BASE_SPEED);
else if (!L && C && R) moveForward(BASE_SPEED, TURN_SPEED);
else if ( L && C && !R) moveForward(TURN_SPEED, BASE_SPEED);
else if (!L && !C && R) moveForward(BASE_SPEED, SHARP_SPEED);
else if ( L && !C && !R) moveForward(SHARP_SPEED, BASE_SPEED);
else if ( L && C && R) moveForward(BASE_SPEED, BASE_SPEED);
else stopMotors();
}
void moveForward(int speedA, int speedB) {
digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW);
analogWrite(ENA, speedA);
digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW);
analogWrite(ENB, speedB);
}
void stopMotors() {
digitalWrite(IN1, LOW); digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW); digitalWrite(IN4, LOW);
analogWrite(ENA, 0); analogWrite(ENB, 0);
}]
}