I have 3 of the QTR-1RC IR Reflectance Sensors. The object is to use them for edge detection to prevent unwanted use of stairs. I snagged some code from one of the other forums and appear to have it hosed somehow. It all works but fails to return appropriate values. With a max value of 1023 per sensor, all I get is zeroes. (“0 0 0”)
I’ve included my purloined and slightly modified sketch below:
#include <PololuQTRSensors.h>
int ledPin = 13;
// create an object for your type of sensor (RC or Analog)
// in this example we have 3 sensors on analog inputs 3,4, 5, a.k.a. Arduino
// digital pins 17 - 19
PololuQTRSensorsRC qtr((unsigned char[]) {17,18,19}, 3); // edit to adjust for the
// number of sensors and desired Arduino pins for connections
void setup()
{
pinMode(ledPin, OUTPUT);
// start calibration phase and move the sensors over both
// reflectance extremes they will encounter in your application:
int i;
for (i = 0; i < 250; i++) // make the calibration take about 5 seconds
{
qtr.calibrate();
delay(20);
}
Serial.begin(9600);
}
void loop()
{
unsigned int sensors[3]; // change 3 to whatever number of sensors you are
// using
// get calibrated sensor values returned in the sensors
// array, along with the line position
// position will range from 0 to 2000, with 1000
// corresponding to the line over the middle sensor
int position = qtr.readLine(sensors);
// output to Serial monitor
Serial.print(sensors[0]);
Serial.print(" ");
Serial.print(sensors[1]);
Serial.print(" ");
Serial.print(sensors[2]);
Serial.print(" ");
Serial.println(); // add cr/lf to serial display
// if all 3 sensors see very low reflectance, take some appropriate action for
// this situation
if (sensors[0] > 750 && sensors[1] > 750 && sensors[2] > 750)
{
digitalWrite(ledPin, HIGH);
delay(2000);
digitalWrite(ledPin, LOW);
return;
}
// compute our "error" from the line position. We will make it so that the
// error is zero when the middle sensor is over the line, because this is our
// goal. Error will range from -1000 to +1000. If we have sensor 0 on the left
// and sensor 2 on the right, a reading of -1000 means that we see the line on
// the left and a reading of +1000 means we see the line on the right.
int error = position - 1000;
int leftMotorSpeed = 100;
int rightMotorSpeed = 100;
if (error < -500) // the line is on the left
leftMotorSpeed = 0; // turn left
if (error > 500) // the line is on the right
rightMotorSpeed = 0; // turn right
// set motor speeds using the two motor speed variables above
}
Thanks for any help!
Meenzal