Arduino rc car with 2 ultrasonic sensors

hello guys i’m making an arduino bluetooth car with 2 ultrasonic sensors for my graduation , one sensor on the front and one on the back , but it doesen’t work , works only the front one.
the purpose of the car is ,when im going forward the ultrasonic catches a obstacle stop the car ,the same in backward.this is my code thank you if you can help me

int motorA1 = 5;
int motorA2 = 6;
int motorB1 = 9;
int motorB2 = 10;
int TRIG_PIN = 12;
int ECHO_PIN = 11;
int TRIG_PIN1 = 4;
int ECHO_PIN1 = 2;
int stato;

void setup() {

pinMode(TRIG_PIN1,OUTPUT);
pinMode(ECHO_PIN1,INPUT);
pinMode(TRIG_PIN,OUTPUT);
pinMode(ECHO_PIN,INPUT);
pinMode(motorA1, OUTPUT);
pinMode(motorA2, OUTPUT);
pinMode(motorB1, OUTPUT);
pinMode(motorB2, OUTPUT);
Serial.begin(9600);
}

void loop() {

if(Serial.available() > 0){
stato = Serial.read();
}

digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long duration = pulseIn(ECHO_PIN,HIGH);

long distance = 0.034 * durata / 2;

digitalWrite(TRIG_PIN1, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN1, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN1, LOW);
durata = pulseIn(ECHO_PIN1,HIGH);

if(stato == ‘F’) { //forward
analogWrite(motorA1, 100);
analogWrite(motorA2, 0);
analogWrite(motorB1, 100);
analogWrite(motorB2,0 );
}
else if (distance<30){
analogWrite(motorA1, 0);
analogWrite(motorA2, 0);
analogWrite(motorB1, 0);
analogWrite(motorB2, 0);

}
if (stato == ‘B’) {//backward
analogWrite(motorA1, 0);
analogWrite(motorA2, 100);
analogWrite(motorB1, 0);
analogWrite(motorB2, 100);

}
else if (distance<30){
analogWrite(motorA1, 0);
analogWrite(motorA2, 0);
analogWrite(motorB1, 0);
analogWrite(motorB2, 0);
}

if (stato == ‘L’) {//Sinistra
analogWrite(motorA1, 100);
analogWrite(motorA2, 0);
analogWrite(motorB1, 0);
analogWrite(motorB2, 100);
}

if (stato == ‘R’) { // Destra
analogWrite(motorA1, 0);
analogWrite(motorA2, 100);
analogWrite(motorB1, 100);
analogWrite(motorB2, 0);
} }

   long duration = pulseIn(ECHO_PIN,HIGH);

long distance = 0.034 * durata / 2;

Whoops

what sir ?

Whoops, that code didn't compile, whoops that code cannot have done what you said it did, whoops that expression uses the wrong (and undefined, which is why the code didn't compile) variable.

briangjomemo22: what sir ?

He means "durata"is never defined. Which means you did NOT post the actual code you are using.

Paul

oh my fall,sorry.

int motorA1 = 5;
int motorA2 = 6;
int motorB1 = 9;
int motorB2 = 10;
int TRIG_PIN = 12;
int ECHO_PIN = 11;
int TRIG_PIN1 = 4;
int ECHO_PIN1 = 2;
int stato;

void setup() {

pinMode(TRIG_PIN1,OUTPUT);
pinMode(ECHO_PIN1,INPUT);
pinMode(TRIG_PIN,OUTPUT);
pinMode(ECHO_PIN,INPUT);
pinMode(motorA1, OUTPUT);
pinMode(motorA2, OUTPUT);
pinMode(motorB1, OUTPUT);
pinMode(motorB2, OUTPUT);
Serial.begin(9600);

}

void loop() {

if(Serial.available() > 0){
stato = Serial.read();
}
long durata;
long durata1;
long distanza;
long distanza1;

// Dare un corto segnale basso per poi dare un segnale alto puro:
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
durata = pulseIn(ECHO_PIN,HIGH);

distanza= 0.034 * durata / 2;
distanza1= 0.034 * durata1 / 2;

digitalWrite(TRIG_PIN1, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN1, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN1, LOW);
durata1 = pulseIn(ECHO_PIN1,HIGH);

if(stato == ‘F’) { //Avanti
analogWrite(motorA1, 100);
analogWrite(motorA2, 0);
analogWrite(motorB1, 100);
analogWrite(motorB2,0 );
}
else if (distanza<30){// Se la distanza e minore di 20 cm si ferma
analogWrite(motorA1, 0);
analogWrite(motorA2, 0);
analogWrite(motorB1, 0);
analogWrite(motorB2, 0);

}
if (stato == ‘B’) {//Indietro
analogWrite(motorA1, 0);
analogWrite(motorA2, 100);
analogWrite(motorB1, 0);
analogWrite(motorB2, 100);

}
else if (distanza<30){
analogWrite(motorA1, 0);
analogWrite(motorA2, 0);
analogWrite(motorB1, 0);
analogWrite(motorB2, 0);
}

if (stato == ‘L’) {//Sinistra
analogWrite(motorA1, 100);
analogWrite(motorA2, 0);
analogWrite(motorB1, 0);
analogWrite(motorB2, 100);
}

if (stato == ‘R’) { // Destra
analogWrite(motorA1, 0);
analogWrite(motorA2, 100);
analogWrite(motorB1, 100);
analogWrite(motorB2, 0);
} }

  distanza= 0.034 * durata / 2;
   distanza1= 0.034 * durata1 / 2;

Don't you want to measure durata1, before you base a calculation on its value?

yes sir thank you so much you are great

Yes, you need to move the calculation after you've measured the pulse width.

sorry sir , im also doing self parking , this is my code but is not good
i tap W and he starts but he goes back right and when the distance is < 7 he goes back left but when the distance is < 5 didnt stop to go forward, he continues go back

if(stato==‘W’){// go back right
analogWrite(motorA2,200);
analogWrite(motorB2,200);
}
else if(distanza1<7){ // go back left

analogWrite(motorA2,200);
analogWrite(motorB2,200);
}
else if(distanza1<5){//go front right

analogWrite(motorA1,200);
analogWrite(motorB1,200);
}
else if(distanza<7){// stop
analogWrite(motorA1,0);
analogWrite(motorB1,0);
} }

  else if(distanza1<7){ // go back left

      analogWrite(motorA2,200);    
      analogWrite(motorB2,200);
      }
      else if(distanza1<5){

Five is less than seven

sorry ?

briangjomemo22: sorry ?

That's just how numbers are - no need to apologise.

no i mean i'm not understanding what i have to do

hello guys im making a self parking car with arduino.
this is my code for the part o self parking.
the problem is cause he dont stop when he goes forward.
if you can help me help me thank you.

if(stato==‘W’){
if (distanza1 > 30){ ///backward left
analogWrite(motorA2,90);
analogWrite(motorB2,160);
}
if(distanza1 < 30 && distanza1 > 15){ //backward right

analogWrite(motorA2,160);
analogWrite(motorB2,90);
}
if(distanza1 < 15){// stop and wait
analogWrite(motorA2,0);
analogWrite(motorB2,0);
delay(1000);
while (distanza>20){ //forward
analogWrite(motorA1,100);
analogWrite(motorB1,150);

}

if(distanza1 < 15){//    stop and wait
  analogWrite(motorA2,0);
  analogWrite(motorB2,0);
  delay(1000);
  while (distanza>20){      //forward
    analogWrite(motorA1,100);
    analogWrite(motorB1,150);    
  }

Is the distanza1 likely to go from <15 to >20 in a second?

I can’t see your code, so I assume distanza1 is qualified as volatile, and is updated by some unseen, background mechanism