Little car that doesn't work properly...

Hey people!

I just started working with arduino this year and for our school project we're building a sort of "mini" car wich follow a white line with 3 sensor at the front, the car seems to work properly when the middle captor is detecting the line, but when the sides captor detect it, they activate the right motor but not in the right direction... anyone as an idea how I could solve it ?

thanks in advance!

programme_voiture_1.5.ino (2.76 KB)

Fulco:
but when the sides captor detect it, they activate the right motor but not in the right direction... anyone as an idea how I could solve it ?

That sounds as if the left and right sensors are connected to the wrong inputs. Try swapping the wires.

Alternatively change this code

cg=digitalRead(ircg);
cm=digitalRead(ircm);
cd=digitalRead(ircd);

to this

cd=digitalRead(ircg);
cm=digitalRead(ircm);
cg=digitalRead(ircd);

...R

OP's code

//******* Déclaration des variables *******

//*******Capteur led**********

const int ircg = 2;
const int ircm = 3; 
const int ircd = 4;
//******Moteurs de gauche********
 
const int MgauchePWM = 6;// (ENA) permet le controle de la vitesse par MLI (PWM) modul longueur d'onde
const int Mgauche1 = 8; // (IN1)
const int Mgauche2 = 7; // (IN2)

//*****Moteurs de droite******

const int Mdroit1 = 10; // (IN3)
const int Mdroit2 = 9; // (IN4)
const int MdroitPWM = 11;// (ENB)

//*******Variables moteur*********

int vitesseD = 255; // vitesse groupe moteurs droit 
int vitesseG = 255; // vitesse groupe moteurs gauche 

//**********Variables leds**********

bool cg = false;
bool cm = false;
bool cd = false;
int capt;  

//******Activation des entrées/sorties*****
void setup()
{  
 Serial.begin(9600); //démarrage de la communication série 
    
  //Connections moteur
 
  pinMode(Mdroit1,OUTPUT);//
  pinMode(Mdroit2,OUTPUT);//
  pinMode(Mgauche1,OUTPUT);//
  pinMode(Mgauche2,OUTPUT);//
  pinMode(MdroitPWM,OUTPUT);//(ENA)Enable groupe moteur coté gauche
  pinMode(MgauchePWM,OUTPUT);//(ENB)Enable groupe moteur coté droit
  
  //Connections capteurs ligne
  
  pinMode(ircg,INPUT);    //SALUT pd de tes mort bite de cheval 
  pinMode(ircm,INPUT);
  pinMode(ircd,INPUT);

  }



//*****Fonction mouvoir******   
void avanceD(int vitA) //groupe droit vers l`avant
{
   digitalWrite(Mdroit1,HIGH);
  digitalWrite(Mdroit2,LOW);
  analogWrite(MdroitPWM,vitA);
  }
void avanceG(int vitB)//groupe gauche vers l`avant
{
  digitalWrite(Mgauche1,HIGH);
  digitalWrite(Mgauche2,LOW);
  analogWrite(MgauchePWM,vitB);
}
 void reculeD(int vitA)//tout droit
 {
  digitalWrite(Mdroit1,LOW);
  digitalWrite(Mdroit2,HIGH);
  analogWrite(MdroitPWM,vitA);
}
 void reculeG(int vitB)//groupe gauche vers l`arrière
  {
  digitalWrite(Mgauche1,LOW);
  digitalWrite(Mgauche2,HIGH);
  analogWrite(MgauchePWM,vitB);
}
void loop()

  //******Programme de déplacement******
  {
     do
  {
    cg=digitalRead(ircg);
    cm=digitalRead(ircm);
    cd=digitalRead(ircd);

    bitWrite(capt,0,cg);
    bitWrite(capt,1,cm);
    bitWrite(capt,2,cd);

    switch(capt)
      {
        case 1:
        avanceD(255);                                          
        reculeG(130);
          break;
        case 2:
        avanceD(270);
        reculeG(-50);
          break;
        case 3:
        avanceG(255);
        reculeD(130);
         break;
        case 4:
        avanceD(130);
        avanceG(130);
          break;
        case 5: 
        avanceG(270);
        reculeD(-50);
           break;
    
        default:
        avanceD (0);//stop
        avanceG (0);
       }
  }
  while ((cg || cm || cd)==0);
}

...R

So when the captor on the right detect the line it activates the 2 motors of the left side, but instead of going straight they go backward, insn't what you're telling me going to act like a mirror ? the motors will still go backward ?

P.S. thx for those quick answer!

Fulco:
So when the captor on the right detect the line it activates the 2 motors of the left side, but instead of going straight they go backward, insn't what you're telling me going to act like a mirror ? the motors will still go backward ?

I can't answer that until you tell me the result of trying my suggestion.

I don't have your hardware and I can't see your workbench.

...R

Nop it doesn't work... the wheels are just going nuts...

Fulco:
Nop it doesn't work... the wheels are just going nuts...

"Going nuts" provides me with no useful information from which to help you.

If you have changed the program please post the latest version in your next Reply and also tell us in as much detail as possible exactly what happens when you run the program.

...R

If your wheels turn left when you instruct them to turn right that's usually a wiring issue. Swap the wires leading to the motor and the wheels will turn right when you instruct them to go right.

If the left wheel turns when you want the right wheel to turn, swap the wires of the wheels.

Yes, it's as simple as that.

If that doesn't solve the problem, do go and read the sticky and follow the instructions there on how to ask an informative question and get detailed help.