I'm doing a project and the only color sensors the school has are the TCS3200-DB. They have two LEDs. I have found alot of code online for those with the 4 LEDs but the ouput is not accurate, they're all over 255.
Can some please direct me as to where I can find a tutorial on the two LED color sensors or provide me with a code sample. its 6am in the morning and I started researching since 10pm. Please help someone.
Found a datasheet but the code provided is in basic.
There is a video with a guy using it, he also provided a link to code that also doesn't work, after adjustments too remove the text to spech.
I will also provide what I have currently, but nothing is outputed in the serial monitor.
Also his code uses the maximum function from the Average.h file but for some reason, an error occurs about not finding the Average.h library, which I downloaded and placed in the Arduino library.
*/
//**************************************** LIBRARIES ***************************************
#include <Average.h> // See http://playground.arduino.cc/Main/Average
//************************************ GLOBAL VARIABLES ************************************
// s0 - purple - 40
// s1 - gray - 37
// s2 - green - 41
// s3 - blue - 39
// led - brown - 36
// out - white - 38
int S0=40,S1=37,S2=41,S3=39, LED=36;
int OUT=38;
void setup() {
TCS3200_Setup();
delay(100);
}
void loop() {
Serial.println(ReadColor());
}
//********************************* FUNCTIONS COLOR SENSOR *********************************
void TCS3200_Setup() {
pinMode(S0,OUTPUT);
pinMode(S1,OUTPUT);
pinMode(S2,OUTPUT);
pinMode(S3,OUTPUT);
pinMode(LED,OUTPUT);
pinMode(OUT,INPUT);
}
void TCS3200_On() {
digitalWrite(LED,HIGH); // Switch LED on
digitalWrite(S0,HIGH); //Output frequency scaling (100%)
digitalWrite(S1,HIGH);
delay(5);
}
void TCS3200_Off() {
digitalWrite(LED,LOW); // Switch LED off
digitalWrite(S0,LOW); //Power off sensor
digitalWrite(S1,LOW);
}
void NoFilter() { //Select no filter
digitalWrite(S2,HIGH);
digitalWrite(S3,LOW);
delay(5);
}
void RedFilter() { //Select red filter
digitalWrite(S2,LOW);
digitalWrite(S3,LOW);
delay(5);
}
void GreenFilter() { //Select green filter
digitalWrite(S2,HIGH);
digitalWrite(S3,HIGH);
delay(5);
}
void BlueFilter() { //Select blue filter
digitalWrite(S2,LOW);
digitalWrite(S3,HIGH);
delay(5);
}
int ReadColor() { //0=white, 1=orange, 2=yellow, 3=red, 4=green, 5=blue, 6=object out of range
float FrequencyClear,FrequencyRed,FrequencyGreen,FrequencyBlue;
int PercentageRed,PercentageGreen,PercentageBlue;
TCS3200_On();
NoFilter();
FrequencyClear=500.0/pulseIn(OUT,LOW,10000); // Frequency in kHz
RedFilter();
FrequencyRed=500.0/pulseIn(OUT,LOW,10000); // Frequency in kHz
GreenFilter();
FrequencyGreen=500.0/pulseIn(OUT,LOW,10000); // Frequency in kHz
BlueFilter();
FrequencyBlue=500.0/pulseIn(OUT,LOW,10000); // Frequency in kHz
TCS3200_Off();
//Output frequency blue, green, red percentage represents the ratio of the
//respective color to the Clear channel absolute value:
PercentageRed=int((FrequencyRed/FrequencyClear)*100.0);
PercentageGreen=int((FrequencyGreen/FrequencyClear)*100.0);
PercentageBlue=int((FrequencyBlue/FrequencyClear)*100.0);
//Learned blue, green, red percentage values of different colors
int SavedColorRed[] = {28,55,42,50,19,13};
int SavedColorGreen[] = {30,25,36,22,45,26};
int SavedColorBlue[] = {45,20,20,30,36,58};
int GetColor[] = {1,2,3,4,5,6};
int ColorArray[3];
int i_color;
int ClosestColor;
int MaxDiff;
int MinDiff=300;
if(FrequencyClear<1.5)ClosestColor=6; // Object out of range
else {
for (i_color=0; i_color<6; i_color++) { //Find closest color
ColorArray[0]=abs(SavedColorRed[i_color]-PercentageRed);
ColorArray[1]=abs(SavedColorGreen[i_color]-PercentageGreen);
ColorArray[2]=abs(SavedColorBlue[i_color]-PercentageBlue);
// MaxDiff=maximum(ColorArray,3);
if (MaxDiff<MinDiff) {
MinDiff=MaxDiff;
ClosestColor=i_color;
}
}
}
return GetColor[ClosestColor];
}
Please help Someone, even if is by making the code simpler.

