Help please with the code arduino

Hello my problem is. I can not make one out of two sketches. And run my project RC car control HC-05 HC-SR04 . I can not figure out how to add speed control. I’ll figure it out. Thank you very much .

Two_mode_motor_ultrasonic_v1.8.ino (7.8 KB)

bluetooth_arduino_car_v1.ino (6.24 KB)

Ok, so what exactly is wrong with your code? (and don't just say you don't know how to merge the two programs - give specific details on the problems you're facing)

From one program you need to take some of the speed control code and insert it into the main one.(Two_mode_motor_ultrasonic_v1.8.ino ) But I do not understand how to do this (((

#include <SoftwareSerial.h> 
 #include <NewPing.h> //ultrasonic library
#include <Servo.h> //servo library
 const int RightmotorF =10;  //digitalpin 7 for right motor forward ВПЕРЕД 
 const int RightmotorB =9;  //digital pin 8 for right motor Backward НАЗАД
 const int LeftmotorF =8; //digitalpin 5 for left motor forward
 const int LeftmotorB =7; //digital pin 6 for right motor Backward

const int lightsA  = 4; 
const int lightsB  = 12;
const int buzzer = 6 ; 
int state;
int vSpeed=200;     // Default speed, from 0 to 255

Servo myservo; 

 
int i=0;
int j=0;
 
int LeftDistance=0;
int RightDistance=0;
int distance=0;
int val;
String message="";

int FrontDistance=0;
NewPing sonar(2, 3, 400); //(trig,echo,maxdistance)
SoftwareSerial BT(0,1);  //Assigning arduino's (RXD,TXD)
void setup()
{
  // Setup LED
 
  pinMode(RightmotorF, OUTPUT);//declaring these pins as output to control them
    pinMode(RightmotorB, OUTPUT);
    pinMode(LeftmotorF,OUTPUT);
    pinMode(LeftmotorB,OUTPUT);
    pinMode(lightsA, OUTPUT);
    pinMode(lightsB, OUTPUT);
    pinMode(buzzer, OUTPUT);
    
  Serial.begin(9600);
 myservo.attach(5);//telling the code that servo is at digital pin 12
  
 // BT.begin(115200);
  
 // BT.print("$$");//Bluetooth stuff dont change
 
  //delay(100);
  
//  BT.println("U,9600,N");
 
 // BT.begin(9600);
   
}

void loop()
{
 
//Save income data to variable 'state'
    
 // if(BT.available())
  //{
 // char GetBT = (char)BT.read();
      
  
  
 if(Serial.available() > 0){     
     state = Serial.read();  
    }
       //Change speed if state is equal from 0 to 4. Values must be from 0 to 255 (PWM)
    if (state  == '0'){
      vSpeed=0;}
    else if (state  == '1'){
      vSpeed=100;}
    else if (state  == '2'){
      vSpeed=180;}
    else if (state  == '3'){
      vSpeed=200;}
    else if (state  == '4'){
      vSpeed=255;}


       if(state =='F'){digitalWrite(RightmotorF,vSpeed);//for forward movement both R  & L forwards are high and backward low
        digitalWrite(RightmotorB,0); digitalWrite(LeftmotorF,0); digitalWrite(LeftmotorB,0);
        digitalWrite(lightsB, LOW);}
         
   else if(state=='S'){ digitalWrite(RightmotorF, LOW);
        digitalWrite(RightmotorB, LOW); digitalWrite(LeftmotorF,LOW); digitalWrite(LeftmotorB,LOW);
        digitalWrite(lightsB, HIGH);}
         
  else if(state=='B'){digitalWrite(RightmotorF, 0);
        digitalWrite(RightmotorB,vSpeed ); digitalWrite(LeftmotorF,0); digitalWrite(LeftmotorB,0);
        digitalWrite(lightsB, 0);}
        
  else if(state=='L'){ digitalWrite(RightmotorF, LOW);
       digitalWrite(RightmotorB, LOW); digitalWrite(LeftmotorF,HIGH); digitalWrite(LeftmotorB,LOW);
       digitalWrite(lightsB, LOW);}
       
   else if(state=='R'){ digitalWrite(RightmotorF, LOW);
     digitalWrite(RightmotorB, LOW); digitalWrite(LeftmotorF,LOW); digitalWrite(LeftmotorB,HIGH);
     digitalWrite(lightsB, LOW);}

   else if(state =='W'){
      if (i==0){  
         digitalWrite(lightsA, HIGH); 
         i=1;
      }
      else if (i==1){
         digitalWrite(lightsA, LOW); 
         i=0;}
     state='n';
    }
}
  
 
     
  if(state =='o')
   {
     while(1)
    {
      
     
      scan();//checking the distance
   
      FrontDistance=distance;
      
     // if(FrontDistance>20|| FrontDistance==0)
        if(FrontDistance>20)
      {
      
       Forward(); 
      }
      else
       {
        movestop();
        navigate();
       
         
         
      }
        
    
    if(BT.available())
    {
          char newBT = (char)BT.read();
        
        if(newBT=='x'){movestop();break;}
    }
     }
   
   
  
   }
   else if(state=='t')
   {
     scan();
   //  Serial.println(distance);
     BT.print(distance);
   }
   
   
    
   }
    
   
 
    

  

 

   
    
    
  
  
  if(Serial.available())
  {
 //this part can be used to test through serial monitor for the motors
    char al=(char)Serial.read();
      
       //  int uS = sonar.ping();
    //   Serial.print("Ping: ");
    //   Serial.print(uS / US_ROUNDTRIP_CM);
     //  Serial.println("cm");
     if(al=='F'){ digitalWrite(RightmotorF,vSpeed);
        digitalWrite(RightmotorB,0); digitalWrite(LeftmotorF,0); digitalWrite(LeftmotorB,0);} 
   else if(al=='S'){digitalWrite(RightmotorF, LOW);
        digitalWrite(RightmotorB, LOW); digitalWrite(LeftmotorF,LOW); digitalWrite(LeftmotorB,LOW);} 
  else if(al=='B'){digitalWrite(RightmotorF, 0);
        digitalWrite(RightmotorB,vSpeed); digitalWrite(LeftmotorF,0); digitalWrite(LeftmotorB,0);  }
  else if(al=='L'){ digitalWrite(RightmotorF, LOW);
       digitalWrite(RightmotorB, LOW); digitalWrite(LeftmotorF,HIGH); digitalWrite(LeftmotorB,LOW);}
   else if(al=='R'){ digitalWrite(RightmotorF, LOW);
     digitalWrite(RightmotorB, LOW); digitalWrite(LeftmotorF,LOW); digitalWrite(LeftmotorB,HIGH);
      
   }
   
    
    Serial.print(al);
   
   
   
  
    
  }
 
 
}
void serv(int a)
{
    val=map(a,0,1023,0,179);
  myservo.write(val);
  delay(1000);
}
void Forward()
{
  digitalWrite(RightmotorF, HIGH);
        digitalWrite(RightmotorB,LOW); digitalWrite(LeftmotorF,LOW); digitalWrite(LeftmotorB,LOW);
}
void Backward()
{
 
        digitalWrite(RightmotorF, LOW);
        digitalWrite(RightmotorB,  HIGH); digitalWrite(LeftmotorF,LOW); digitalWrite(LeftmotorB,LOW);
}
void Right()
{
  digitalWrite(RightmotorF, LOW);
     digitalWrite(RightmotorB, LOW); digitalWrite(LeftmotorF,LOW); digitalWrite(LeftmotorB,HIGH);
}
void Left()
{
  
     digitalWrite(RightmotorF, LOW);
       digitalWrite(RightmotorB, LOW); digitalWrite(LeftmotorF,HIGH); digitalWrite(LeftmotorB,LOW);
}  
void movestop()
{
  digitalWrite(RightmotorF, LOW);
        digitalWrite(RightmotorB, LOW); digitalWrite(LeftmotorF,LOW); digitalWrite(LeftmotorB,LOW);
   
}
void scan()
{
 int uS = sonar.ping();
      
  distance=(uS / US_ROUNDTRIP_CM); 
 delay(500);
}

  void navigate()
{
   
    serv(1023);                                //Move the servo to the left (my little servos didn't like going to 180 so I played around with the value until it worked nicely)
                                          //Wait half a second for the servo to get there
    scan();                                           //Go to the scan function
     LeftDistance = distance;                          //Set the variable LeftDistance to the distance on the left
    
 
   serv(10);                                  //Move the servo to the right
                                        //Wait half a second for the servo to get there
    scan();                                           //Go to the scan function
    RightDistance = distance;                         //Set the variable RightDistance to the distance on the right
    
    
    if(abs(RightDistance - LeftDistance) < 5)
    {
      Backward();                                  //Go to the moveBackward function
      delay(200);                                      //Pause the program for 200 milliseconds to let the robot reverse
     Right();                                     //Go to the moveRight function
      delay(100);       //Pause the program for 200 milliseconds to let the robot turn right
   serv(512);
  }
    else if(RightDistance < LeftDistance)                  //If the distance on the right is less than that on the left then...
    {
    Left();                                      //Go to the moveLeft function
     delay(100);       //Pause the program for half a second to let the robot turn
   serv(512);
  }
    else if(LeftDistance < RightDistance)             //Else if the distance on the left is less than that on the right then...
    {
     Right();                                     //Go to the moveRight function
     delay(100);                                      //Pause the program for half a second to let the robot turn
   serv(512);
  }
}

Does not work speed control (((

And here it works completely. But there is no ULTRASONIC SENSOR ((
[code/*
 * Created by Vasilakis Michalis // 12-12-2014 ver.2
 * Project: Control RC Car via Bluetooth with Android Smartphone
 * More information at www.ardumotive.com
 */
 
//L293 Connection   
  const int motorA1  = 10;  // Pin  2 of L293 
  const int motorA2  = 9;  // Pin  7 of L293
  const int motorB1  = 8; // Pin 10 of L293 IN3
  const int motorB2  =7;  // Pin 14 of L293 EN2
//Leds connected to Arduino UNO Pin 4
  const int lights  = 4;
//Buzzer / Speaker to Arduino UNO Pin 6
  const int buzzer = 6 ;   
//Bluetooth (HC-06 JY-MCU) State pin on pin 2 of Arduino
  const int BTState = 2;
//Calculate Battery Level
  const float maxBattery = 8.0;// Change value to your max battery voltage level! 
  int perVolt;                 // Percentage variable 
  float voltage = 0.0;         // Read battery voltage
  int level;
// Use it to make a delay... without delay() function!
  long previousMillis = -1000*10;// -1000*10=-10sec. to read the first value. If you use 0 then you will take the first value after 10sec.  
  long interval = 1000*10;       // interval at which to read battery voltage, change it if you want! (10*1000=10sec)
  unsigned long currentMillis;   //unsigned long currentMillis;
//Useful Variables
  int i=0;
  int j=0;
  int state;
  int vSpeed=200;     // Default speed, from 0 to 255

void setup() {
    // Set pins as outputs:
    pinMode(motorA1, OUTPUT);
    pinMode(motorA2, OUTPUT);
    pinMode(motorB1, OUTPUT);
    pinMode(motorB2, OUTPUT);
    pinMode(lights, OUTPUT); 
    pinMode(BTState, INPUT);    
    // Initialize serial communication at 9600 bits per second:
    Serial.begin(9600);
}
void loop() {
  //Stop car when connection lost or bluetooth disconnected
//     if(digitalRead(BTState)==LOW) { state='S'; }

  //Save income data to variable 'state'
    if(Serial.available() > 0){     
      state = Serial.read();   
    }
  
  //Change speed if state is equal from 0 to 4. Values must be from 0 to 255 (PWM)
    if (state == '0'){
      vSpeed=0;}
    else if (state == '1'){
      vSpeed=100;}
    else if (state == '2'){
      vSpeed=180;}
    else if (state == '3'){
      vSpeed=200;}
    else if (state == '4'){
      vSpeed=255;}
 	  
  /***********************Forward****************************/
  //If state is equal with letter 'F', car will go forward!
    if (state == 'F') {
    	analogWrite(motorA1, vSpeed); analogWrite(motorA2, 0);
        analogWrite(motorB1, 0);      analogWrite(motorB2, 0); 
    }
  /**********************Forward Left************************/
  //If state is equal with letter 'G', car will go forward left
    else if (state == 'G') {
    	analogWrite(motorA1, vSpeed); analogWrite(motorA2, 0);  
        analogWrite(motorB1, 200);    analogWrite(motorB2, 0); 
    }
  /**********************Forward Right************************/
  //If state is equal with letter 'I', car will go forward right
    else if (state == 'I') {
      	analogWrite(motorA1, vSpeed); analogWrite(motorA2, 0); 
        analogWrite(motorB1, 0);      analogWrite(motorB2, 200); 
    }
  /***********************Backward****************************/
  //If state is equal with letter 'B', car will go backward
    else if (state == 'B') {
    	analogWrite(motorA1, 0);   analogWrite(motorA2, vSpeed); 
        analogWrite(motorB1, 0);   analogWrite(motorB2, 0); 
    }
  /**********************Backward Left************************/
  //If state is equal with letter 'H', car will go backward left
    else if (state == 'H') {
    	analogWrite(motorA1, 0);   analogWrite(motorA2, vSpeed); 
        analogWrite(motorB1, 200); analogWrite(motorB2, 0); 
    }
  /**********************Backward Right************************/
  //If state is equal with letter 'J', car will go backward right
    else if (state == 'J') {
    	analogWrite(motorA1, 0);   analogWrite(motorA2, vSpeed); 
        analogWrite(motorB1, 0);   analogWrite(motorB2, 200); 
    }
  /***************************Left*****************************/
  //If state is equal with letter 'L', wheels will turn left
    else if (state == 'L') {
    	analogWrite(motorA1, 0);   analogWrite(motorA2, 0); 
        analogWrite(motorB1, 200); analogWrite(motorB2, 0); 
    }
  /***************************Right*****************************/
  //If state is equal with letter 'R', wheels will turn right
    else if (state == 'R') {
    	analogWrite(motorA1, 0);   analogWrite(motorA2, 0); 
        analogWrite(motorB1, 0);   analogWrite(motorB2, 200); 		
    }
  /************************Lights*****************************/
  //If state is equal with letter 'W', turn leds on or of off
    else if (state == 'W') {
      if (i==0){  
         digitalWrite(lights, HIGH); 
         i=1;
      }
      else if (i==1){
         digitalWrite(lights, LOW); 
         i=0;
      }
      state='n';
    }
  /**********************Horn sound***************************/
  //If state is equal with letter 'V', play (or stop) horn sound
    else if (state == 'V'){
      if (j==0){  
         tone(buzzer, 1000);//Speaker on 
         j=1;
      }
      else if (j==1){
         noTone(buzzer);    //Speaker off 
         j=0;
      }
      state='n';  
    }
  /************************Stop*****************************/
  //If state is equal with letter 'S', stop the car
    else if (state == 'S'){
        analogWrite(motorA1, 0);  analogWrite(motorA2, 0); 
        analogWrite(motorB1, 0);  analogWrite(motorB2, 0);
    }
  /***********************Battery*****************************/
  //Read battery voltage every 10sec.
    currentMillis = millis();
    if(currentMillis - (previousMillis) > (interval)) {
       previousMillis = currentMillis; 
       //Read voltage from analog pin A0 and make calibration:
       voltage = (analogRead(A0)*5.015 / 1024.0)*11.132;
       //Calculate percentage...
       perVolt = (voltage*100)/ maxBattery;
       if      (perVolt<=75)               { level=0; }
       else if (perVolt>75 && perVolt<=80) { level=1; }    //        Battery level
       else if (perVolt>80 && perVolt<=85) { level=2; }    //Min ------------------------   Max
       else if (perVolt>85 && perVolt<=90) { level=3; }    //    | 0 | 1 | 2 | 3 | 4 | 5 | >
       else if (perVolt>90 && perVolt<=95) { level=4; }    //    ------------------------
       else if (perVolt>95)                { level=5; }   
       Serial.println(level);    
    }
    
}


]

This part does not work here.

if(Serial.available() > 0){     
     state = Serial.read();  
    }
       //Change speed if state is equal from 0 to 4. Values must be from 0 to 255 (PWM)
    if (state  == '0'){
      vSpeed=0;}
    else if (state  == '1'){
      vSpeed=100;}
    else if (state  == '2'){
      vSpeed=180;}
    else if (state  == '3'){
      vSpeed=200;}
    else if (state  == '4'){
      vSpeed=255;}


       if(state =='F'){digitalWrite(RightmotorF,vSpeed);//for forward movement both R  & L forwards are high and backward low
        digitalWrite(RightmotorB,0); digitalWrite(LeftmotorF,0); digitalWrite(LeftmotorB,0);
        digitalWrite(lightsB, LOW);}
if(Serial.available() > 0){     
     state = Serial.read();  
    }
       //Change speed if state is equal from 0 to 4. Values must be from 0 to 255 (PWM)
    if (state  == '0'){
      vSpeed=0;}
    else if (state  == '1'){
      vSpeed=100;}
    else if (state  == '2'){
      vSpeed=180;}
    else if (state  == '3'){
      vSpeed=200;}
    else if (state  == '4'){
      vSpeed=255;}


       if(state =='F'){
       digitalWrite(RightmotorF,vSpeed);//for forward movement both R  & L forwards are high and backward low
        digitalWrite(RightmotorB,0); 
        digitalWrite(LeftmotorF,0); 
        digitalWrite(LeftmotorB,0);
        digitalWrite(lightsB, LOW);
}

Add a Serial.print (state); after the read and see what value you really get. Try turning off carriage return and line feed in the serial monitor (lower left option), which your code is not accounting for from what you have posted.

digitalWrite(RightmotorF,vSpeed);//

That needs to be analogWrite. digitalWrite will just make the pin 0, or 1.

Thank you very much. Tomorrow I'll try our night already.