Why doesn't this detect color correctly? [Working now :)]

is was giving good results, until i changed something, it was 12am and i was really sleepy !
well first, i was looking somewhere and i tough, each colored object reflects its own color light better ! i went to try this and saw yes ! but knew for sure a lot of people tough of this already xD

i made this code
:

int CN[3]; // holds each color result

int blue = 11, green = 12, red = 13; // just to dont get messy !

void swap(int,int);

void setup(){
Serial.begin(9600);

pinMode(13,OUTPUT);
pinMode(12,OUTPUT);
pinMode(11,OUTPUT);
}

void loop(){
digitalWrite(green,HIGH);
delay(15);
CN[0] = analogRead(0);
digitalWrite(green,LOW);

digitalWrite(blue,HIGH);
delay(15); // delay for RBG LED to get its full brightness
CN[1] = analogRead(0); // reads photoresistor while the blue led is on (must see more light if object is blue);
digitalWrite(blue,LOW); // turn off for next color

digitalWrite(red,HIGH);
delay(15);
CN[2] = analogRead(0);
digitalWrite(red,LOW);

/if(CN[0] > CN[1] && CN[0] > CN[2]){
Serial.println("Green!");
} else
if(CN[1] > CN[0] && CN[1] > CN[2]){
Serial.println("Blue!");
} else {
if(CN[2] > CN[0] && CN[2] > CN[1]){
Serial.println("Red!");
}
}
/
Serial.print("Green: ");
Serial.println(CN[0]);
Serial.print("Blue: ");
Serial.println(CN[1]);
Serial.print("Red: ");
Serial.println(CN[2]);
Serial.println(" ------------------- END");

delay(2000); // time to read and think !
}

/void swap(int a, int b)
{
temp = a;
a = b;
b = temp;
}
/

it always give me Green ! but when i put my finger or a red colored object it shows red ! is shows green when theres nothing on top of it!

it shows green still even when a blue object is on top of it !

here are results on console:

nothing on front :

Green: 235
Blue: 194
Red: 199
------------------- END

red object on front:

Green: 125
Blue: 86
Red: 220

blue object on front:

Green: 304
Blue: 269
Red: 184
---------

green object:

Green: 419
Blue: 196
Red: 280
------------------- END

I just puted a phororesistor and a rbg led next to it !

Hi, first post your code between code tags # .
What are you using for sensors and can you post a CAD or picture of a hand drawn circuit diagram.
Also a picture of your project, to help us.
You need to check the colour response of your LDR they are not flat, also colour level output of tri-colour LED is also not necessarity flat, you need the specs of both.

Tom..... :slight_smile:

Photoresistor - part number? datasheet?

Is it a CdS sensor? CdS has a spectral response that's nowhere
near flat across the visible spectrum, it peaks in the yellow/orange/red,
very poor at the blue end.

BTW the human eye is no match for a colorimeter when determining if
a "blue" object is really blue or blue-green, since the eye's receptors
have a lot of overlap. And LEDs vary a lot from different manufacturers.

Without the spectral response graphs for your sensor, LEDs and the objects
under test there's no reason to suppose the values you get aren't entirely
reasonable.

sorry for late replybut a little busy i will read them after lunch xD

MarkT:
Photoresistor - part number? datasheet?

Is it a CdS sensor? CdS has a spectral response that's nowhere
near flat across the visible spectrum, it peaks in the yellow/orange/red,
very poor at the blue end.

BTW the human eye is no match for a colorimeter when determining if
a "blue" object is really blue or blue-green, since the eye's receptors
have a lot of overlap. And LEDs vary a lot from different manufacturers.

Without the spectral response graphs for your sensor, LEDs and the objects
under test there's no reason to suppose the values you get aren't entirely
reasonable.

i don't have any details about my parts !

im actually destroyed it for making a robot which, i abandoned because my breadboard is so big for robot chassis + my motors wont run with 4 1.2v batteries and faced many problems which mainly is power, i can't find good source of power

but heres picture of remade :

so simple !

i also tried putting a little paper between led and photo resistor, it didn't make any difference , it was so thin xD

but guess main reason is that 3 -color led doesn't have same light on each !

YAY ! i made it work ! but i found out even a slight diffrence in photoresistor's place makes a huge diffrence !

this is calibrated code for my stuff, i will work on an auto calibrate if anyone interested !:

int CN[3]; // holds each color result
int avarage;
int blue = 11, green = 10, red = 9; // just to dont get messy !

void swap(int,int);

void setup(){
  Serial.begin(9600);
  
  pinMode(blue,OUTPUT);
  pinMode(green,OUTPUT);
  pinMode(red,OUTPUT);
}

void loop(){
  analogWrite(green,84);
  delay(15);
  
  avarage = 0;
  for(int i = 0;i<10;i++){
    avarage += analogRead(0);
    delay(15);
  }
  CN[0] = avarage/10;
  digitalWrite(green,LOW);
  
  //end
  
  analogWrite(blue,225);
  delay(15); // delay for RBG LED to get its full brightness 
  
  avarage = 0;
  for(int i = 0;i<10;i++){
    avarage += analogRead(0);
    delay(15);
  }
  CN[1] = avarage/10; // reads photoresistor while the blue led is on (must see more light if object is blue);
  digitalWrite(blue,LOW); // turn off for next color
  
  //end
  
  analogWrite(red,124);
  delay(15);
  
  avarage = 0;
  for(int i = 0;i<10;i++){
    avarage += analogRead(0);
    delay(15);
  }
  CN[2] = avarage/10;
  digitalWrite(red,LOW);
  
  /*if(CN[0] > CN[1] && CN[0] > CN[2]){
    Serial.println("Green!");
  } else
    if(CN[1] > CN[0] && CN[1] > CN[2]){
     Serial.println("Blue!");
    } else {
    if(CN[2] > CN[0] && CN[2] > CN[1]){
      Serial.println("Red!");
    }
  }*/
  Serial.print("Green: ");
  Serial.println(CN[0]);
  Serial.print("Blue: ");
  Serial.println(CN[1]);
  Serial.print("Red: ");
  Serial.println(CN[2]);
  Serial.println(" ------------------- END");
  
  
  delay(2000); // time to read and think ! 
}

for light color detecting i can use 3 photo resistors and color them on 3 main colors ! red blue green :slight_smile:

total code with auto calibration:

int CN[3]; // holds each color result
int avarage;
int blue = 11, green = 10, red = 9; // just to dont get messy !
int GV = 36 ,RV = 67 ,BV = 97;

int ARsure();

void setup(){
  Serial.begin(9600);
  
  pinMode(blue,OUTPUT);
  pinMode(green,OUTPUT);
  pinMode(red,OUTPUT);
  
   //Starts Blue Calibrating
  BV = 100;
  analogWrite(blue,BV);
  while(ARsure() != 100){
    if(avarage > 100){
      BV--;
    } else if (avarage < 100) {
      BV++;
    }
    analogWrite(blue,BV);
  }
  
  digitalWrite(blue,LOW);
  //Blue end
  GV = 100;
  analogWrite(green,GV);
  while(ARsure() != 100){
    if(avarage > 100){
      GV--;
    } else if (avarage < 100) {
      GV++;
    }
    analogWrite(green,GV);
  }
  
   digitalWrite(green,LOW);
  //Green end
  
  RV = 100;
  analogWrite(red,RV);
  while(ARsure() != 100){
    if(avarage > 100){
      RV--;
    } else if (avarage < 100) {
      RV++;
    }
    analogWrite(red,RV);
  }
  
   digitalWrite(red,LOW);
  //Red end
  
}

void loop(){
  analogWrite(green,GV);
  delay(15);
  
  avarage = 0;
  for(int i = 0;i<10;i++){
    avarage += analogRead(0);
    delay(15);
  }
  CN[0] = avarage/10;
  digitalWrite(green,LOW);
  
  //end
  
  analogWrite(blue,BV);
  delay(15); // delay for RBG LED to get its full brightness 
  
  avarage = 0;
  for(int i = 0;i<10;i++){
    avarage += analogRead(0);
    delay(15);
  }
  CN[1] = avarage/10; // reads photoresistor while the blue led is on (must see more light if object is blue);
  digitalWrite(blue,LOW); // turn off for next color
  
  //end
  
  analogWrite(red,RV);
  delay(15);
  
  avarage = 0;
  for(int i = 0;i<10;i++){
    avarage += analogRead(0);
    delay(15);
  }
  CN[2] = avarage/10;
  digitalWrite(red,LOW);
  
  if(CN[0] > CN[1] && CN[0] > CN[2]){
    Serial.println("Green!");
  } else
    if(CN[1] > CN[0] && CN[1] > CN[2]){
     Serial.println("Blue!");
    } else {
    if(CN[2] > CN[0] && CN[2] > CN[1]){
      Serial.println("Red!");
    }
  }
  Serial.print("Green: ");
  Serial.println(CN[0]);
  Serial.print("Blue: ");
  Serial.println(CN[1]);
  Serial.print("Red: ");
  Serial.println(CN[2]);
  Serial.println(" ------------------- END");
  
  
  delay(2000); // time to read and think ! 
}

int ARsure(){
  avarage = 0;
  for(int i=0;i<8;i++){
      delay(1);
      avarage += analogRead(0);
    }
    
    avarage = avarage / 8;
    //Serial.println(avarage);
    return avarage;
}