My car cannot turn when I want

Hi,it's me again :smiley: !

So I have a final problem with my car. It advances well, it stops well, everything is good. However, I want my car to turn if an other vehicule infront turns also. Nothing happens when this condition arrives.

I have two sensors, a Ping and a MaxSonar EZ2. They are set on both sides of the car. One on each. So in my program, I said that if per example the MaxSonar detects something in a range of 50cm AND the Ping doesn't detect anything, my car should turn right. However, nothing happens.

I placed an object at this position but nothing happens.

Sonar: http://imgur.com/SIUsiFW
Ping: Imgur: The magic of the Internet

And my code:

 if(cm>50 and valeurPing<50)
  
      if(cm<50 and valeurPing>50)
{  

    
    myservo.write(0); 
    delay(500);
    myservo.write(70);   
delay(500);
digitalWrite(motor,HIGH);
delay(150);
digitalWrite(motor,LOW);
delay(300); 

}

cm= Sonar and ValeurPing = Ping

if both Sonar and Ping don't detect anything in a range of 50cm, it advance and if they do, it stops so there's no problem there. Also,the servo works fine because I tested the example sweep and it worked.

What's wrong? Thanks

What's wrong?

You didn't post your code, but what values are going to satisfy these conditions

 if(cm>50 and valeurPing<50)
  
      if(cm<50 and valeurPing>50)

?

oh forget the if(cm>50 and valeurPing<50) it's an error of copy paste. If you want my whole code:

const int pingPin = 7;
int moteur= 3;
#include <Servo.h> 
int rouge = 6;
int vert = 5;
int bleu= 4;
Servo myservo;
int pos = 0;
float Inch=0.00;
float cm=0.00;
int SonarPin=A1;
int  sensorValue;





void setup() {
 Serial.begin(9600);

pinMode(moteur, OUTPUT);
pinMode(rouge, OUTPUT);
pinMode(vert,OUTPUT);
pinMode(bleu,OUTPUT);
myservo.attach(9);


}

long int acquisitionPing(void){
    long int duration;

    pinMode(pingPin, OUTPUT);
    digitalWrite(pingPin, LOW);
    delayMicroseconds(2);
    digitalWrite(pingPin, HIGH);
    delayMicroseconds(5);
    digitalWrite(pingPin, LOW);
    pinMode(pingPin, INPUT);
    duration = pulseIn(pingPin, HIGH);
    return duration / 29 / 2;
}

long int acquisitionMaxSonar(void){
  sensorValue=analogRead(SonarPin);
delay(50);
Inch= (sensorValue*0.497);
cm=Inch*2.54;
  }  



void loop()
{
  
   long int valeurPing;
   long int valeurSonar;
    // on fait l'acquisition des senseurs
   valeurPing = acquisitionPing();
   valeurSonar = acquisitionMaxSonar();
  // on traite les informations recueillies
  // par exemple
  
Serial.print(cm);
Serial.println('cm');
delay(500);
  
  
  if (valeurPing>50 and cm > 50)
{
digitalWrite(rouge,HIGH);
delay(500);
digitalWrite(rouge,LOW);
delay(500);  
digitalWrite(vert,HIGH);
delay(500);
digitalWrite(vert,LOW);
delay(500); 
digitalWrite(bleu,HIGH);
delay(500);
digitalWrite(bleu,LOW);
delay(500);
    myservo.write(70);     
digitalWrite(moteur,HIGH);
delay(150);
digitalWrite(moteur,LOW);
delay(300);



if (valeurPing < 50 and cm <50 )
{
  
      myservo.write(70);   
    analogWrite(moteur,0);
  delay(500);
  digitalWrite(rouge,HIGH);
  delay(500); 
 }
   
  if(cm>50 and valeurPing<50)
  
    { 
  
    myservo.write(179); 
    delay(500);
    myservo.write(70);   
delay(500);
digitalWrite(moteur,HIGH);
delay(150);
digitalWrite(moteur,LOW);
delay(300); 

 digitalWrite(vert,HIGH);
 delay(500);
 digitalWrite(vert,LOW);
delay(500); }
  
  if(cm<50 and valeurPing>50)
{  

 myservo.write(0); 
    delay(500);
    myservo.write(70);   
delay(500);
digitalWrite(moteur,HIGH);
delay(150);
digitalWrite(moteur,LOW);
delay(300); 
 
  digitalWrite(bleu,HIGH);
  delay(500);
  digitalWrite(bleu,LOW);
  delay(500); 
   } 

}  }

My eyes pleaded with me to reformat your code

const int pingPin = 7;
int moteur= 3;
#include <Servo.h> 
int rouge = 6;
int vert = 5;
int bleu= 4;
Servo myservo;
int pos = 0;
float Inch=0.00;
float cm=0.00;
int SonarPin=A1;
int  sensorValue;

void setup() {
  Serial.begin(9600);

  pinMode(moteur, OUTPUT);
  pinMode(rouge, OUTPUT);
  pinMode(vert,OUTPUT);
  pinMode(bleu,OUTPUT);
  myservo.attach(9);
}

long int acquisitionPing(void){
  long int duration;

  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  return duration / 29 / 2;
}

long int acquisitionMaxSonar(void){
  sensorValue=analogRead(SonarPin);
  delay(50);
  Inch= (sensorValue*0.497);
  cm=Inch*2.54;
}  

void loop()
{
  long int valeurPing;
  long int valeurSonar;
  // on fait l'acquisition des senseurs
  valeurPing = acquisitionPing();
  valeurSonar = acquisitionMaxSonar();
  // on traite les informations recueillies
  // par exemple

  Serial.print(cm);
  Serial.println('cm');
  delay(500);

  if (valeurPing>50 and cm > 50)
  {
    digitalWrite(rouge,HIGH);
    delay(500);
    digitalWrite(rouge,LOW);
    delay(500);  
    digitalWrite(vert,HIGH);
    delay(500);
    digitalWrite(vert,LOW);
    delay(500); 
    digitalWrite(bleu,HIGH);
    delay(500);
    digitalWrite(bleu,LOW);
    delay(500);
    myservo.write(70);     
    digitalWrite(moteur,HIGH);
    delay(150);
    digitalWrite(moteur,LOW);
    delay(300);

    if (valeurPing < 50 and cm <50 )
    {

      myservo.write(70);   
      analogWrite(moteur,0);
      delay(500);
      digitalWrite(rouge,HIGH);
      delay(500); 
    }

    if(cm>50 and valeurPing<50)

    { 

      myservo.write(179); 
      delay(500);
      myservo.write(70);   
      delay(500);
      digitalWrite(moteur,HIGH);
      delay(150);
      digitalWrite(moteur,LOW);
      delay(300); 

      digitalWrite(vert,HIGH);
      delay(500);
      digitalWrite(vert,LOW);
      delay(500); 
    }

    if(cm<50 and valeurPing>50)
    {  

      myservo.write(0); 
      delay(500);
      myservo.write(70);   
      delay(500);
      digitalWrite(moteur,HIGH);
      delay(150);
      digitalWrite(moteur,LOW);
      delay(300); 

      digitalWrite(bleu,HIGH);
      delay(500);
      digitalWrite(bleu,LOW);
      delay(500); 
    } 
  }  
}

Allright I will remove the french :smiley:

const int pingPin = 7;
int moteur= 3;
#include <Servo.h> 
Servo myservo;
int pos = 0;
float Inch=0.00;
float cm=0.00;
int SonarPin=A1;
int  sensorValue;





void setup() {
pinMode(motor, OUTPUT);
myservo.attach(9);

}

long int acquisitionPing(void){
    long int duration;

    pinMode(pingPin, OUTPUT);
    digitalWrite(pingPin, LOW);
    delayMicroseconds(2);
    digitalWrite(pingPin, HIGH);
    delayMicroseconds(5);
    digitalWrite(pingPin, LOW);
    pinMode(pingPin, INPUT);
    duration = pulseIn(pingPin, HIGH);
    return duration / 29 / 2;
}

long int acquisitionMaxSonar(void){
  sensorValue=analogRead(SonarPin);
delay(50);
Inch= (sensorValue*0.497);
cm=Inch*2.54;
  }  



void loop()
{
  
   long int valuePing;
   valuePing = acquisitionPing();
  
  
  if (valuePing>50 and cm > 50)
{
    myservo.write(70);     
digitalWrite(motor,HIGH);
delay(150);
digitalWrite(motor,LOW);
delay(300);


if (valuePing < 50 and cm <50 )
{
  
      myservo.write(70);   
    analogWrite(motor,0);
  delay(500);

 }
   
  if(cm>50 and valuePing<50)
  
    { 
  
    myservo.write(179); 
    delay(500);
    myservo.write(70);   
delay(500);
digitalWrite(motor,HIGH);
delay(150);
digitalWrite(motor,LOW);
delay(300); 

  
  if(cm<50 and valuePing>50)
{  

 myservo.write(0); 
    delay(500);
    myservo.write(70);   
delay(500);
digitalWrite(motor,HIGH);
delay(150);
digitalWrite(motor,LOW);
delay(300); 
   } 

}  }

So I slightly modified the format of your code, and removed some statements so you can see the overall flow and perhaps notice what your problem is:

void loop() {
  [...]

  if (valeurPing>50 and cm > 50) {
    [...]

    if (valeurPing < 50 and cm <50 ) {
      [...]
    }

    if(cm>50 and valeurPing<50) { 
      [...]
    }

    if(cm<50 and valeurPing>50) {  
      [...]
    } 
  }  
}

Take a careful look at the (formatted) code... can you see the important difference between the 4 "if" statements? And see how correctly formatting your code can help you find problems?

Hope this helps,

Brad
KF7FER

 if (valeurPing>50 and cm > 50) {
    [...]
  } 
    if (valeurPing < 50 and cm <50 ) {
      [...]
    }

    if(cm>50 and valeurPing<50) { 
      [...]
    }

    if(cm<50 and valeurPing>50) {  
      [...]
  
  }  
}

Yes I see... there was a } at the wrong place... It's just a wrong typing. I made this mistake when I modified my code. But when I tested my car, the } was at the good place.

Or maybe you are talking about something else?