QTR-8A and L298N Fast Line Tracker Robot

Hello, I am making a fast line tracker robot. But, I have trouble with my code. My robot doesn’t follow the line. I am using six of the eight sensors from the QTR (sensors 2, 3, 4, 5, 6, 7) and are connected to an arduino UNO to analog pins 0-5. Can @JonathanKosh tell me the problem?

This is my code? Also, how do I make PID?

#include <QTRSensors.h>

#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];

const int enA = 10; //L298
const int enB = 5;
const int in1 = 9; //motor a
const int in2 = 8; //motor a
const int in3 = 7; //motor b
const int in4 = 6; //motor b

const int mz80 = 4;

int distance = 0;

void setup() {
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);

  Serial.begin(9600);

  pinMode(mz80, INPUT);

  qtra.calibrate(QTR_EMITTERS_ON);

}

void loop() {
  distance = digitalRead(mz80);
  if (distance == 1) {
    next;
  }
  
  else {

  }
}

void next() {
  /*0 and 1 left (qtr sensors)
    2 and 3 middle
    4 and 5 right                    */

  unsigned int position = qtra.readLine(sensorValues);

  for (unsigned char i = 0; i < NUM_SENSORS; i++)
  {
    Serial.print(sensorValues[i]);
    Serial.print('\t');
  }

  Serial.println(position);

  if (sensorValues[2] < 50 || sensorValues[3] < 50) {
    analogWrite(10, 255);
    digitalWrite(9, 1);
    digitalWrite(8, 0);

    analogWrite(5, 255);
    digitalWrite(6, 0);
    digitalWrite(7, 1);
  }

  else {
    if (sensorValues[0] < 50 || sensorValues[1] < 50 ) {
      analogWrite(10, 100);
      digitalWrite(9, 1);
      digitalWrite(8, 0);

      analogWrite(5, 255);
      digitalWrite(6, 0);
      digitalWrite(7, 1);
    }

    else {
      if (sensorValues[4] < 50 || sensorValues[5] < 50 ) {
        analogWrite(10, 255);
        digitalWrite(9, 1);
        digitalWrite(8, 0);

        analogWrite(5, 100);
        digitalWrite(6, 0);
        digitalWrite(7, 1);
      }

    }
  }
}

Hello.

I am sorry you are having trouble getting your robot to follow a line. What, specifically, is the behavior of your robot? It would help to share a link to a video that shows this behavior. Before going into PID control, I think it is reasonable to first verify that your QTR array is reading the line well. To get a sense of that, can you post a sample of the output of Serial.println(position); from your Arduino IDE’s Serial Monitor as each sensor in the QTR array passes over a line on your line following course?

-Jon

1 Like