Code for line follower with baby orangutan 328 and qtr-8rc

Goodnight!!

I have a problem with the programming for my white line follower in black background, the problem is in my code since it is not giving me results and I ask for your support to help me out of this hurry, then I show you the code and the Follower data to help me please, I would greatly appreciate it.

Previously I was using qtr8a sensors and the line follower worked, the problem was when I damaged my analog sensors and unfortunately I did not find the same ones and replace them with qtr8rc (digital) and I thought the programming would be simple to change, but for I have become a mess since the follower does not work.

Microcontroller: Baby Orangutan 328p
Motors: Pololu 10: 1
Sensors: QTR-8RC

Code:

#include <OrangutanDigital.h>
#include <QTRSensors.h>
#include <OrangutanMotors.h>
//------------------------------------------------------------------------------------//
//Sensores de Linea PD
#define NUM_SENSORS 6 // Numero de sensores que usa
#define NUM_SAMPLES_PER_SENSOR 5 // Muestras por sensor
#define EMITTER_PIN 2 // emitter is controlled by digital pin 2
// sensors 0 through 5 are connected to analog inputs 0 through 5, respectively
QTRSensorsRC qtrrc((unsigned char[]) {14, 15, 16, 17, 18, 19}, 
 NUM_SENSORS, NUM_SAMPLES_PER_SENSOR, EMITTER_PIN);
unsigned int sensorValues[NUM_SENSORS];
OrangutanMotors motors;
//--------------------------------------------------------------------------------------//
//Velocidad Maxima Robot//--------------------------//----------------------------------//
const int maximum = 80;
//--------------------------------------------------------------------------------------//
//VALORES PD//----------//VALORES PD//--------------------------------------------------//
int VProporcional = 1.45;
int VDerivativo = 16;
//--------------------------------------------------------------------------------------//
//Velocidad de Calibracion
int velcalibrate = 20; 
//--------------------------------------------------------------------------------------//
void setup()
{
 int inPin = 10;
 int val = 0;
 pinMode(9, OUTPUT);
 pinMode(8, OUTPUT);
 pinMode(inPin,INPUT);
 
 val = digitalRead(inPin);
 while (val == HIGH)
 {
 digitalWrite(9, HIGH);
 digitalWrite(8, HIGH);
 val = digitalRead(inPin);
 };
 if (val == LOW)
 {
 digitalWrite(9, LOW);
 digitalWrite(8, LOW); 
};
 motors.setSpeeds(0,0);// Motores detenidos 
 
 
//-------------Instrucciones para Empezar a hacer la Calibracion de Sensores--------------------------------------//
 
 delay(1500); 
 digitalWrite(9, HIGH);
 digitalWrite(8, HIGH);// Enciende el leds para indicar que se esta calibrando.
 for (int counter=0; counter<21; counter++)
 { 
 if (counter < 6 || counter >= 15)
 OrangutanMotors::setSpeeds(-velcalibrate, velcalibrate);
 else
 OrangutanMotors::setSpeeds(velcalibrate, -velcalibrate);
 qtrrc.calibrate(); 
 delay(20);
 }
 
 digitalWrite(9, LOW); // Apaga el led para indicar que se termino la calibracion.
 digitalWrite(8, LOW); 
 
 OrangutanMotors::setSpeeds(0, 0);
 
 delay(200);
 digitalWrite(9, HIGH); 
 digitalWrite(8, HIGH);
 delay(200);
 digitalWrite(9, LOW); // Parpadeo para indicar que el robot esta listo.
 digitalWrite(8, LOW);
 delay(200); // Parpadeo para indicar que el robot esta listo.
 digitalWrite(9, HIGH); 
 digitalWrite(8, HIGH); // Parpadeo para indicar que el robot esta listo.
 delay(200);
 digitalWrite(9, LOW); // Parpadeo para indicar que el robot esta listo.
 digitalWrite(8, LOW);
 delay(200);
 //---------------------------Fin Calibracion de Sensores----------------------------------------------------//
pinMode(inPin,INPUT);
 
 val = digitalRead(inPin);
 while (val == HIGH)
 {
 digitalWrite(9, HIGH);
 digitalWrite(8, HIGH);
 val = digitalRead(inPin);
 };
 if (val == LOW)
 {
 digitalWrite(9, LOW);
 digitalWrite(8, LOW);
 delay(1000); // Retardo X segundos antes de Empezar a andar 
};
}
unsigned int last_proportional = 0;
long integral = 0;
void loop()
{
 
 unsigned int position = qtrrc.readLine(sensorValues); // leemos posicion de la linea en la variable position
 
 // Referencia donde seguira la linea, mitad sensores.
 int proportional = (int)position - 2500;
 // Calculos PD
 int derivative = proportional - last_proportional;
 integral += proportional;
 last_proportional = proportional;
 
 int power_difference = proportional/VProporcional + integral*0 + derivative*VDerivativo;
 
 if (power_difference > maximum)
 power_difference = maximum;
 if (power_difference < -maximum)
 power_difference = -maximum;
 
 if (power_difference < 0)
 OrangutanMotors::setSpeeds(maximum, maximum + power_difference);
 else
 OrangutanMotors::setSpeeds(maximum - power_difference,maximum);
};

Hello.

Which pins are you using on your Baby Orangutan for the QTR-8RC sensors? Can you post pictures clearly showing the connections between the sensor array and the Baby Orangutan? Also, can you provide more details on what your line follower is suppose to do and what it is actually doing, and, if possible, can you post a video showing the behavior?

- Amanda

Yes, this is the connection diagram that I made.

And as for what it does is after I turn it on and after it calibrates it just turns the left motor.

Correction.

This is the true diagram.

Thanks for posting a nice wiring diagram of your robot. I suspect your power source is not able to provide enough current to run all the components in your system. What kind of power supply are you using (e.g. LiPo battery)? Can you provide a link to its specifications?

By the way, it looks like there are no current-limiting resistors connected between your LEDs and the Baby Orangutan controller. Driving an LED from an I/O line without an appropriate current-limiting resistor risks damaging both the LED and I/O pin, so I recommend adding those resistors.

- Amanda

Hello!
Did you solve your problem? I had the same problem and figured it out that it was the QTR reading, I solved it by simply changing the pins labels, that is, intead of:

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

I repleaced it with:

QTRSensorsRC qtrrc((unsigned char[]) {A4, A3, A2, A1, A0},NUM_SENSORS, TIMEOUT, EMITTER_PIN); 

It is now working for me!