Has anyone been able to get a color sensor to work? What sensor did you use and could you share your code? Thanks.
I have tried both the TCS3414 and the TCS34725 and all the code samples I could find on the internet. I have tweaked the gains and tried modifying the code but I can't get any decent readings from either sensor. I just want my robot lawnmower to distinguish between green grass and brown earth. The sensing distance would be variable (2 to 5 inches) and the sensor should work with varying illumination brightness. Maybe I need optics?
My latest trial code is:
#include <Wire.h>
#include "Adafruit_TCS34725.h"
// Pick analog outputs, for the UNO these three work well
// use ~560 ohm resistor between Red & Blue, ~1K for green (its brighter)
#define redpin 3
#define greenpin 5
#define bluepin 6
// for a common anode LED, connect the common pin to +5V
// for common cathode, connect the common to ground
// set to false if using a common cathode LED
#define commonAnode true
// our RGB -> eye-recognized gamma color
byte gammatable[256];
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_700MS, TCS34725_GAIN_16X);
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!
}
// use these three pins to drive an LED
pinMode(redpin, OUTPUT);
pinMode(greenpin, OUTPUT);
pinMode(bluepin, OUTPUT);
// thanks PhilB for this gamma table!
// it helps convert RGB colors to what humans see
for (int i=0; i<256; i++) {
float x = i;
x /= 255;
x = pow(x, 2.5);
x *= 255;
if (commonAnode) {
gammatable[i] = 255 - x;
} else {
gammatable[i] = x;
}
//Serial.println(gammatable[i]);
}
}
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
Serial.print("C:\t"); Serial.print(clear);
Serial.print("\tR:\t"); Serial.print(red);
Serial.print("\tG:\t"); Serial.print(green);
Serial.print("\tB:\t"); Serial.println(blue);
// Figure out some basic hex code for visualization
uint32_t sum = clear;
float r, g, b;
r = red; r /= sum;
g = green; g /= sum;
b = blue; b /= sum;
r *= 256; g *= 256; b *= 256;
//Serial.print("\t");
//Serial.print((int)r, HEX); Serial.print((int)g, HEX); Serial.print((int)b, HEX);
//Serial.println();
//Serial.print((int)r ); Serial.print(" "); Serial.print((int)g);Serial.print(" "); Serial.println((int)b );
float total = r + g + b;
float redPart = r/total;
float greenPart = g/total;
float bluePart = b/total;
analogWrite(redpin, gammatable[(int)r]);
analogWrite(greenpin, gammatable[(int)g]);
analogWrite(bluepin, gammatable[(int)b]);
sendPlotData("1-clr", (clear/clear));
sendPlotData("2-red", (red/clear));
sendPlotData("3-green", (green/clear));
sendPlotData("4-blue", (blue/clear));
}
//*********format data for MegunoLink *****************************************************
void sendPlotData(String seriesName, float data) {
Serial.print("{");
Serial.print(seriesName);
Serial.print(",T,");
Serial.print(data);
Serial.println("}");
// Serial1.print("{");
// Serial1.print(seriesName);
// Serial1.print(",T,");
// Serial1.print(data);
// Serial1.println("}");
}