LINE CHANGING Function in arduino uno and l293d motor shield

Hello. i have a doubt on the given code

#include <AFMotor.h>
#include <Servo.h>
#include <SoftwareSerial.h> // TX RX software library for bluetooth


//define

#define leftir A5
#define rightir A4

//define


//


#define trigPin2 9
#define echoPin2 10

//


AF_DCMotor motor1(1);  // Motor 1 connected to M1 port of L293D
AF_DCMotor motor2(2);  // Motor 2 connected to M2 port of L293D
AF_DCMotor motor3(3);  // Motor 3 connected to M3 port of L293D
AF_DCMotor motor4(4);  // Motor 4 connected to M4 port of L293D

void setup() {


  //ir

  pinMode(leftir,INPUT);
  pinMode(rightir,INPUT);

  //ir


  //ultrasonic sensor

  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);


  //ultrasonic sensor
  


  //
  Serial.begin(9600);  // Initialize serial communication
  motor1.setSpeed(255); // Set motor speed (0-255)
  motor2.setSpeed(255);
  motor3.setSpeed(255);
  motor4.setSpeed(255);
}

void loop() {

  if (Serial.available() > 0) {
    char command = Serial.read();
    executeCommand(command);

  }


  //ultrasonc sensor

  int duration, distance;     // The Duration will be the input pulse width and distance will be the distance to the obstacle in centimeters
  

  //new ultrasonic

  digitalWrite(trigPin2, HIGH); // The output pulse with 1ms width on trigPin
  delay(1);
  digitalWrite(trigPin2, LOW);
  duration = pulseIn(echoPin2, HIGH);  // Measures the pulse input in the echo pin
  distance = (duration/2) / 29.1;   // Distance is half the duration devided by 29.1 (from datasheet)
  // if the distance is less than 0.5 meter and more than 0 (0 or less means over range)
  if (distance <= 10 && distance >= 0) {

    stopMoving();
    Serial.println("EOAB");


  } else {
  // Nothing
  }
  // Waits 50 milliseconds
  delay(50);

  //new ultra

  
  //ultrasonic sensor

}

void executeCommand(char command) {
  switch (command) {
    case 'F':
      moveForward();
      break;
    case 'B':
      moveBackward();
      break;
    case 'L':
      turnLeft();
      break;
    case 'R':
      turnRight();
      break;
    case 'S':
      stopMoving();
      break;
    case 'Z':
      linefollowing();
      break;


  }
}



void moveForward() {
  motor1.setSpeed(255);
  motor2.setSpeed(255);
  motor3.setSpeed(255);
  motor4.setSpeed(255);
  motor1.run(FORWARD);
  motor2.run(FORWARD);
  motor3.run(FORWARD);
  motor4.run(FORWARD);
}

void moveBackward() {
  motor1.setSpeed(255);
  motor2.setSpeed(255);
  motor3.setSpeed(255);
  motor4.setSpeed(255);
  motor1.run(BACKWARD);
  motor2.run(BACKWARD);
  motor3.run(BACKWARD);
  motor4.run(BACKWARD);
}

void turnLeft() {
  // Increase speed for the forward motors, decrease for the backward ones
  motor1.setSpeed(255);
  motor2.setSpeed(255);
  motor3.setSpeed(255);  
  motor4.setSpeed(255);  
  
  motor1.run(BACKWARD);
  motor2.run(BACKWARD);
  motor3.run(FORWARD);
  motor4.run(FORWARD);
}

void turnRight() {
  // Increase speed for the forward motors, decrease for the backward ones
  motor1.setSpeed(255);  
  motor2.setSpeed(255);  
  motor3.setSpeed(255);
  motor4.setSpeed(255);
  
  motor1.run(FORWARD);
  motor2.run(FORWARD);
  motor3.run(BACKWARD);
  motor4.run(BACKWARD);
}


void stopMoving() {
  motor1.setSpeed(0);
  motor2.setSpeed(0);
  motor3.setSpeed(0);
  motor4.setSpeed(0);
}



  //line detection forward
void linefollowing() {
  int leftSensor = digitalRead(leftir);
  int rightSensor = digitalRead(rightir);

  if (leftSensor == LOW && rightSensor == LOW) {
    // Both sensors detect the line, move forward
    motor1.setSpeed(150);
    motor2.setSpeed(150);
    motor3.setSpeed(150);
    motor4.setSpeed(150);
    motor1.run(FORWARD);
    motor2.run(FORWARD);
    motor3.run(FORWARD);
    motor4.run(FORWARD);
  }
  else if (leftSensor == LOW && rightSensor == HIGH) {
    // Left sensor detects the line, turn left
    motor1.setSpeed(200);
    motor2.setSpeed(200);
    motor3.setSpeed(100);
    motor4.setSpeed(100);
    motor1.run(BACKWARD);
    motor2.run(BACKWARD);
    motor3.run(FORWARD);
    motor4.run(FORWARD);
  }
  else if (leftSensor == HIGH && rightSensor == LOW) {
    // Right sensor detects the line, turn right
    motor1.setSpeed(100);
    motor2.setSpeed(100);
    motor3.setSpeed(200);
    motor4.setSpeed(200);
    motor1.run(FORWARD);
    motor2.run(FORWARD);
    motor3.run(BACKWARD);
    motor4.run(BACKWARD);
  }
  else {
    // No line detected, stop
    stopMoving();
  }
}

How to add line changing function to the car when it is in line following mode ?

Is the current code for line following is ok? How to add line changing function to the car when the car recieve letter 'X' and 'C' to changing line to Left and Righ lane Respectivily

can you help to do it and say new things that i can implement in this project other than line following using same setup.

This project also contain an esp32cam for video streaming via wifi to the mobile app. Can i use any thing to make the car better?

does it work?

What you mean?

does the car run as expected with this code. Does it follow lines ?

Do you mean the code for line following is working? If thats you question yes it works some times because this project is not completed

I need to know about more on line changing. Do any one know how to do it

How to add line changing function to the car when the car recieve letter 'X' and 'C' to changing line to Left and Righ lane Respectivily

Hello anyone there Please Help

we won't do the code for you.... bumping your thread up won't help, it's more likely going to annoy participants.

Line changing will need some sort of state machine

say you are going straight and want to go to the lane on your left
you turn left, say 45°
you keep moving until you detect the new line
you turn right 45° to go straight again
you go back to the line tracking algorithm

how to do it

by coding in C++ ?

you might benefit from studying state machines. Here is a small introduction to the topic: Yet another Finite State Machine introduction

thanks

@J-M-L

in

void executeCommand(char command) {}

updated

case 'x'
    switchtoleftline();
    break;
void switchtoleftline() {

  //linefollowing sub
  motor1.setSpeed(150);
  motor2.setSpeed(150);
  motor3.setSpeed(160);
  motor4.setSpeed(160);
  motor1.run(FORWARD);
  motor2.run(FORWARD);
  motor3.run(FORWARD);
  motor4.run(FORWARD);

  delay(1000);

  if(digitalRead(leftir)==0 && digitalRead(rightir)==0) {
    linefollowing();
  }
}

is this okay for line changing to left line

Probably not

You need a change of direction then wait until you hit the new line and then turn again to get aligned on the line without shooting too far off.

You can’t have a delay in that code…

Take a piece Of paper and draw the state machine ( the states, the events and the actions)