Go Down

Topic: Line Following RC Car (Read 3462 times) previous topic - next topic

BuckeyeTaco

Hello, I am having trouble with my science fair project. I took a cheap RC car and hooked it up to the Arduino Uno. I then connected it to a Pololu QTR-8RC reflectance array. This would make it follow the line I draw. But my when I upload my code, the car does nothing.
Here is my code:



#include <QTRSensors.h> // using Sensor Library
 
 // Sensor settings
 
 #define NUM_SENSORS             6 
 #define NUM_SAMPLES_PER_SENSOR  4 
 #define EMITTER_PIN             2


 QTRSensorsAnalog qtra((unsigned char[]) {0, 1, 2, 3, 4, 5},
  NUM_SENSORS, NUM_SAMPLES_PER_SENSOR, EMITTER_PIN);
 unsigned int sensorValues[NUM_SENSORS];
 
 // Set variables
 
int
PWM_A   = 3,  //This is the power for channel A or the front motor
DIR_A   = 12, //Direction A is digital pin 12. It is the direction for channel A
BRAKE_A = 9,  //Brake A is digital pin 3. It is the brake for the steering motor

const int
carSpeed = 125,
PWM_B   = 13,
BRAKE_B = 8,
steerSpeed = 255;



// Setup

void setup()
{
  //Setup Channel A as Front Motor (Turning)
 
  pinMode(PWM_A, OUTPUT); //Initiates Motor Channel A pin
  pinMode(BRAKE_A, OUTPUT); //Initiates Brake Channel A pin
  pinMode(DIR_A, OUTPUT);//Initiates Direction Channel A pin

  //Setup Channel B as Rear Motor (speed)

  pinMode(PWM_B, OUTPUT); //Initiates Motor Channel A pin
  pinMode(BRAKE_B, OUTPUT);  //Initiates Brake Channel A pin
  // for (int i = 0; i < 250; i++)  // make the calibration take about 10 seconds
 
  // setup monitor
 

    Serial.begin(9600); //Show data on screen
    delay(1000);
     
    qtra.calibrate();  // reads all sensors 10 times at 2500 us per read (i.e. ~25 ms per call)
   
  }
 
  void loop()
  {
    unsigned int position = qtra.readLine(sensorValues);
   
    unsigned int sensors[3];
   
    int carPosition = qtra.readLine(sensors);  // get sensor reading

    int line = carPosition - 1000;  // line = position of line
   
    analogWrite(BRAKE_B, LOW); // disables motor break
    analogWrite(PWM_B, carSpeed);  //Set rear motor speed
   
   
    // turning the car based on sensor reading
   
   
    if (line = 500)  //If line is straight,
    { (BRAKE_A, HIGH);  //Brake for channel A is high so it doesn't turn
    }
   
   if (line > 500)  //if line is on right
   { analogWrite(BRAKE_A, LOW);  // setting brake LOW disable motor brake
  analogWrite(DIR_A, HIGH);   // setting direction to HIGH the motor will spin forward and the car will turn right
  analogWrite(PWM_A, steerSpeed);
   }
   
   if (line < -500)
   {analogWrite(BRAKE_A, LOW);  // setting againg the brake LOW to disable motor brake
  analogWrite(DIR_A, LOW);    // setting direction to Low the motor will spin backwards and the car will turn left
  analogWrite(PWM_A, steerSpeed);     // Set the speed of the motor
   }
   
   // Monitor
    //Serial.print();
    Serial.println(carSpeed); 
    Serial.print("\t");
    Serial.println(steerSpeed);
    Serial.print("\t");
    Serial.println(line);
    Serial.print("\t");
    Serial.println(carPosition);
    Serial.print("\t");
 
    delay(500);
    Serial.println("");
  }

JimboZA

I don't have time to go through that code since I'm just leaving for work- 0500 here.

But what have you done so far in terms of troubleshooting? Do you know for a fact that the sensors are reading correctly? Do you get the values you expect for "line" in your serial prints?

Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

zoomkat

Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

AWOL

#3
Feb 03, 2015, 02:25 pm Last Edit: Feb 03, 2015, 02:28 pm by AWOL
Code: [Select]
  if (line = 500)
More of the usual.

Code: [Select]
  { (BRAKE_A, HIGH); 
What's that?

Don't forget the code tags when posting code.
"Pete, it's a fool (who) 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.
I speak for myself, not Arduino.

BuckeyeTaco

Sometimes the sensor will work, other times it won't I don't know what's wrong with it. Should I clean it off?

TomGeorge

Hi,

Can you please post a copy of your sketch, using code tags?
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png or pdf?

Tom.... :)
Everything runs on smoke, let the smoke out, it stops running....

BuckeyeTaco

If you want a useless code you can but I don't know how to draw a circuit

BuckeyeTaco

I've done what you guys told me to do but now the rear motor goes and the front motor will just turn right but I don't think my sensor readings are right.

JimboZA

You said in a PM to me that you can't get the serial monitor to work: you should explain here what you did to open it: Ctrl+Shift+M is the easiest way.

Try this code which for me just printed a bunch of 0s for an unconnected pin:

Code: [Select]
//really simple code for serial monitor

byte myPin=3;

void setup()
{
  pinMode(myPin, INPUT);
  Serial.begin(9600);
  Serial.println("starting");
}

void loop()
{
Serial.println(digitalRead(myPin));
delay(2000);
}
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

TomGeorge

#9
Feb 06, 2015, 12:04 pm Last Edit: Feb 06, 2015, 12:14 pm by TomGeorge
Hi,


What are you using to drive the motors?
How have you got them connected to the arduino?
How have  you got  the sensors hooked to the arduino?
What are you using to power the arduino and the motors.

Can you post a picture of your project so we can see how it is setup.

Tom...... :)
How did you connect it all up if you don't know about circuit diagrams.
Also look up how you use analogWrite , unless you have assigned values to HIGH and LOW they are useless, 0 to 255.
Unless you mean digitalWrite!!!!!!!!!
Everything runs on smoke, let the smoke out, it stops running....

TomGeorge

Hi, also looking at your sketch, the value of 500 for straight will not be a stable value, it will have noise.
So use a deadband of say 10,  
<495 line is on left
>505 line is on right
else straight ahead.

Tom.... :)
Everything runs on smoke, let the smoke out, it stops running....

Go Up