0

I am using the Adafruit TCS34725 to sense color. I have used the Colorview code example to set it up to sense all colors, works great. The thing I want is for it to tell me if it is seeing either green or red, I don't care about the other colors.

Can anyone suggest what I can do?

2
  • if(r>200&&g<200&&b<200){println("RED!");}else if(r<200&&g>200&&b<200){println("GREEN!");} ? – Paul Apr 26 '16 at 9:53
  • Okay, it seems you have 16-bit values, but don't make it too hard. As long as there is way more red as green/blue, you can say it's red. – Paul Apr 26 '16 at 9:56
1

Based on what I could find, you will have to calibrate the sensor to recognize these colours. First, run the Colourview code. Take down the raw RGB and CLEAR values obtained for an object you consider green and another you consider red. Find out if these values are consistent, each time you power down the sensor and turn it on again. If they aren't, record how much dead zone there is i.e. the maximum difference in readings (between resets) that you get for the same colour. Then using the values you get, try this code:

#include <Wire.h>
#include "Adafruit_TCS34725.h"

#define MAX_OFFSET 50  // the maximum diff you found between readings

typedef struct {  // struct to hold raw color data 
   uint16_t red;
   uint16_t green;
   uint16_t blue;
   uint16_t clear;
} Color;

Color my_red = {3344, 3333, 23444, 3333}; //here you enter your minimum recorded values for red in the order of the struct members above
Color my_green = {22222, 4444, 5555, 3333}; //the same for green

void setup() {
  Serial.begin(9600);
  Serial.println("Color View Test!");

  if (tcs.begin()) {
    Serial.println("Found sensor");
  } else {
    Serial.println("No TCS34725 found ... check your connections");
    while (1); // halt!
  }
}

void loop() {
  uint16_t clear, red, green, blue;

  tcs.setInterrupt(false);      // turn on LED

  delay(60);  // takes 50ms to read 

  tcs.getRawData(&red, &green, &blue, &clear);

  tcs.setInterrupt(true);  // turn off LED
  if (compare_colour(&my_red, red, green, blue, clear)){
      Serial.println("I see red.");
  }
  else if (compare_colour(&my_green, red, green, blue, clear)){
      Serial.println("I see green.");
  }
}

boolean compare_color(Color * std, uint16_t r, uint16_t g, uint16_t b, uint16_c){
if ((r - std.red <= MAX_OFFSET) && (r - std.red >= 0) &&
      (g - std.green <= MAX_OFFSET) && (g - std.green >= 0) &&
      (b - std.blue <= MAX_OFFSET) && (b - std.blue >= 0) &&
      (c - std.clear <= MAX_OFFSET) && (c - std.clear >= 0))
  {
      return true;
  }
  return false;
}

You can use this code directly after you have gotten the sensor readings for red and green objects.

5
  • Where should I substitute it? Not really sure where I should put this – Dan Wilkes Jan 19 '16 at 19:34
  • Replace the colorview setup() and loop() with mine. Also copy all the extra stuff in my code (before my setup()), into the colorview code, at the same position. In the colorview code, get rid of any code at the top, concerning the RGB LEDs since you won't be using them (like the #define green_pin and so on), though this really isnt necessary. Make sure to get rid of the gammaTable created at the top though, to save memory. – SoreDakeNoKoto Jan 19 '16 at 20:50
  • I appreciate your time and answers, I have tried this with no luck.... I guess I am doing something wrong. Could you insert and replace the necessary parts of code so that I can see if this will work as I need it to? – Dan Wilkes Jan 25 '16 at 20:23
  • @DanWilkes Ok. First tell me...have you gotten the readings for a red and a green object? Using the original Colorview code? – SoreDakeNoKoto Jan 26 '16 at 17:32
  • I have edited the code. You can use it as is. Only after you have gotten the the readings though. I cant tell you the values the sensor will return for a red/green object. You must run the colorview code (no need for the RGB LEDs) and take at least 10 separate readings and then select the minimum for each colour and the also obtain the maximum difference between the maximum and minimum readings for any colour and set the MAX_OFFSET variable to this difference. – SoreDakeNoKoto Jan 26 '16 at 22:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.