Do you think my program will work?

UKHeliBob:
Why are you still doing this

    for(pos = 0; pos < 180; pos += 1)  

{                                   
      myservo.write(pos);

}


instead of


myservo.write(179);



Is your car ever going to travel in a straight line or will it stagger along in fits and starts with the LEDs flashing for 3 seconds each time it changes its mind and decides to steer the other way ?

Ohhhh sorry I just realized that I forgot to change it! And yes, the car has to go straight :smiley:

The good 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;
const int MaxSonar = 1; 
long anVolt, inches, cm2;
int sum=0;
int avgrange=60;





void setup() {
  

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

}

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){
   //variables needed to store values
    long int anVolt, cm;
    int sum=0;//Create sum variable so it can be averaged
    int avgrange=60;//Quantity of values to average (sample size)

  for(int i = 0; i < avgrange ; i++)
  {
    //Used to read in the analog voltage output that is being sent by the MaxSonar device.
    //Scale factor is (Vcc/512) per inch. A 5V supply yields ~9.8mV/in
    //Arduino analog pin goes from 0 to 1024, so the value has to be divided by 2 to get the actual inches
    anVolt = analogRead(MaxSonar)/2;
    sum += anVolt;
    delay(10);
  }  
  cm = 254* sum/avgrange/100; // 254/100 = 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
  if (valeurSonar>30 and valeurPing>30 ) 
{
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);
  
analogWrite(moteur,170);
delay(500);


}

if (valeurPing < 30 and valeurSonar <30 )
{
    analogWrite(moteur,0);
  delay(500);
  digitalWrite(rouge,HIGH);
  delay(500); 
 }
   
  if(valeurSonar>30 and valeurPing<30)
  
    { 
     analogWrite(moteur, 90);
  delay(500);
 myservo.write(0);
myservo.write(90);   
 digitalWrite(vert,HIGH);
 delay(500);
 digitalWrite(vert,LOW);
delay(500); }
  
  if(valeurSonar<30 and valeurPing>30)
{  
  analogWrite(moteur, 90);
  delay(500);
  
   myservo.write(179);  
   myservo.write(90);  
  digitalWrite(bleu,HIGH);
  delay(500);
  digitalWrite(bleu,LOW);
  delay(500); 
   } 

}

And yes, when the car goes forward, the LED should blink in Red,green blue, when it stops, light in red, when it turns right, blink in green and when it turns left, blink in blue.