Calibration problems with QTR-8RC

Can’t seem to get calibrate to work (just prints 2,500 for each sensor) and also, read() just outputs 0. If I use the QTRCCExample file then both work. My code is as follows:

sensor.ino

#include <QTRSensors.h>
#include "sensor.h"

// Initialize the sensor class
sensor sensor(2500, 2);

void setup()
{

    // initialize serial:
    Serial.begin(9600);
    sensor.setup();
    sensor.calibrate();
}

void loop()
{
   sensor.run();
}

sensor.h

#include <QTRSensors.h>
#include <Arduino.h>

class sensor
{

public:

    sensor(short sensorTimeout, short emitterPin);
    ~sensor();
    void setup();
    void calibrate();
    void run();


private:

    short _sensorTimeout;           
    short _emitterPin;             
    QTRSensorsRC _arraySensors; 
    unsigned int _sensorValues[6];
};

sensor.cpp

#include "sensor.h"

sensor::sensor(short sensorTimeout, short emitterPin) :
    _sensorTimeout(sensorTimeout), _emitterPin(emitterPin)

{

}

sensor::~sensor() {}

void sensor::setup()
{


    unsigned char mainSensorArray[] =  { 31, 33, 35, 37, 39, 41 };

    QTRSensorsRC _arraySensors(mainSensorArray, 6, 2500, 2);

}

void sensor::run()
{
    _arraySensors.read(_sensorValues);

    for (int i = 0; i < 6; i++)
    {
        Serial.print(_sensorValues[i]);
        Serial.print('\t');
        return;
    }
    delay(500);
}


void sensor::calibrate()
{

    for (int i = 0; i < 500; i++)  //calibrate for sensors over full white/full black)
    {
        _arraySensors.calibrate();       // reads each sensor 10 times at 2500 us per read
    }
}

I can’t personally work out what is going wrong. Any pointers?

Hello.

It looks like there are a few errors in your code. In sensor.cpp, you have QTRSensorsRC _arraySensors(mainSensorArray, 6, 2500, 2);. This creates a local variable which has all the information about the pins and settings, but that local variable just goes away when the function returns. You should call init on the _arraySensors member. Also, the read() function does not use the calibrated values. Instead, you should use readCalibrated() in your run() function.

- Jeremy

Wonderful. That fixed my problems. Thanks Jeremy.

I was wondering whether you could answer a few other questions:

  1. For 10 sensors how long does “readCalibrated” take to function?
  2. Is it possible to have two QTRSensorsRC objects in the same project?
  3. If I started with 2x 8 sensors and made 6,6,2,2, can the two x6 share an emitter pin? And will the x2 have the emitter always on?

When reading the sensors, the IO line is first driven high for 10us to charge the capacitor. The IO line is then changed to an input and used to measure the pin until it reads low or until times out. In your code, you have the timeout set to 2500us, so it could take about 2510us to measure the sensors.

It should be possible to declare two QTRSensorsRC objects in the same project. It should also be possible to have multiple sensor arrays share an IO pin for controlling their emitters.

When breaking the QTR-8RC (or QTR-8A) sensor array into two pieces, soldering the included resistor will permanently keep the emitters on. If you want to control the emitters on the 2 sensor board, you could add a circuit similar to the one found on the QTR-8RC that uses a N-channel MOSFET.

- Jeremy