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