QTR Sensors Not Differentiating Between Colors Properly

I want my Pololu Zumo sensors to detect the difference between a black color and gray color and beep a different noise for both. The issue I am having is the 1000-1300 range is always detecting both. Here is the code I am using (heavily based off border detect). Thanks in advance.

#include <Wire.h>
#include <ZumoShield.h>

#define LED 13
#define MINE_THRESHOLD     500
#define BORDER_THRESHOLD   1500
#define REVERSE            200
#define TURN               200
#define FORWARD            200
#define REVERSE_TIME       200
#define TURN_TIME          300
#define SENSORS            6
unsigned int sensor_values[SENSORS];

ZumoBuzzer buzzer;
ZumoMotors motors;
Pushbutton button(ZUMO_BUTTON);
ZumoReflectanceSensorArray sensors(QTR_NO_EMITTER_PIN);

void CountDown()
{
  digitalWrite(LED, HIGH);
  button.waitForButton();
  digitalWrite(LED, LOW);

  for (int i = 0; i < 4 ; i++)
  {
    delay(1000);
    buzzer.playNote(NOTE_G(3), 200, 15);
  }
  delay(1000);
  buzzer.playNote(NOTE_G(4), 500, 15);
  delay(1000);
}

void setup() {
  pinMode(LED, HIGH);

  CountDown();

}

void loop() 
{
  if (button.isPressed())
  {
    motors.setSpeeds(0,0);
    button.waitForRelease();
    CountDown();
  }

  sensors.read(sensor_values);

  if (sensor_values[2] > 1500 && sensor_values[2] < 2000)
  {
    motors.setSpeeds(0, 0);
    buzzer.playNote(NOTE_G(3), 200, 15);
    delay(1000);
    motors.setSpeeds(-REVERSE, -REVERSE);
    delay(REVERSE_TIME);
    motors.setSpeeds(TURN, -TURN);
    delay(TURN_TIME);
    motors.setSpeeds(FORWARD, FORWARD);
  }
  else if (sensor_values[3] > 1500 && sensor_values[3] < 2000)
  {
    motors.setSpeeds(0, 0);
    buzzer.playNote(NOTE_G(3), 200, 15);
    delay(1000);
    motors.setSpeeds(-REVERSE, -REVERSE);
    delay(REVERSE_TIME);
    motors.setSpeeds(TURN, -TURN);
    delay(TURN_TIME);
    motors.setSpeeds(FORWARD, FORWARD);
  }
  else if (sensor_values[0] > 700 && sensor_values[0] < 1000)
  {
    motors.setSpeeds(0, 0);
    buzzer.playNote(NOTE_G(4), 500, 15); 
    delay(1000);
    motors.setSpeeds(-REVERSE, -REVERSE);
    delay(REVERSE_TIME);
    motors.setSpeeds(-TURN, TURN);
    delay(TURN_TIME);
    motors.setSpeeds(FORWARD, FORWARD);
  }
  else if (sensor_values[5] > 1000 && sensor_values[5] < 1300) 
  {
    motors.setSpeeds(0, 0);
    buzzer.playNote(NOTE_G(4), 500, 15); 
    delay(1000);
    motors.setSpeeds(-REVERSE, -REVERSE);
    delay(REVERSE_TIME);
    motors.setSpeeds(-TURN, TURN);
    delay(TURN_TIME);
    motors.setSpeeds(FORWARD, FORWARD);
  }
  else
  {
    motors.setSpeeds(FORWARD, FORWARD);
  }
}

Hello.

Can you describe in more detail what you are trying to do? You only mention black and gray colors, but your code looks like it is checking multiple sensors for various thresholds. Also, have you tried printing the values to see what the difference between your black and gray surfaces actually is? Note that the reflectance sensors do not directly measure the color, they measure the reflectivity of the surface (which can be affected by color).

Brandon

Hi

I have a sumo ring (black border) and a mine (image below) - the mine is printed out and placed inside the ring. I want my pololu zumo to detect the black border, beep a low pitch noise, turn, reverse and continue forward. However, when the pololu zumo detects the mine, I want the robot to beep a high pitch noise and avoid it. I hope this is a better explanation.

Just to clarify, you are not trying to detect any colors, just different shades of gray/black; is that right? It is not clear to me how you expect to be able to differentiate between detecting the center of the mine (which is black) and detecting the edge of the ring (which is black), or if you are trying to do that at all. You might be able to detect the gradient around the mine and react to that before crossing over it (and have that behavior different than seeing the edge of the ring). Did you try looking at the readings to see the actual values and how much they differ like I suggested?

Could you post a picture of your entire setup (e.g. the sumo ring with the “mine(s)” placed on it)? A video of the Zumo trying to detect the mine might help too.

Brandon

Yes, I don’t want to detect the centre of the mine but rather the shade / gradient colors appearing before it. However, I also want to detect the edge of the ring (black). Also, I am not sure how to look at the readings of the actual values so I would much appreciate it if you could tell me how or link a tutorial of some sort. As shown in the video, the robot beeps the same noise as the border and mine sometimes. I assume this is because the sensor detecting the border and mine is the same, however in other case it isn’t. I hope this helps. Pololu Zumo Project - YouTube

Hello.

I expect the readings to be very low when the sensors are above a white surface. When they are at the outer edge of the gradient around the mine, I suspect they will be somewhat high, and when they see the outer ring (or center of the mine), they will likely be at or near the maximum timeout value configured.

So, I suspect you could get it to work with 2 threshold values: 1 threshold between the white and edge-of-gradient values and 1 threshold for black (i.e. edge of the ring). Then, you could check each sensor to see if it is reading greater than the edge-of-gradient threshold. If it is, you can do another check to see if it is greater than the edge-of-ring threshold, and that should tell you which one you are seeing. To find those threshold values, I recommend looking at the readings you get as you pass the sensors over the different surfaces.

When you call sensors.read(sensor_values); in your code, it is reading the sensors and storing the readings in the sensor_values array. So when you use sensor_values[#] in your if statements, you are checking the reading of the specified sensor. Similarly, if you want to see the readings, you could print them out to the Arduino Serial Monitor by using 'Serial.println(sensor_values[#]);`. You can see an example of this implemented in our QTRRCRawValuesExample.ino example in the QTRSensors library, which is included in the Zumo shield library.

Brandon