Using both RC and Analog QTR sensors at the same time

Here is the problem I’m using the calibration examples provided with the QTR library. I used both examples to run RC and analog codes separately. Now I have to combine them in one code. However, when I combine the code for the analog and the code for the digital sensors, I have trouble making it work since the calibration code works with specific saved words (NUM_SENSORS, TIMEOUT, position, etc.). My question is how can I use the two calibration codes (first the one for the analog sensors and then the one for the digital) one after the other without changing the saved words (for example to NUM_ASENSORS and NUM_RCSENSORS, A_position and RC_position, etc.), which I have tried and it doesn’t work ?
Here are the two codes I’m trying to combine:
1. Analog

#define NUM_SENSORS             5 // number of sensors used
#define NUM_SAMPLES_PER_SENSOR  4  // average 4 analog samples per sensor reading
#define EMITTER_PIN             QTR_NO_EMITTER_PIN

QTRSensorsAnalog qtra((unsigned char[]) {0, 1, 2, 3, 4 }, 
  NUM_SENSORS, NUM_SAMPLES_PER_SENSOR, EMITTER_PIN);
unsigned int sensorValues[NUM_SENSORS];

void setup()
{
  delay(500);
  int i;
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);    // turn on LED to indicate we are in calibration mode
  for (i = 0; i < 400; i++)  // make the calibration take about 10 seconds
  {
    qtra.calibrate();       // reads all sensors 10 times at 2.5 ms per six sensors (i.e. ~25 ms per call)
  }
  digitalWrite(13, LOW);     // turn off LED to indicate we are through with calibration

  // print the calibration minimum values measured when emitters were on
  Serial.begin(9600);
  for (i = 0; i < NUM_SENSORS; i++)
  {
    Serial.print(qtra.calibratedMinimumOn[i]);
    Serial.print(' ');
  }
  Serial.println();
  
  // print the calibration maximum values measured when emitters were on
  for (i = 0; i < NUM_SENSORS; i++)
  {
    Serial.print(qtra.calibratedMaximumOn[i]);
    Serial.print(' ');
  }
  Serial.println();
  Serial.println();
  delay(1000);
}


void loop()
{
  unsigned int position = qtra.readLine(sensorValues);
  unsigned char i;
  for (i = 0; i < NUM_SENSORS; i++)
  {
    Serial.print(sensorValues[i] * 10 / 1001);
    Serial.print(' ');
  }
  Serial.print("    ");
  Serial.println(position);

}

2. Digital

#define NUM_SENSORS   1     // number of sensors used
#define TIMEOUT       2500  // waits for 2500 us for sensor outputs to go low
#define EMITTER_PIN   QTR_NO_EMITTER_PIN     // emitter is controlled by digital pin 2


QTRSensorsRC qtrrc((unsigned char[]) {3},    // sensor is connected to digital pin 3
  NUM_SENSORS, TIMEOUT, EMITTER_PIN); 
unsigned int sensorValues[NUM_SENSORS];


void setup()
{
  delay(500);
  int i;
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);    // turn on LED to indicate we are in calibration mode
  for (i = 0; i < 400; i++)  // make the calibration take about 10 seconds
  {
    qtrrc.calibrate();       // reads all sensors 10 times at 2500 us per read (i.e. ~25 ms per call)
  }
  digitalWrite(13, LOW);     // turn off LED to indicate we are through with calibration

  // print the calibration minimum values measured when emitters were on
  Serial.begin(9600);
  for (i = 0; i < NUM_SENSORS; i++)
  {
    Serial.print(qtrrc.calibratedMinimumOn[i]);
    Serial.print(' ');
  }
  Serial.println();
  
  // print the calibration maximum values measured when emitters were on
  for (i = 0; i < NUM_SENSORS; i++)
  {
    Serial.print(qtrrc.calibratedMaximumOn[i]);
    Serial.print(' ');
  }
  Serial.println();
  Serial.println();
  delay(1000);
}


void loop()
{
  unsigned int position = qtrrc.readLine(sensorValues);
 
  unsigned char i;
  for (i = 0; i < NUM_SENSORS; i++)
  {
    Serial.print(sensorValues[i] * 10 / 1001);
    Serial.print(' ');
  }
  Serial.print("    ");
  Serial.println(position);

}

Hello, Eva.

I edited your post to put your code tags around your programs, which makes them much easier to read. You can do this yourself by surrounding your code with [ code ][ /code ] tags (minus the spaces) or by clicking the “Code” button at the top of the post text box.

There is nothing inherently special about the names of the defined constants used in those programs. As you’ve found out, you cannot have two defined constants with the same names, so you should just rename them in a consistent way (e.g. do a find-replace operation in each program before trying to combine them). Note that you don’t even need to define them as constants; you could just replace them with actual numbers in your code, though this makes your code harder to read and modify later.

If you say that renaming them doesn’t work, I suspect you have other issues with your ultimate implementation. Could you post the simplest version of a merged program that you expect to work but doesn’t and describe how it behaves?

- Ben

Ok, I combined the two codes and the calibrations are working. However, I have another question now. When the analog sensors are on they give position from 0 to 4000(I am using 5 of them) and according to that position the robot moves and corrects himself so he can follow a line. The RC sensor is put under the left side in the middle of the robot. This sensor’s job will be to detect an intersection. When I calibrate, the RC sensor always give a first number between 0 and 9 (I believe that stands for brightness) and a second number which is always 0 (since I am using only 1 sensor). Since the position is always 0 no matter if the sensor is on a line or not, I want to use the brightness number (for example say: if (brightness >= 1 && brightness <=9) {velocity = 0;} or something like that) so the robot can stop on an intersection. The problem is that I don’t know what variable to use to call the value of the brightness (in the case of position, the word position is in red, do it is a saved word that can be used to call the position). Do you have any suggestions? Thank you!

Here is my code

  velocity = 55;
 // turn = 0;
 // trexRefresh(velocity, turn);   //  the serial port on the Trex Jr needs to be refreshed at 50 Hz or so (i.e. every 20 msec) 
  
  
 unsigned int sensors[NUM_SENSORS];
 position = qtra.readLine(sensorValues,  QTR_EMITTERS_ON,  0);
 unsigned int frontPosition = position;
 
   int error = frontPosition - 2000;

  //variables being defined
  deltaMotorSpeed = KP * error + KD * (error - lastError);
  lastError = error;
 
 
 
   position = qtrrc.readLine(sideSensorValues);
  
  //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 j;
  for (j = 0; j < NUM_SIDE_SENSORS; j++)
  {
    Serial.print(sideSensorValues[j] * 10 / 1001);
    Serial.print(' ');
  }
  Serial.print("    ");
  Serial.println(position);
 unsigned int sideSensors[NUM_SIDE_SENSORS];
position = qtrrc.readLine(sideSensorValues,  QTR_EMITTERS_ON,  0);
unsigned int sidePosition = position;
/*MY QUIESTION!!! unsigned int brightness = qtrrc.(some function that can call value 
of first number the sensor gives/the reflectance number which is between 0 and 9)*/

 if ( brightness >=1 && brightness <=9 ) {
   velocity = 0; }

Do you think that’s going to work if I can find out what’s the equivalent of the word “position” when it comes to reflectance?

unsigned int brightness = qtrrc.readCalibrated(sideSensorValues, QTR_EMITTERS_ON);
 if ( brightness >500 ) {
   velocity = 0; }

I don’t follow what you are saying here. Why are you getting two numbers back for one sensor?

The name of your variables doesn’t have any impact on how your program actually functions. You could call it “foo” if you wanted to. But yes, you can just read the calibrated sensor value, which should be around 0 if it’s over a reflective surface and around 1000 if it’s over a non-reflective surface (e.g. a line), and make a decision based on that reading alone. You might want to set your threshold higher and determine that you are over a line if it is above 700, not over a line if you are below 300, and maybe partially over a line between 400 and 600.

- Ben