Help with Pololu QTR-8RC sensor, please!

I’m using Arduino Uno and Pololu QTR-8RC. I’m creating first line follower in my life, so please, treat with understanding
I’ve found 2 identical codes, which uses only 5 sensors of QTR-8RC, and these codes don’t work fine.

First:

 #include <AFMotor.h>
 #include <QTRSensors.h>
   
 AF_DCMotor motor1(1, MOTOR12_8KHZ );
 AF_DCMotor motor2(2, MOTOR12_8KHZ );
   
 #define KP .2
 #define KD 5
 #define M1_minumum_speed 150
 #define M2_minumum_speed 150
 #define M1_maksimum_speed 200
 #define M2_maksimum_speed 200
 #define MIDDLE_SENSOR 4
 #define NUM_SENSORS 5
 #define TIMEOUT 2500
 #define EMITTER_PIN 2
 #define DEBUG 0
   
 QTRSensorsRC qtrrc((unsigned char[]) { A4,A3,A2,A1,A0} ,NUM_SENSORS, TIMEOUT, EMITTER_PIN);
   
 unsigned int sensorValues[NUM_SENSORS];
   
 void setup()
 {
   delay(1500);
   manual_calibration();
   set_motors(0,0);
 }
   
 int lastError = 0;
 int last_proportional = 0;
 int integral = 0;
   
 void loop()
 {
   
   unsigned int sensors[5];
   int position = qtrrc.readLine(sensors);
   int error = position - 2000;
   
   int motorSpeed = KP * error + KD * (error - lastError);
   lastError = error;
   
   int leftMotorSpeed = M1_minumum_speed + motorSpeed;
   int rightMotorSpeed = M2_minumum_speed - motorSpeed;
   
   // set motor speeds using the two motor speed variables above
   set_motors(leftMotorSpeed, rightMotorSpeed);
 }
   
 void set_motors(int motor1speed, int motor2speed)
 {
   if (motor1speed > M1_maksimum_speed ) motor1speed = M1_maksimum_speed;
   if (motor2speed > M2_maksimum_speed ) motor2speed = M2_maksimum_speed;
   if (motor1speed < 0) motor1speed = 0; 
   if (motor2speed < 0) motor2speed = 0; 
   motor1.setSpeed(motor1speed); 
   motor2.setSpeed(motor2speed);
   motor1.run(FORWARD); 
   motor2.run(FORWARD);
 }
   
 void manual_calibration() {
   
   int i;
   for (i = 0; i < 250; i++){
     
     qtrrc.calibrate(QTR_EMITTERS_ON);
     delay(20);
 }
   
   if (DEBUG) {
     
     Serial.begin(9600);
     
     for (int i = 0; i < NUM_SENSORS; i++){
       
       Serial.print(qtrrc.calibratedMinimumOn[i]);
       Serial.print(' ');
 }
     Serial.println();
   
     for (int i = 0; i < NUM_SENSORS; i++){
       
       Serial.print(qtrrc.calibratedMaximumOn[i]);
       Serial.print(' ');
 }
     Serial.println();
     Serial.println();
 }
 }

Second:

 #include <PololuQTRSensors.h>
 #include <AFMotor.h>

 AF_DCMotor motor1(1, MOTOR12_8KHZ ); // PIN 11 - create motor #1 pwm
 AF_DCMotor motor2(2, MOTOR12_8KHZ ); // PIN 3 - create motor #2 pwm

 // Change the values below to suit your robot's motors, weight, wheel type, etc.
 #define KP .2
 #define KD 5
 #define M1_DEFAULT_SPEED 50
 #define M2_DEFAULT_SPEED 50
 #define M1_MAX_SPEED 70
 #define M2_MAX_SPEED 70
 #define MIDDLE_SENSOR 3
 #define NUM_SENSORS  5      // number of sensors used
 #define TIMEOUT       2500  // waits for 2500 us for sensor outputs to go low
 #define EMITTER_PIN   2     // emitter is controlled by digital pin 2
 #define DEBUG 0 // set to 1 if serial debug output needed

 PololuQTRSensorsRC qtrrc((unsigned char[]) {  18,17,16,15,14} ,NUM_SENSORS, TIMEOUT, EMITTER_PIN);

 unsigned int sensorValues[NUM_SENSORS];

 void setup()
 {
   delay(1000);
   manual_calibration(); 
   set_motors(0,0);
 }

 int lastError = 0;
 int  last_proportional = 0;
 int integral = 0;


 void loop()
 {
   unsigned int sensors[5];
   int position = qtrrc.readLine(sensors);
   int error = position - 2000;

   int motorSpeed = KP * error + KD * (error - lastError);
   lastError = error;

   int leftMotorSpeed = M1_DEFAULT_SPEED + motorSpeed;
   int rightMotorSpeed = M2_DEFAULT_SPEED - motorSpeed;

   // set motor speeds using the two motor speed variables above
   set_motors(leftMotorSpeed, rightMotorSpeed);
 }

 void set_motors(int motor1speed, int motor2speed)
 {
   if (motor1speed > M1_MAX_SPEED ) motor1speed = M1_MAX_SPEED; // limit top speed
   if (motor2speed > M2_MAX_SPEED ) motor2speed = M2_MAX_SPEED; // limit top speed
   if (motor1speed < 0) motor1speed = 0; // keep motor above 0
   if (motor2speed < 0) motor2speed = 0; // keep motor speed above 0
   motor1.setSpeed(motor1speed);     // set motor speed
   motor2.setSpeed(motor2speed);     // set motor speed
   motor1.run(FORWARD);  
   motor2.run(FORWARD);
 }


 void manual_calibration() {

   int i;
   for (i = 0; i < 250; i++)  // the calibration will take a few seconds
   {
     qtrrc.calibrate(QTR_EMITTERS_ON);
     delay(20);
   }
   if (DEBUG) { // if true, generate sensor dats via serial output
     Serial.begin(9600);
     for (int i = 0; i < NUM_SENSORS; i++)
     {
       Serial.print(qtrrc.calibratedMinimumOn[i]);
       Serial.print(' ');
     }
     Serial.println();

     for (int i = 0; i < NUM_SENSORS; i++)
     {
       Serial.print(qtrrc.calibratedMaximumOn[i]);
       Serial.print(' ');
     }
     Serial.println();
     Serial.println();
   }
 }

How can I enable all 8 sensors and does it have sense?
Thanks for any help)

Hello.

I reverted your post back to the previous version where your code was formatted properly, making it more readable. To see the correct modification, click on the pencil icon in the upper right-hand corner of the edited post and view the raw source. Please use this method to post code in the future.

I did not see anything obviously wrong with your code, and it should be fine to use just five sensors on the QTR-8RC, so I do not think that is causing the problem. Can you clarify what you mean by “these codes don’t work fine” and “does it have sense”? Can you post pictures clearly showing all the connections in your setup?

- Amanda

Thanks for editing!
All 2 versions should work good, right?

Unless your robot is set up exactly the same as the robot the code was intended for, I would not expect it to just work without any modifications.

- Amanda