Fallo en el código con el sensor de ultrasonidos HCSR04

Ante todo pedir perdón por mi otro post soy un novato que ademas no sabe borrarlo.
A ver si me podéis ayudar saque un código para usar un coche vía bluetooth.
Aquí el código original: Arduino Bluetooth RC Car (Android Controlled) - Ardumotive Arduino Greek Playground

El código con el sensor metido y la pantalla:

Al meter esto en el void loop el bluetooth deja de responder si metes la primera parte que pone la distancia en la pantalla funciona con un retraso increíble y con la segunda funciona bien solo que no se puede meter por lugares pequeños por bluetooth.

Primera parte:
// medidor
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(ultrasonic.Ranging(CM));
lcd.print("cm");

delay(100);

Segunda parte:
// freno de distancia
distancia = ultrasonic.Ranging(CM);
if( distancia < 45){
pausa = ultrasonic.Ranging(CM) * 10;

analogWrite(motorA1, 0); analogWrite(motorA2, 200);
analogWrite(motorB1, 0); analogWrite(motorB2, 0);
delay(pausa);
}
else if (distancia < 55){
analogWrite(motorA1, 0); analogWrite(motorA2, 0);
analogWrite(motorB1, 0); analogWrite(motorB2, 0);
delay(pausa);
}

Si me ayudáis a ponerlo para que no cuelgue y reaccione bien con el blutu lo agradecería.
He pensado que a lo mejor se podría poner con una letra esto funcionaria? así:
/Seguridad**/
//If state is equal with letter 'H', car will go backward left
else if (state == 'R') {
// freno de distancia
distancia = ultrasonic.Ranging(CM);
if( distancia < 45){
pausa = ultrasonic.Ranging(CM) * 10;

analogWrite(motorA1, 0); analogWrite(motorA2, 200);
analogWrite(motorB1, 0); analogWrite(motorB2, 0);
delay(pausa);
}
else if (distancia < 55){
analogWrite(motorA1, 0); analogWrite(motorA2, 0);
analogWrite(motorB1, 0); analogWrite(motorB2, 0);
delay(pausa);
}
}

Te he pedido por privado algunas correcciones a tu post. Por favor prestales atención.

Este código contempla que se detenga si distancia es menor a 45 cmts pero lo de 55 cmts es confuso.

/*
 * Created by Vasilakis Michalis // 12-12-2014 ver.2
 * Project: Control RC Car via Bluetooth with Android Smartphone
 * More information at www.ardumotive.com
 */
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
//                     Addr, En, Rw, Rs, d4, d5, d6, d7, backlighpin, polarity
LiquidCrystal_I2C lcd( 0x27, 2,   1,  0,  4,  5,  6,  7,           3, POSITIVE );
#define  LED_OFF  0
#define  LED_ON   1

#include "Ultrasonic.h"

// Definimos el sensor ultrasónico
 Ultrasonic ultrasonic(7,8); //pin trig, pin echo
 int distancia; // variable de distancia del ultrasonico

//L293 Connection   
  const int motorA1  =  5;  // Pin  2 of L293
  const int motorA2  =  6;  // Pin  7 of L293
  const int motorB1  = 10; // Pin 10 of L293
  const int motorB2  =  9;  // Pin 14 of L293
//Leds connected to Arduino UNO Pin 12
  const int lights   = 12;
//Buzzer / Speaker to Arduino UNO Pin 3
  const int buzzer   = 3 ;   
//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);

    // parte del codigo de la pantalla
    lcd.begin (16,2);  // inicializar lcd
    // Activamos la retroiluminacion
    lcd.setBacklight(LED_ON);
    lcd.print("testing...");
}
 
void loop() {
    

    state = 0;
	// medidor
	distancia = ultrasonic.Ranging(CM);
 	lcd.clear();
 	lcd.setCursor(0, 0);
 	lcd.print(distancia);
 	lcd.print("cm")


    //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();   
    }

    if (distancia < 45)
        state = 'S';


    //Change speed if state is equal from 0 to 4. Values must be from 0 to 255 (PWM)
    switch (state) {
    	    case '0': vSpeed = 0;
    	   	   		  break;
            case '1': vSpeed = 100;
           			  break;
            case '2': vSpeed = 180;
                      break;
            case '3': vSpeed = 200;
                      break;
            case '4': vSpeed = 255;
                      break;
    		/***********************Forward****************************/
  			// If state is equal with letter 'F', car will go forward!
    	    case 'F': analogWrite(motorA1, vSpeed); 
    	   			  analogWrite(motorA2, 0);
        			  analogWrite(motorB1, 0);      
        			  analogWrite(motorB2, 0); 
        			  break; 
			/**********************Forward Left************************/
			// If state is equal with letter 'G', car will go forward left
    		case 'G': analogWrite(motorA1, vSpeed); 
    				  analogWrite(motorA2, 0);  
        			  analogWrite(motorB1, 200);    
        			  analogWrite(motorB2, 0); 
        			  break;
			/**********************Forward Right************************/
			// If state is equal with letter 'I', car will go forward right
    		case 'I': analogWrite(motorA1, vSpeed); 
    				  analogWrite(motorA2, 0); 
        			  analogWrite(motorB1, 0);      
        			  analogWrite(motorB2, 200); 
        			  break;

			/***********************Backward****************************/
			//If state is equal with letter 'B', car will go backward
    		case 'B': analogWrite(motorA1, 0);   
    				  analogWrite(motorA2, vSpeed); 
				      analogWrite(motorB1, 0);   
				      analogWrite(motorB2, 0); 
				      break;
  			/**********************Backward Left************************/
		  	//If state is equal with letter 'H', car will go backward left
		    case 'H': analogWrite(motorA1, 0);   
		    		  analogWrite(motorA2, vSpeed); 
		        	  analogWrite(motorB1, 200); 
		        	  analogWrite(motorB2, 0); 
		        	  break;
		  	/**********************Backward Right************************/
		  	//If state is equal with letter 'J', car will go backward right
		    case 'J': analogWrite(motorA1, 0);   
		    		  analogWrite(motorA2, vSpeed); 
		        	  analogWrite(motorB1, 0);   
		        	  analogWrite(motorB2, 200); 
                  	  break;
		  	/***************************Left*****************************/
		  	//If state is equal with letter 'L', wheels will turn left
		    case 'L': analogWrite(motorA1, 0);   
		              analogWrite(motorA2, 0); 
		        	  analogWrite(motorB1, 200); 
		        	  analogWrite(motorB2, 0); 
                      break;
		  	/***************************Right*****************************/
		  	//If state is equal with letter 'R', wheels will turn right
		    case 'R': analogWrite(motorA1, 0);   
		              analogWrite(motorA2, 0); 
		        	  analogWrite(motorB1, 0);   
		        	  analogWrite(motorB2, 200); 		
                      break;
		  	/************************Lights*****************************/
		  	//If state is equal with letter 'W', turn leds on or of off
		    case 'W': if (i==0){  
				         digitalWrite(lights, HIGH); 
				         i=1;
				      }
				      else if (i==1){
				         digitalWrite(lights, LOW); 
				         i=0;
				      }
				      state='n';
                  	  break;
			/**********************Horn sound***************************/
		  	//If state is equal with letter 'V', play (or stop) horn sound
		    case 'V': if (j==0){  
				         tone(buzzer, 1000);//Speaker on 
				         j=1;
				      }
				      else if (j==1){
				         noTone(buzzer);    //Speaker off 
				         j=0;
				      }
				      state='n';  
                  	  break;
		  	/************************Stop*****************************/
		  	//If state is equal with letter 'S', stop the car
		    case 'S': analogWrite(motorA1, 0);  
		              analogWrite(motorA2, 0); 
		              analogWrite(motorB1, 0);  
		              analogWrite(motorB2, 0);
                      break;
    }
    /***********************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.0 / 1023.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);    
    }
    
}

Mira que no he usado delay que es lo que detiene el funcionamiento de tu auto de control.

surbyte:
Te he pedido por privado algunas correcciones a tu post. Por favor prestales atención.

Este código contempla que se detenga si distancia es menor a 45 cmts pero lo de 55 cmts es confuso.

/*
  • Created by Vasilakis Michalis // 12-12-2014 ver.2
  • Project: Control RC Car via Bluetooth with Android Smartphone
  • More information at www.ardumotive.com
    */
    #include <Wire.h>
    #include <LCD.h>
    #include <LiquidCrystal_I2C.h>
    //                    Addr, En, Rw, Rs, d4, d5, d6, d7, backlighpin, polarity
    LiquidCrystal_I2C lcd( 0x27, 2,  1,  0,  4,  5,  6,  7,          3, POSITIVE );
    #define  LED_OFF  0
    #define  LED_ON  1

#include "Ultrasonic.h"

// Definimos el sensor ultrasónico
Ultrasonic ultrasonic(7,8); //pin trig, pin echo
int distancia; // variable de distancia del ultrasonico

//L293 Connection 
  const int motorA1  =  5;  // Pin  2 of L293
  const int motorA2  =  6;  // Pin  7 of L293
  const int motorB1  = 10; // Pin 10 of L293
  const int motorB2  =  9;  // Pin 14 of L293
//Leds connected to Arduino UNO Pin 12
  const int lights  = 12;
//Buzzer / Speaker to Arduino UNO Pin 3
  const int buzzer  = 3 ; 
//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 = -100010;// -100010=-10sec. to read the first value. If you use 0 then you will take the first value after 10sec. 
  long interval   =  100010;      // interval at which to read battery voltage, change it if you want! (101000=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);

// parte del codigo de la pantalla
    lcd.begin (16,2);  // inicializar lcd
    // Activamos la retroiluminacion
    lcd.setBacklight(LED_ON);
    lcd.print("testing...");
}

void loop() {

state = 0;
// medidor
distancia = ultrasonic.Ranging(CM);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(distancia);
lcd.print("cm")

//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(); 
    }

if (distancia < 45)
        state = 'S';

//Change speed if state is equal from 0 to 4. Values must be from 0 to 255 (PWM)
    switch (state) {
        case '0': vSpeed = 0;
          break;
            case '1': vSpeed = 100;
            break;
            case '2': vSpeed = 180;
                      break;
            case '3': vSpeed = 200;
                      break;
            case '4': vSpeed = 255;
                      break;
    /Forward*****/
  // If state is equal with letter 'F', car will go forward!
        case 'F': analogWrite(motorA1, vSpeed);
        analogWrite(motorA2, 0);
          analogWrite(motorB1, 0);     
          analogWrite(motorB2, 0);
          break;
/Forward Left**/
// If state is equal with letter 'G', car will go forward left
    case 'G': analogWrite(motorA1, vSpeed);
      analogWrite(motorA2, 0); 
          analogWrite(motorB1, 200);   
          analogWrite(motorB2, 0);
          break;
/Forward Right**/
// If state is equal with letter 'I', car will go forward right
    case 'I': analogWrite(motorA1, vSpeed);
      analogWrite(motorA2, 0);
          analogWrite(motorB1, 0);     
          analogWrite(motorB2, 200);
          break;

		/***********************Backward****************************/
		//If state is equal with letter 'B', car will go backward

case 'B': analogWrite(motorA1, 0); 
      analogWrite(motorA2, vSpeed);
      analogWrite(motorB1, 0); 
      analogWrite(motorB2, 0);
      break;
  /Backward Left**/
  //If state is equal with letter 'H', car will go backward left
    case 'H': analogWrite(motorA1, 0); 
      analogWrite(motorA2, vSpeed);
          analogWrite(motorB1, 200);
          analogWrite(motorB2, 0);
          break;
  /Backward Right**/
  //If state is equal with letter 'J', car will go backward right
    case 'J': analogWrite(motorA1, 0); 
      analogWrite(motorA2, vSpeed);
          analogWrite(motorB1, 0); 
          analogWrite(motorB2, 200);
                    break;
  /Left**/
  //If state is equal with letter 'L', wheels will turn left
    case 'L': analogWrite(motorA1, 0); 
              analogWrite(motorA2, 0);
          analogWrite(motorB1, 200);
          analogWrite(motorB2, 0);
                      break;
  /Right**/
  //If state is equal with letter 'R', wheels will turn right
    case 'R': analogWrite(motorA1, 0); 
              analogWrite(motorA2, 0);
          analogWrite(motorB1, 0); 
          analogWrite(motorB2, 200);
                      break;
  /Lights*****/
  //If state is equal with letter 'W', turn leds on or of off
    case 'W': if (i==0){ 
        digitalWrite(lights, HIGH);
        i=1;
      }
      else if (i==1){
        digitalWrite(lights, LOW);
        i=0;
      }
      state='n';
                    break;
/Horn sound*****/
  //If state is equal with letter 'V', play (or stop) horn sound
    case 'V': if (j==0){ 
        tone(buzzer, 1000);//Speaker on
        j=1;
      }
      else if (j==1){
        noTone(buzzer);    //Speaker off
        j=0;
      }
      state='n'; 
                    break;
  /Stop*****/
  //If state is equal with letter 'S', stop the car
    case 'S': analogWrite(motorA1, 0); 
              analogWrite(motorA2, 0);
              analogWrite(motorB1, 0); 
              analogWrite(motorB2, 0);
                      break;
    }
    /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.0 / 1023.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);   
    }
   
}




Mira que no he usado delay que es lo que detiene el funcionamiento de tu auto de control.

Te explico yo puse que cuando la distancia es menor de 45 cm el coche va marcha atrás para que no se choque, y cuando es mayor de 55 se para. Para que no siga marcha atrás. Porque al quitarlo acelero hasta romperme un cristal jajaja

tu pusiste que cuando sea menor de 45 el coche vaya marcha atras

if( distancia < 45){

pero tambien pusiste

if( distancia < 55){

Asi que nunca hara lo que dices.
Pusiste menor no mayor.

NOTA: vuelvo a pedirte que edites el post inicial para que se vea apropiadamente como ya te he mostrado. en mis respuestas. Asi se postean códigos no simplemente pegados.
intenta no replicar una resuesta total ya que no contribuye en nada. Si es parcial como para responder a alguna afirmación no digo nada.