I want to start by saying that I am really new to all this electrical stuff and any advice would be greatly appreciated. So, I am building a line following robot with the following components:
- 4 brushless motors
- Adafruit motor shield
- QTR-8RC sensor
- Arduino mega
The robot is supposed to follow a white line on a black grid. I have been trying to callibrate the sensors using the code below but the sensors are only able to detect the white line if I shine a bright light using my phone's flashlight to remove the shadow that is casted on the grid by the sensor itself. Also note that I am using analog pins instead of the digital pins on my arduino, not sure if this is causing the issue.
Code:
#include <QTRSensors.h>
// Create an instance of the QTRSensors library
QTRSensors qtr;
const uint8_t SensorCount = 8;
uint16_t sensorValues[SensorCount];
void setup()
{
// --- CHANGE 1: Set the sensor type to RC (Resistor-Capacitor) ---
qtr.setTypeAnalog();
// --- CHANGE 2: Use digital pins. Make sure you connect the sensor to these pins on your Arduino Mega ---
// A good choice on the Mega is a block of digital pins like 22-29.
qtr.setSensorPins((const uint8_t[]){A8,A9, A10, A11, A12, A13, A14, A15}, SensorCount);
// You can also set a timeout for the RC sensors, which is good practice.
// This prevents the code from getting stuck waiting for a sensor to respond. 2500 microseconds is a good starting point.
qtr.setTimeout(2500);
Serial.begin(9600);
delay(500);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH); // Turn on LED to indicate calibration is starting
// Calibrate for about 10 seconds:
// During this time, you should slide the sensor array back and forth over the black line
// and the white surface to expose it to the minimum and maximum reflectance values.
Serial.println("Starting Callibration");
for (uint16_t i = 0; i < 400; i++)
{
qtr.calibrate();
delay(20);
}
digitalWrite(LED_BUILTIN, LOW); // Turn off LED to indicate calibration is done
// Print the calibration data to the Serial Monitor for debugging.
Serial.println("Calibration complete. Minimum and Maximum sensor values:");
for (uint8_t i = 0; i < SensorCount; i++)
{
Serial.print(qtr.calibrationOn.minimum[i]);
Serial.print(' ');
}
Serial.println();
for (uint8_t i = 0; i < SensorCount; i++)
{
Serial.print(qtr.calibrationOn.maximum[i]);
Serial.print(' ');
}
Serial.println();
Serial.println();
delay(1000);
}
void loop()
{
// The readLineBlack() function returns a value from 0 to 7000 (since you have 8 sensors).
// 0 means the line is on the far left (under sensor 0).
// 3500 means the line is in the middle.
// 7000 means the line is on the far right (under sensor 7).
// It also fills the 'sensorValues' array with the current calibrated readings from each sensor.
uint16_t position = qtr.readLineWhite(sensorValues);
// Print the raw calibrated values from each sensor.
// For an RC sensor, a value of 1000 means fully black and 0 means fully white.
for (uint8_t i = 0; i < SensorCount; i++)
{
Serial.print(sensorValues[i]);
Serial.print('\t');
}
// Print the calculated line position
Serial.print(" Position: ");
Serial.println(position);
delay(250);
}


