Pololu QTR Reflectance Sensors Problem

Hello I have a Pololu QTR-RC Reflectance Sensors. I have a simple code to do calibration and sensor reading. My problem is that if I run the calibrate() method the delay() function starts to behave in a strange manner. E.g. delay(100) lasts for a few seconds. What can be the problem ? Something wrong with time configuration ?

Parts of the the code:

PololuQTRSensorsRC qtr((unsigned char[]) {IR1,IR2,IR3,IR4,IR5,IR6,IR7,IR8},NUM_SENSORS,2000,QTR_NO_EMITTER_PIN);
unsigned int sensorValues[NUM_SENSORS];

void calibrateQTR()
{
unsigned int counter; // used as a simple timer
for (counter=0; counter<21; counter++)
{
if (counter < 20) //|| counter >= 10)
setSpeeds(80, -80);
else
{
setSpeeds(-80,80);
delay(100);
setSpeeds(0,0);
}
qtr.calibrate();
delay(15);
}
setSpeeds(0,0);

for (counter=0; counter<31; counter++)
{
if (counter < 30) //|| counter >= 10)
setSpeeds(-80, 80);
else
{
setSpeeds(80,-80);
delay(100);
setSpeeds(0,0);
}
qtr.calibrate();
delay(15);
}
setSpeeds(0,0);
}

Hello,

Please simplify your code to the minimum possible program that demonstrates the problem (can you, for example, just have a single call to calibrate and a single delay?) and post your complete program. The complete program should just be a few lines long.

You also need to tell us what kind of board this code is running on and how it is connected to the QTR if you want help.

-Paul

I am using the following hardware:
Arduino Duemilanove ATmega 328
Pololu QTR-RC sensor ( 6 lines are connected to analog inputs, and two to digital input of Arduino)
Pololu Adjustable Boost Regulator 4-25V (9.5V for motors like in 3pi robot)
TB6612FNG Dual Motor Driver Carrier

The problem: I am trying to do the calibration like the 3pi robot ( go 90 deg right then 180 left and then 90 right again)
If I comment the qtr.calibrate() function call then everything is working normal. But when I uncomment it the robot starts to go round around it’s center. (seems that either qtr.calibrate() or dealy() are starting to slowdown). But after all the sensor seem to work normally.

My Code :

 #include <PololuQTRSensors.h>

//Define constants
#define NUM_SENSORS   8     // number of sensors used
#define TIMEOUT       2500  // waits for 2500 us for sensor outputs to go low

// Defines QTR sensor lines
#define IR1  14
#define IR2  15
#define IR3  16
#define IR4  17
#define IR5  18
#define IR6  19
#define IR7  7
#define IR8  8

// Define Motors Pins
#define MotA1 3
#define MotA2 9
#define MotB1 11
#define MotB2 10
// Define peripheral constants 
#define Lights  12
#define L_ON 1
#define L_OFF 0
#define Buzzer  5
#define B_OFF  0

//Global Objects
PololuQTRSensorsRC qtr((unsigned char[]) {IR1,IR2,IR3,IR4,IR5,IR6,IR7,IR8},NUM_SENSORS,2000,QTR_NO_EMITTER_PIN); 
unsigned int sensorValues[NUM_SENSORS];

void initBuzzer()
{
  // Set pin as output
  pinMode(Buzzer,OUTPUT);
  // mute
  analogWrite(Buzzer,0);
}

void buzzSet(unsigned char level)
{
  analogWrite(Buzzer,level);
}

void initLights()
{
  // Set pin as output
  pinMode(Lights,OUTPUT);
  // Set lights to low (turns off the lights)
  digitalWrite(Lights,LOW);
}

void lightsSet(unsigned char state)
{
  if(state == L_OFF){
  digitalWrite(Lights,LOW);
  }
  else  {
    digitalWrite(Lights,HIGH);
  }
}

void initMotors()
{
  // Set motor pins as OUTPUTs
  pinMode(MotA1,OUTPUT);
  pinMode(MotA2,OUTPUT);
  pinMode(MotB1,OUTPUT);
  pinMode(MotB2,OUTPUT);
  // Set motors to break
  analogWrite(MotA1,0);
  analogWrite(MotA2,0);
  analogWrite(MotB1,0);
  analogWrite(MotB2,0);
}

void setM1Speed(int speed)
{
  unsigned char reverse = 0;
  
  if (speed < 0) {
    speed = -speed;	// make speed a positive quantity
    reverse = 1;	// preserve the direction
  }
  else  {
    if (speed > 0xFF)	// 0xFF = 255
          speed = 0xFF;
  }
  
  if (reverse)  { // reverse drive
    analogWrite(MotA1,0);
    analogWrite(MotA2,speed);
  }
  else  { //forward drive
    analogWrite(MotA1,speed);
    analogWrite(MotA2,0);
  }
}

void setM2Speed(int speed)
{
  unsigned char reverse = 0;
  
  if (speed < 0) {
    speed = -speed;	// make speed a positive quantity
    reverse = 1;	// preserve the direction
  }
  else  {
    if (speed > 0xFF)	// 0xFF = 255
          speed = 0xFF;
  }
  
  if (reverse)  { // reverse drive
    analogWrite(MotB1,0);
    analogWrite(MotB2,speed);
  }
  else  { //forward drive
    analogWrite(MotB1,speed);
    analogWrite(MotB2,0);
  }
}

void setSpeeds(int m1Speed, int m2Speed)
{
  setM1Speed(m1Speed);
  setM2Speed(m2Speed);
}

void calibrateQTR()
{
  unsigned int counter;
  for (counter=0; counter<80; counter++)
  {
    if (counter < 20 || counter >= 60)
      setSpeeds(70, -70);
    else
      setSpeeds(-70, 70);

    qtr.calibrate();
    delay(15);
  }
  setSpeeds(0, 0);
}


void setup()
{
  Serial.begin(9600);
  delay(2000);
  initMotors();
  initLights();
  initBuzzer();
  calibrateQTR();
 
}

void loop()
{
   unsigned int position = qtr.readLine(sensorValues);
  
  //qtrrc.read(sensorValues);

  // print the sensor values as numbers from 0 to 9, where 0 means maximum reflectance and
  // 9 means minimum reflectance, followed by the line position
  unsigned char i;
  for (i = 0; i < NUM_SENSORS; i++)
  {
    Serial.print(sensorValues[i]);// * 10 / 1001);
    Serial.print(' ');
  }
  Serial.print("    ");
  Serial.println(position);
  
  delay(250);
}

Really great support guys :frowning:

Hello,

I am sorry for not getting back to you earlier. But it looks like you did not take my suggestion very seriously - you need to simplify your code to the minimal number of lines that demonstrates a problem. For example, you have not talked about the buzzer yet, so I assume you do not think that is causing a problem. So if you remove all of the buzzer code, the problem should still be there, and it will be easier to track down what is wrong.

You should be able to get your code down to just 10 or 20 lines that do not do what you want, and most likely you will discover the problem yourself along the way.

-Paul

Thanks, I’ll try and will post back the result :slight_smile: