Go Down

Topic: Arduino code car following a line (Read 99 times) previous topic - next topic

Douye

Hello,
We are french and we are in high school and we are a group of 4 for TPE.
We are building a car following a line.
We already realize the engine spinning the rear wheels and the servo motor by directing the front wheels of our car.
We have 3 sensors positioned at the front used to capture the line and thereby rotate the car according to the line drawn on the floor.
We Arduino code for the engine that works.
We tried to assemble the engine code with the servo code and sensors, which gives:


#include <Servo.h>
#define PIRD 2
#define PIRM 3
#define PIRG 4
#define MOTEUR 8
int val = 0;          
int pirState = LOW;    
int servoPin = 9;
 
Servo servo;  
 
int pos = 0;  
 
void setup()
{
servo.attach(servoPin);
pinMode(MOTEUR, OUTPUT);
pinMode(PIRD, INPUT);
pinMode(PIRM, INPUT);
pinMode(PIRG, INPUT);
Serial.begin(9600);
}
 
void loop()
 
{
Serial.println(digitalRead(PIRM));
val = digitalRead(PIRM);
if(val == LOW)          
{
  if(pirState == LOW)      
  {
    digitalWrite(MOTEUR, HIGH);
    pirState = HIGH;      
  }
}
  {
    Serial.println(digitalRead(PIRM));
    val = digitalRead(PIRM);
    if(val == HIGH);

    Serial.println(digitalRead(PIRD));
    val = digitalRead(PIRD);

    if(val == HIGH)
    if(pirState == LOW)    
   {        
     digitalWrite(MOTEUR, HIGH);
     for (pos = 0; pos <= 45; pos += 1)

     servo.write(pos);          
     delay(15);                    

    pirState = HIGH;      
   }
 }    
   {
     Serial.println(digitalRead(PIRM));
     val = digitalRead(PIRM);
     if(val == HIGH);

    Serial.println(digitalRead(PIRG));
    val = digitalRead(PIRG);

    if(val == HIGH)

    if(pirState == LOW)  
    {            
      digitalWrite(MOTEUR, HIGH);
      for (pos = 45; pos >= 0; pos -= 1)
      {
        servo.write(pos);            
         delay(15);                    
      }                      
     pirState = HIGH;      
    }
 }    
}


When performing all the connections, nothing works, could you help us?
We must make it next week so we need to help faster.
Thank you in advance :)

PaulS

Code: [Select]
if(val == HIGH);
The ; on the end is interesting...

You have useless curly braces throughout that code. You posted your code incorrectly.

You have names for variables that make no sense. PIR stands for Passive Infrared Sensor. That is NOT what you have for sensing a line.

When you have reasonable names, and no useless curly braces, I think it will be much more obvious what the problem is.

If not, explain what the code actually does, and how that differs from what you want. "It doesn't work" is just too lame for words.

Douye

Thank you for your prompt reply, we are just students without the help of our teachers ... We made some small changes, is it better?



#include <Servo.h>
#define CAPD 2
#define CAPM 3
#define CAPG 4
#define MOTEUR 8
int val = 0;           
int pirState = LOW;     
int servoPin = 9;
     
Servo servo; 
     
int pos = 0; 
     
void setup()
{
  servo.attach(servoPin);
  pinMode(MOTEUR, OUTPUT);
  pinMode(CAPD, INPUT);
  pinMode(CAPM, INPUT);
  pinMode(CAPG, INPUT);
  Serial.begin(9600);
}
     
void loop()
     
{
  Serial.println(digitalRead(CAPM));
  val = digitalRead(CAPM);
  if(val == LOW)           
  {
    if(pirState == LOW)     
   
      digitalWrite(MOTEUR, HIGH);
      pirState = HIGH;       
   
  }
    {
      Serial.println(digitalRead(CAPM));
      val = digitalRead(CAPM);
      if(val == HIGH);
 
      Serial.println(digitalRead(CAPD));
      val = digitalRead(CAPD);
 
      if(val == HIGH)
      if(pirState == LOW)     
             
      digitalWrite(MOTEUR, HIGH);
      for (pos = 0; pos <= 45; pos += 1)

      servo.write(pos);         
      delay(15);                     
 
      pirState = HIGH;       
     
   }   
     {
       Serial.println(digitalRead(CAPM));
       val = digitalRead(CAPM);
       if(val == HIGH);
 
      Serial.println(digitalRead(CAPG));
      val = digitalRead(CAPG);
 
      if(val == HIGH)
 
      if(pirState == LOW)   
                 
      digitalWrite(MOTEUR, HIGH);
      for (pos = 45; pos >= 0; pos -= 1)
         
      servo.write(pos);           
      delay(15);                   
                             
      pirState = HIGH;     
   }   
}

PaulS

Code: [Select]
int pirState = LOW;     
It does not make sense to try to assign the state of a PIR. Warm bodies moving near the sensor, or not, affect it's state.

Code: [Select]
    {
      Serial.println(digitalRead(CAPM));

What is that curly brace for?

Code: [Select]
   if(val == HIGH);
Why is there still a ; on the end of this statement?

You REALLY need some functions like goStraight(), turnLeft(), and turnRight(). You need to call those functions, instead of in-lining the code, since you have NO comments that explain WHY the code is doing what it is doing.

Douye

We tested another code we created earlier. Can you tell us if it is better suited for our project ?


#include <Servo.h> // bibliotheque servo
#define MOTEUR 8

// capteurs

int capteurDroite = A3; // analog pin 3
int capteurGauche = A4; // analog pin 4
int capteurMilieu = A5; // analog pin 5

// valeurs des capteurs

int droite = 0;
int gauche = 0;
int milieu = 0;


int seuil = 100; // seuil de détection de la bande noire, à régler en fonction de la luminosité ambiante

// servos

Servo myservo;  // création de l'objet servo
int pos = 0;

void setup(){
 // Serial.begin(9600);   // démarrage port série
 
 // servo
 

 myservo.attach(10);  // servo sur le pin 10

}

void lecture(){ // on lit les capteurs

 droite = analogRead(capteurDroite);
 gauche = analogRead(capteurGauche);
 milieu = analogRead(capteurMilieu);
}
void loop(){
 digitalWrite(13, HIGH);
 lecture();
 
 while ((gauche <=seuil && droite <= seuil && milieu <= seuil )){ // si tout est ok on avance
   digitalWrite(MOTEUR, HIGH);
   lecture();
 }
 
   if (gauche >  seuil) // si capteur extreme gauche on tourne a fond jusqu'à ce que le capteur central gauche ait récupéré la ligne
 
   digitalWrite(MOTEUR, HIGH);
    for (pos = 180; pos >= 0; pos -= 1)
       
     myservo.write(pos);            
     delay(15);            
   gauche = analogRead(capteurGauche);
 

if (droite > seuil) // // si capteur extreme droit on tourne a fond jusqu'à ce que le capteur central droit ait récupéré la ligne
  digitalWrite(MOTEUR, HIGH);
   for (pos = 0; pos <= 180; pos += 1)

     myservo.write(pos);          
     delay(15);  
   droite = analogRead(capteurDroite);
 }



We use these transmitters :
http://www.state-elec.com/honeywell/pdf/Optical%20Sensors%20SEP8736.pdf

as well as appropriate receptors but we have no reference.

PaulS

Quote
Can you tell us if it is better suited for our project ?
Does it do what you want?

AWOL

Code: [Select]
if (droite > seuil) // // si capteur extreme droit on tourne a fond jusqu'à ce que le capteur central droit ait récupéré la ligne
  digitalWrite(MOTEUR, HIGH);
   for (pos = 0; pos <= 180; pos += 1)

It's not clear (to me) if the for loop is meant to be part of the conditional.
The indentation suggests it is, but the lack of a brace suggests otherwise.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy