/* Robot Chameleon based on TCS3200 COLOR SENSOR and RGB LED
Author: Markus Bindhammer
Date: 2-6-2014
*/
//**************************************** LIBRARIES ***************************************
#include // See
playground.arduino.cc/Main/Average
//************************************ GLOBAL VARIABLES ************************************
int OUT=2,S2=3,S3=4,LED=5,S0=6,S1=7; // define TCS3200 pins
int redPin=11,greenPin=10,bluePin=9; // define RGB LED pins
void setup() {
RGB_LED_Setup();
TCS3200_Setup();
}
void loop() {
GetColor();
delay(200);
}
//************************************ FUNCTIONS RGB LED ***********************************
void RGB_LED_Setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void setColor(int red, int green, int blue) {
analogWrite(redPin, 255-red);
analogWrite(greenPin, 255-green);
analogWrite(bluePin, 255-blue);
}
//********************************* 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);
}
void GetColor() { //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 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=max(ColorArray,3);
if (MaxDiff