Line Following RC Car

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("");
}

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?

7 below:

http://forum.arduino.cc/index.php/topic,148850.0.html

  if (line = 500)

More of the usual.

  { (BRAKE_A, HIGH);

What's that?

Don't forget the code tags when posting code.

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

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… :slight_smile:

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

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.

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:

//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);
}

myPin.JPG

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!!!!!!!!!

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… :slight_smile: