Problem reading&displaying QTR without the use of libraries

I have a problem reading my QTR-8A Reflectance Sensor Array from pololu.com/catalog/product/1213 and it’s all connected correctly i believe. The robot is finally line following but is acting weird and part of my problem is that I’m coding it blindly because I don’t exactly know the values of each sensor and position. I have tried to download and put the QTR sensor library into MPIDE under documents/mpide/libraries/QTRSensors to print the values to the serial monitor but I couldn’t even verify the code because it gives me error (
C:\Users\Vitaliy\Documents\mpide\libraries\QTRSensors\QTRSensors.cpp:36:21: fatal error: Arduino.h: No such file or directory compilation terminated. ).

I have tried everything from just serial print individual sensor to serialprint my “Readsensor” part from my code and many other combination and also researched everywhere and didnt find any fix. I have also tried to download the library on a diffrent computer which is 32 bit unlike my laptop is 64 bit and that didn’t help.

Here’s my code for line following without a library:

int LeftSensors, RightSensors, TotalSensorsAvg;
int LeftVelocity, RightVelocity;

//const int LEFT_MOTOR = 3, RIGHT_MOTOR = 9;
#define stby 8
#define LEFTmotor1 4
#define LEFTmotor2 6
#define RIGHTmotor1 80
#define RIGHTmotor2 81

//#define LEFT_MOTOR 3 //PWM
//#define RIGHT_MOTOR 9 //fPWM

const int LEFT_MOTOR = 3, RIGHT_MOTOR = 9;
long sensors[] = {
0, 0, 0, 0, 0, 0};
long sensors_average = 0;
int sensors_sum = 0;
int Position = 0;
int proportional = 0;
long integral = 0;
int derivative = 0;
int last_proportional = 0;
int control_value = 0;

int max_difference = 75;
float Kp = .05*200;
float Ki = .000001*100;
float Kd = 1.105*300;



void setup()
{

pinMode(stby,OUTPUT);
pinMode(LEFTmotor1,OUTPUT);
pinMode(LEFTmotor2,OUTPUT);
pinMode(RIGHTmotor1,OUTPUT);
pinMode(RIGHTmotor2,OUTPUT);
digitalWrite(stby,HIGH);

digitalWrite(LEFTmotor1,HIGH);
digitalWrite(LEFTmotor2,LOW);
digitalWrite(RIGHTmotor1,HIGH);
digitalWrite(RIGHTmotor2,LOW);
Serial.begin(9600);

}

void loop()
{
TotalSensorsAvg = (analogRead(0) + analogRead(1) + analogRead(2) + analogRead(3) + analogRead(4) + analogRead(5))/6/4;
Serial.println(TotalSensorsAvg);

{

{
ReadSensors();
GetAverage();
GetSum();
GetPosition();
GetProportional();
GetIntegral();
GetDerivative();
GetControl();
AdjustControl();
SetMotors();
}
}
}

//void Stop()
//{
//analogWrite(LEFT_MOTOR,0);
// analogWrite(RIGHT_MOTOR,0);
//}

void ReadSensors()
{
// read 6 sensors
for (int i = 0; i < 6; i++)
{
sensors[i] = analogRead(i + 1);

// round readings less than 50 down to zero to filter surface noise
if (sensors[i] < 50)
{
sensors[i] = 0;
}
}
}

void GetAverage()
{
// zero average variable of the sensors
sensors_average = 0;

for (int i = 0; i < 6; i ++)
{
sensors_average += sensors[i] * i * 1000;
}
}

void GetSum()
{
// zero variable of sum of the sensors
sensors_sum = 0;

// sum the total of all the sensors
for (int i = 0; i < 6; i++)
{
sensors_sum += int(sensors[i]);
}
}

void GetPosition()
{
// calculate the current position on the line
Position = int(sensors_average / sensors_sum);
}

void GetProportional()
{
// caculate the proportional value
proportional = Position - 2500;
}

void GetIntegral()
{
// calculate the integral value
integral = integral + proportional;
}

void GetDerivative()
{
// calculate the derivative value
derivative = proportional - last_proportional;

// store proportional value in last_proportional for the derivative calculation on next loop
last_proportional = proportional;
}

void GetControl()
{
// calculate the control value
control_value = int(proportional * Kp + integral * Ki + derivative * Kd);
}

void AdjustControl()
{
// if the control value is greater than the allowed maximum set it to the maximum
if (control_value > max_difference)
{
control_value = max_difference;
}

// if the control value is less than the allowed minimum set it to the minimum
if (control_value < -max_difference)
{
control_value = -max_difference;
}
}

void SetMotors()
{
TotalSensorsAvg = (analogRead(0) + analogRead(1) + analogRead(2) + analogRead(3) + analogRead(4) + analogRead(5))/6/4;

if (TotalSensorsAvg < 85) // on white board
{
if((TotalSensorsAvg > 30)) // catch the line
{
// if control_value is less than zero a right turn is needed to acquire the center of the line
if (control_value < 0)
{
analogWrite(RIGHT_MOTOR, max_difference + control_value);
analogWrite(LEFT_MOTOR, max_difference);
LeftVelocity = max_difference;
RightVelocity = max_difference + control_value;
}
// if control_value is greater than zero a left turn is needed to acquire the center of the line
else
{
analogWrite(RIGHT_MOTOR, max_difference);
analogWrite(LEFT_MOTOR, max_difference - control_value);
LeftVelocity = max_difference - control_value;
RightVelocity = max_difference;
}
}
if (TotalSensorsAvg <= 30) // lost the line
{
while (LeftVelocity > RightVelocity) // turn right
{
analogWrite(LEFT_MOTOR,max_difference);
// digitalWrite(6,LOW);
analogWrite(RIGHT_MOTOR,0);
// digitalWrite(81,LOW);
break;
}
while (RightVelocity > LeftVelocity) // turn left
{
analogWrite(LEFT_MOTOR,0);
//digitalWrite(6,LOW);
analogWrite(RIGHT_MOTOR,max_difference);
//digitalWrite(81,LOW);
break;
}
}
}
else if (TotalSensorsAvg > 85) //om black board

{
if((TotalSensorsAvg < 160)) // catch the line
{
if (control_value < 0)
{
analogWrite(RIGHT_MOTOR, max_difference);
analogWrite(LEFT_MOTOR, max_difference + control_value);
LeftVelocity = max_difference + control_value;
RightVelocity = max_difference;
}
// If control_value is greater than zero a left turn is needed to
// acquire the center of the line
else
{
analogWrite(RIGHT_MOTOR, max_difference - control_value);
analogWrite(LEFT_MOTOR, max_difference);
LeftVelocity = max_difference;
RightVelocity = max_difference - control_value;
}
}
if (TotalSensorsAvg >= 160) // lost the line
{
while (LeftVelocity > RightVelocity) // turn right
{
analogWrite(LEFT_MOTOR,max_difference);
//digitalWrite(6,LOW);
analogWrite(RIGHT_MOTOR,0);
//digitalWrite(81,LOW); //low is positive dircetion, high is negative direction
break;
}
while (RightVelocity > LeftVelocity) // turn left
{
analogWrite(LEFT_MOTOR,0);
//digitalWrite(6,LOW);
analogWrite(RIGHT_MOTOR,max_difference);
//digitalWrite(81,LOW);
break;
}
}
}
// else
// {
// analogWrite(RIGHT_MOTOR,max_difference);
// analogWrite(LEFT_MOTOR,max_difference);
// }
}

So, can anyone please tell me if anything in the code seems wrong that will cause the robot to line follow weirdly, how to serial print the sensor values without the use of the library and lastly I’m confused when I got to Mpide window and click Tools/programmer what option should I pick and why do I need it when the robot line followed without clicking any of the option except what board I’m using and the port.

I’m new to this chipKit and pretty new to programing in general so any help would be very helpful.

Thank you

Hi.

I am not very familiar with the chipkit, and I am not sure if it is meant to work with Arduino libraries. However, for now I think it would be good to focus on getting one thing working at a time. Have you verified that your QTR sensor is working by looking at its output with a multimeter or oscilloscope?

-Claire

Yes, I have finally got the robot to do a proper fast line following but my biggest issue is that I still can’t print out the values. I know how to print the qtr sensor values using the library, but since I can’t use the qtr library with chip kit I have to find a way to print the values without using the serial print.
Here’s a code that I have tired to run to print the values. The PID and few other bits here are extra from another code. If you could help me to print the sensor values without using library it would be extremely helpful.

int LeftSensors, RightSensors, TotalSensorsAvg;
int LeftVelocity, RightVelocity;
#define NUM_SENSORS 6 // number of sensors used
#define NUM_SAMPLES_PER_SENSOR 4 // average 4 analog samples per sensor reading
#define EMITTER_PIN 13 // emitter is controlled by digital pin 2

long sensors[] = {
0, 0, 0, 0, 0, 0};
long sensors_average = 0;
int sensors_sum = 0;
int Position = 0;
int proportional = 0;
long integral = 0;
int derivative = 0;
int last_proportional = 0;
int control_value = 0;

int max_difference = 255;
float Kp = .05300;
float Ki = .000001
100;
float Kd = 1.105*100;

void setup()
{

pinMode(0, INPUT);
pinMode(1, INPUT);
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
Serial.begin(2600);
}

void loop()
{
unsigned char i;
for (i = 0; i < NUM_SENSORS; i++)
Serial.print(analogRead(1));
Serial.print(’ ‘);
Serial.print(analogRead(2));
Serial.print(’ ‘);
Serial.print(analogRead(3));
Serial.print(’ ‘);
Serial.println(analogRead(4));
Serial.print(’ ‘);
Serial.print(analogRead(5));
Serial.print(’ ‘);
Serial.print(analogRead(6));
Serial.print(’ ');
delay(650);
}

Thanks for replying

I am glad you got your robot working. Like I said in my last post, I am not familiar with the chipkit, so I cannot offer much help in programming it. However, from a quick look at your code it seems like there might be a few things that are causing problems. For example, are you sure that you want to set the baud rate to 2600?

Separately, for future posts, please use the tags around your code to make it easier to read.

-Claire