Robot line follower with robot Arm

Hello I'm new to this Arduino code here and my project was to do a line follower with robot Arm so I came up with the idea to separate these two to 2 code and then combine them in 1 but when I combine these two only my Robot Arm work and just after I'm not include Robot Arm in then the car go working so can someone explain to me please :smiling_face_with_tear: .
Here is my code :

include <Servo.h>
// Khai báo động cơ
int S_A = 10;  // tốc độ động cơ A
int M_A1 = 2;  // động cơ A cực +
int M_A2 = 3;  // động cơ A cực -
int M_B1 = 4;  // động cơ B cực -
int M_B2 = 5;  // động cơ B cực +
int S_B = 9;   // tốc độ động cơ B

// Khai báo cảm biến
int R_S = A0; //sincer R
int S_S = A1; //sincer S
int L_S = A2; //sincer L
int pos1;  //canh tay
int pos2;  //khuy tay
int pos3;  //co tay

int trigPin = A3;
int echoPin = A4;
int duration;
float d_cm;

Servo servo1; 
Servo servo2;
Servo servo3;
void setup() 
{
servo1.attach(11);  // canh tay
servo2.attach(12);  // khuy tay
servo3.attach(13);  // co tay
servo2.write(150);  //khuy tay goc bd
servo3.write(80);  // co tay goc bd
servo1.write(20);  // canh tay goc b
Serial.begin(9600);

// Cài đặt cảm biến siêu âm
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
  // Cài đặt động cơ
pinMode(M_B1, OUTPUT);
pinMode(M_B2, OUTPUT);
pinMode(M_A1, OUTPUT);
pinMode(M_A2, OUTPUT);
pinMode(S_B, OUTPUT);
pinMode(S_A, OUTPUT);

pinMode(L_S, INPUT);
pinMode(S_S, INPUT);
pinMode(R_S, INPUT);

analogWrite(S_A, 150); 
analogWrite(S_B, 150); 
delay(200);

digitalWrite(M_A1, LOW);
digitalWrite(M_A2, HIGH);
digitalWrite(M_B1, HIGH);
digitalWrite(M_B2, LOW);  
delay(4000);
}
void loop()
{  
  // Đo khoảng cách bằng cảm biến siêu âm
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  d_cm = duration * 0.034 / 2.;
  Serial.print("Distance: ");
  Serial.print(d_cm);
  Serial.println(" cm");

  // Nếu khoảng cách <= 10 cm, thực hiện gắp vật
  if (d_cm <= 10) {
  Stop();
  delay(500);
  Back();
  delay(500);
  gapVat();
  delay(500);
  turnLeft();
  delay(1000);
  forword();
  delay(1500);
  thaVat();
  delay(500);
  turnLeft();
  delay(1500);
  forword();
  delay(2000);
  }
if ((digitalRead(L_S) == 0)&&(digitalRead(S_S) == 1)&&(digitalRead(R_S) == 0)){forword();}

if ((digitalRead(L_S) == 1)&&(digitalRead(S_S) == 1)&&(digitalRead(R_S) == 0)){turnLeft();}
if ((digitalRead(L_S) == 1)&&(digitalRead(S_S) ==0)&&(digitalRead(R_S) == 0)) {turnLeft();}

if ((digitalRead(L_S) == 0)&&(digitalRead(S_S) == 1)&&(digitalRead(R_S) == 1)){turnRight();}
if ((digitalRead(L_S) == 0)&&(digitalRead(S_S) == 0)&&(digitalRead(R_S) == 1)){turnRight();}

if ((digitalRead(L_S) == 1)&&(digitalRead(S_S) == 1)&&(digitalRead(R_S) == 1)){turnLeft();}
}

void forword(){
digitalWrite(M_A1, LOW);
digitalWrite(M_A2, HIGH);
digitalWrite(M_B1, HIGH);
digitalWrite(M_B2, LOW);  
}


void turnRight(){
digitalWrite(M_A1, LOW);
digitalWrite(M_A2, LOW);
digitalWrite(M_B1, HIGH);
digitalWrite(M_B2, LOW);  
}

void turnLeft(){
digitalWrite(M_A1, LOW);
digitalWrite(M_A2, HIGH);
digitalWrite(M_B1, LOW);
digitalWrite(M_B2, LOW);
}

void Stop(){
digitalWrite(M_A1, LOW);
digitalWrite(M_A2, LOW);
digitalWrite(M_B1, LOW);
digitalWrite(M_B2, LOW);
}
void Back()
{
digitalWrite(M_A1, HIGH);
digitalWrite(M_A2, LOW);
digitalWrite(M_B1, LOW);
digitalWrite(M_B2, HIGH);
}
void gapVat()
{
for(pos2 = servo2.read();pos2 >=60;pos2 -=1.5 )
  {servo2.write(pos2);
  delay(100);
  }
   if (servo2.read()== 60){
  for(pos3 = servo3.read();pos3 >=60;pos3 -=1 )
  servo3.write(pos3);
  delay(100);
  }
  delay(2000);
  if (servo3.read()== 60){
  for(pos1 = servo1.read();pos1 <=80;pos1 +=1 )
  servo1.write(pos1);
  delay(1000);
  }
  if (servo1.read()== 80){
  for(pos3 = servo3.read();pos3 <=80;pos3 +=1 )
  servo3.write(pos3);
  delay(1000);
  }
  if (servo3.read()== 80){
  for(pos2 = servo2.read();pos2 <=120;pos2 +=1 )
  servo2.write(pos2);
  delay(100);
  }
}
void Thavat()
{
  for(pos2 = servo2.read();pos2 >=100;pos2 -=1 ){
  servo2.write(pos2);
  delay(100);}
  if (servo2.read()== 100){
  for(pos1 = servo1.read();pos1 >=0;pos1 -=1 )
  servo1.write(pos1);
  delay(500);
  if (servo1.read()== 0){
  for(pos2 = servo2.read();pos2 <=150;pos2 +=1 )
  servo2.write(pos2);
  delay(100);
  servo3.write(70);  // co tay goc bd
  servo1.write(20);
  }
  }
}

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

Please autoformat the code. Ctrl + T and post that code. Some mistakes show up directly by that.
When asking for help in an English speaking forum code coments in English are favourable.



#include <Servo.h>
int S_A = 10;  // for end 2
int M_A1 = 2;  // for in1
int M_A2 = 3;  // for in2
int M_B1 = 4;  // for in3
int M_B2 = 5;  // for in4
int S_B = 9;   // for end 1

// Khai báo cảm biến
int R_S = A0;  //sincer R right
int S_S = A1;  //sincer S mid
int L_S = A2;  //sincer L left
int pos1;      // arm
int pos2;      //wrist
int pos3;      //robot grap

int trigPin = A3;
int echoPin = A4;
int duration;
float d_cm;

Servo servo1;
Servo servo2;
Servo servo3;
void setup() {
  servo1.attach(11);  // arm
  servo2.attach(12);  // wrist
  servo3.attach(13);  // grab
  servo2.write(150);  //
  servo3.write(80);   //
  servo1.write(20);   //
  Serial.begin(9600);


  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  pinMode(M_B1, OUTPUT);
  pinMode(M_B2, OUTPUT);
  pinMode(M_A1, OUTPUT);
  pinMode(M_A2, OUTPUT);
  pinMode(S_B, OUTPUT);
  pinMode(S_A, OUTPUT);

  pinMode(L_S, INPUT);
  pinMode(S_S, INPUT);
  pinMode(R_S, INPUT);

  analogWrite(S_A, 150);
  analogWrite(S_B, 150);
  delay(200);

  digitalWrite(M_A1, LOW);
  digitalWrite(M_A2, HIGH);
  digitalWrite(M_B1, HIGH);
  digitalWrite(M_B2, LOW);
  delay(4000);
}
void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  d_cm = duration * 0.034 / 2.;
  Serial.print("Distance: ");
  Serial.print(d_cm);
  Serial.println(" cm");

  if (d_cm <= 10) {
    Stop();
    delay(500);
    Back();
    delay(500);
    grap();
    delay(500);
    turnLeft();
    delay(1000);
    forword();
    delay(1500);
    drop();
    delay(500);
    turnLeft();
    delay(1500);
    forword();
    delay(2000);
  }
  if ((digitalRead(L_S) == 0) && (digitalRead(S_S) == 1) && (digitalRead(R_S) == 0)) { forword(); }

  if ((digitalRead(L_S) == 1) && (digitalRead(S_S) == 1) && (digitalRead(R_S) == 0)) { turnLeft(); }
  if ((digitalRead(L_S) == 1) && (digitalRead(S_S) == 0) && (digitalRead(R_S) == 0)) { turnLeft(); }

  if ((digitalRead(L_S) == 0) && (digitalRead(S_S) == 1) && (digitalRead(R_S) == 1)) { turnRight(); }
  if ((digitalRead(L_S) == 0) && (digitalRead(S_S) == 0) && (digitalRead(R_S) == 1)) { turnRight(); }

  if ((digitalRead(L_S) == 1) && (digitalRead(S_S) == 1) && (digitalRead(R_S) == 1)) { turnLeft(); }
}

void forword() {
  digitalWrite(M_A1, LOW);
  digitalWrite(M_A2, HIGH);
  digitalWrite(M_B1, HIGH);
  digitalWrite(M_B2, LOW);
}


void turnRight() {
  digitalWrite(M_A1, LOW);
  digitalWrite(M_A2, LOW);
  digitalWrite(M_B1, HIGH);
  digitalWrite(M_B2, LOW);
}

void turnLeft() {
  digitalWrite(M_A1, LOW);
  digitalWrite(M_A2, HIGH);
  digitalWrite(M_B1, LOW);
  digitalWrite(M_B2, LOW);
}

void Stop() {
  digitalWrite(M_A1, LOW);
  digitalWrite(M_A2, LOW);
  digitalWrite(M_B1, LOW);
  digitalWrite(M_B2, LOW);
}
void Back() {
  digitalWrite(M_A1, HIGH);
  digitalWrite(M_A2, LOW);
  digitalWrite(M_B1, LOW);
  digitalWrite(M_B2, HIGH);
}
void grap() {
  for (pos2 = servo2.read(); pos2 >= 60; pos2 -= 1.5) {
    servo2.write(pos2);
    delay(100);
  }
  if (servo2.read() == 60) {
    for (pos3 = servo3.read(); pos3 >= 60; pos3 -= 1)
      servo3.write(pos3);
    delay(100);
  }
  delay(2000);
  if (servo3.read() == 60) {
    for (pos1 = servo1.read(); pos1 <= 80; pos1 += 1)
      servo1.write(pos1);
    delay(1000);
  }
  if (servo1.read() == 80) {
    for (pos3 = servo3.read(); pos3 <= 80; pos3 += 1)
      servo3.write(pos3);
    delay(1000);
  }
  if (servo3.read() == 80) {
    for (pos2 = servo2.read(); pos2 <= 120; pos2 += 1)
      servo2.write(pos2);
    delay(100);
  }
}
void drop() {
  for (pos2 = servo2.read(); pos2 >= 100; pos2 -= 1) {
    servo2.write(pos2);
    delay(100);
  }
  if (servo2.read() == 100) {
    for (pos1 = servo1.read(); pos1 >= 0; pos1 -= 1)
      servo1.write(pos1);
    delay(500);
    if (servo1.read() == 0) {
      for (pos2 = servo2.read(); pos2 <= 150; pos2 += 1)
        servo2.write(pos2);
      delay(100);
      servo3.write(70);
      servo1.write(20);
    }
  }
}

i don't know what wrong
pls help me

I don't know what wrong but when I unattached servo1,2,3 it will work normally so maybe it's about servo but I don't know how to code it

On boards other than the Mega, use of the servo library disables analogWrite() (PWM) functionality on pins 9 and 10, whether or not there is a Servo on those pins.

So you need to change S_A and S_B to different pins.

thanks for your reply I will fix and test immediately

Glad it worked
Have FUN with it!