Go Down

Topic: Why won't it work?: line following RC car (Read 753 times) previous topic - next topic

BuckeyeTaco

I am working on a project for science fair that involves a toy car. It will have a pololu QTR-8RC reflectance array that will be the input. There will be two DC motors, one in the front for steering, another in the back to make the car go. When I upload the code to the Uno, it shows no errors. But the car does nothing. I was wandering if someone could spot something wrong with my code.

Here it is:
Code: [Select]
[/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
DIR_B  = 10,
BRAKE_B = 8,
BRAKE_A = 9;  //Brake A is digital pin 3. It is the brake for the steering motor

const int
carSpeed = 125,
PWM_B   = 13,
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
  pinMode(DIR_B, OUTPUT);
  // 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()
  {
    int carPosition = qtra.readLine(sensorValues);    // get sensor reading
   
    digitalWrite(10, HIGH);
    digitalWrite(8, LOW);
    digitalWrite(3, carSpeed);
   
    unsigned int position = qtra.readLine(sensorValues);
   
    unsigned int sensorValues[3];

    int line = carPosition - 1000;  // line = position of line
     
    // 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)
   {digitalWrite(BRAKE_A, LOW);  // setting againg the brake LOW to disable motor brake
  digitalWrite(DIR_A, LOW);    // setting direction to Low the motor will spin backwards and the car will turn left
  digitalWrite(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("");
  }

Please help, I have been working on this forever and it is getting frustrating.

JimboZA

#1
Feb 05, 2015, 05:47 am Last Edit: Feb 05, 2015, 05:51 am by JimboZA
In your other thread on this exact topic, it was pointed out that....

Code: [Select]
if (line = 500)

.... is wrong, yet that's still in your code.

It should have == not =, as explained here under the heading "Warning".

I also asked you there, if the serial prints indicated that the sensors were giving the correct values. Are they?
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

AWOL

So's the other stuff pointed out.
Thread locked - post answers here

DO NOT CROSS-POST, CROSS-POSTING WASTES TIME.

Go Up