help needed to better my program

Hi I'm 17 and working on a final school project,
I need some help in bettering the code for my robot witch has 3 main functions.
1st- a line follow sensor (to follow a black line in a circuit )
2nd- a sonar sensor ( to avoid any obstacles that might be in the way of that circuit)
3rd - Bluetooth (to control the whole robot )
At the moment the two first function work really well but unfortunately the Bluetooth has a really big delay.
I would like for the bluettoth to overwrite any other funtion
does any one have any solution to better this?
(when I only have the bluetooth code it works with out any delay)
I'm using a 4in1 by ebotics witch is very similar to a arduino uno

#include <NewPing.h>
 char data = 0;  //VARIABLE FOR STORING BLUETOOTH DATA
int mdt=4;//motor esquerda frente
int mdf=5;//motor esquerda tras
int mef=7;//motor direita frente
int met=6;//motor direita tras
int e=13;// esquerda sensor linha
int m=12;// meio sensor linha
int d=11;// direita sensor linha
int trigPin = 9;      // trig pin of HC-SR04
int echoPin = 10;     // Echo pin of HC-SR04
int distance = 100;
int MAX_DISTANCE = 400;
int distanceR = 0;
int distanceL = 0;
NewPing sonar(trigPin, echoPin, MAX_DISTANCE); 

void setup() {
  
Serial.begin(9600);         //Sets the baud for serial data transmission 
pinMode(mdt,OUTPUT); 
pinMode(mdf,OUTPUT); 
pinMode(mef,OUTPUT); 
pinMode(met,OUTPUT); 
pinMode(e,INPUT); 
pinMode(m,INPUT); 
pinMode(d,INPUT); 
pinMode(trigPin, OUTPUT);         // set trig pin as output
pinMode(echoPin, INPUT);          //set echo pin as input to capture reflected waves
}

void loop(){
  
  if(Serial.available() > 0)      // SEND DATA ONLY WHEN YOU RECEIVE DATA
{
   data = Serial.read();        //READ THE INCOMING DATA AND STORE INTO DATA 

      // INTERPRETATION OF BLUETOOTH ENTRY DATA

switch (data)
{
   case'F':{ frente();
   }
   break;

   case 'L':{ Esquerda();
   } 
   break;
 
   case 'R':{ Direita();
   }
   break;
   
   case 'S':{ para();
   }
   break;

   case 'B':{ tras();
   }
   break;
}
}

if(!digitalRead(e)&&digitalRead(m)&&!digitalRead(d)){ frente(); //010 vai frente
}
if(digitalRead(e)&&!digitalRead(m)&&!digitalRead(d)){ Esquerda(); //100 vai esquerda
}
if(!digitalRead(e)&&!digitalRead(m)&&digitalRead(d)){ Direita(); //001 vai direita
}

 if (distance < 10)
  {
  tras();
  delay(300);
  viraesquerda();
  delay(650);
  distanceL = olhaesquerda();
  delay(500);
  viradireita();
  delay(1350);
  distanceR = olhadireita();
  delay(500);
  viraesquerda();
  delay(700);
  para();
  delay(500);
  
  if(distanceL>=distanceR)
  {
    Esquerda();
    delay(700);
    frente();
    delay(600);
    Direita();
    delay(700);
    frente();
    delay(1000);
    Direita();
    delay(700);
    do { frente();
    } 
    while (!digitalRead(e)&&!digitalRead(m)&&!digitalRead(d));
    Esquerda();
    delay(800);
  }else
  {
   Direita();
    delay(700);
    frente();
    delay(600);
    Esquerda();
    delay(1000);
    frente();
    delay(1000);
    Esquerda();
    delay(1000);
    do { frente();
    } 
    while (!digitalRead(e)&&!digitalRead(m)&&!digitalRead(d));
    Direita();
    delay(800);
  }
 }
 
 distance = readPing();
  }
  
int olhaesquerda()
{ 
     digitalWrite(mef,LOW);
     analogWrite(met,0);
     analogWrite(mdf,0);
     digitalWrite(mdt,LOW);
     int distance = readPing();
     delay(100);
     return distance;
}

int olhadireita()
{ 
    digitalWrite(mef,LOW);
    analogWrite(met,0);
    analogWrite(mdf,0);
    digitalWrite(mdt,LOW);
    int distance = readPing();
    delay(100);
    return distance;
    delay(100);
}
    
int readPing() { 
  delay(70);
  int cm = sonar.ping_cm();
  if(cm==0)
  {
    cm = 400;
  }
  return cm;
} 
  
void frente(){ // frente
digitalWrite(mef,LOW);
analogWrite(met,255);
analogWrite(mdf,255);
digitalWrite(mdt,HIGH);
}

void Esquerda(){ // esquerda
digitalWrite(mef,LOW);
analogWrite(met,0);
analogWrite(mdf,255);
digitalWrite(mdt,HIGH);
}

void Direita(){ // direita
digitalWrite(mef,LOW);
analogWrite(met,255);
analogWrite(mdf,0);
digitalWrite(mdt,HIGH);
}

void tras(){ //tras
digitalWrite(mef,HIGH);
analogWrite(met,255);
analogWrite(mdf,255);
digitalWrite(mdt,LOW);
}

void para(){ //para
digitalWrite(mef,LOW);
analogWrite(met,0);
analogWrite(mdf,0);
digitalWrite(mdt,LOW);
}
void viraesquerda(){ 
digitalWrite(mef,HIGH);
analogWrite(met,140);
analogWrite(mdf,140);
digitalWrite(mdt,HIGH);
}
void viradireita(){ 
digitalWrite(mef,LOW);
analogWrite(met,140);
analogWrite(mdf,140);
digitalWrite(mdt,LOW);
}

bestcodeintheworld.ino (3.95 KB)

Please post the code, in code tags

done!

You have a large number of calls to delay in your code so your bluetooth response will indeed be slow. There is no easy fix - you will need to restructure your code to use millis to control timing. Take a look at Robin2's thread on the topic here.

OK, I will look into it thank you!