Go Down

Topic: Color Sensor detecting colors but not giving good rgb values (Read 399 times) previous topic - next topic

marcopy

Hello Everybody! I made a color sensor and it detects colors fine. But on the serial monitor it does not send the good rgb values because most of the time, it sends 255,255,255 for every color!Here is the code:
Code: [Select]
int redPin = 11;
int greenPin = 10;
int bluePin = 6;

int S2 = 7;
int S3 = 8;
int outPin = 4;

int rColorStrength;
int gColorStrength;
int bColorStrength;

unsigned int pulseWidth;

void setup() {
  // put your setup code here, to run once:

  Serial.begin(9600);

  pinMode(redPin,OUTPUT);
  pinMode(greenPin,OUTPUT);
  pinMode(bluePin,OUTPUT);

  pinMode(S2,OUTPUT);
  pinMode(S3,OUTPUT);
  pinMode(outPin,INPUT);

}

void loop() {
  // put your main code here, to run repeatedly:
  //red
  digitalWrite(S2,LOW);
  digitalWrite(S3,LOW);
 
  pulseWidth = pulseIn(outPin,LOW);

  rColorStrength = pulseWidth/400. -1;
  rColorStrength = (255-rColorStrength);
 
  //green
  digitalWrite(S2,HIGH);
  digitalWrite(S3,HIGH);
 
  pulseWidth = pulseIn(outPin,LOW);

  gColorStrength = pulseWidth/400. -1;
  gColorStrength = (255-gColorStrength);
 
  //blue
  digitalWrite(S2,LOW);
  digitalWrite(S3,HIGH);
 
  pulseWidth = pulseIn(outPin,LOW);

  bColorStrength = pulseWidth/400. -1;
  bColorStrength = (255-bColorStrength);

  Serial.print(rColorStrength);
  Serial.print(" , ");
  Serial.print(gColorStrength);
  Serial.print(" , ");
  Serial.print(bColorStrength);
  Serial.println("");
  //red
  if(rColorStrength>gColorStrength && gColorStrength>bColorStrength){
    rColorStrength = 255;
    gColorStrength = gColorStrength/2.;
    bColorStrength = 0;
  }
  if(rColorStrength>bColorStrength && bColorStrength>gColorStrength){
    rColorStrength = 255;
    bColorStrength = bColorStrength/2.;
    gColorStrength = 0;
  }
  //green
  if(gColorStrength>rColorStrength && rColorStrength>bColorStrength){
    gColorStrength = 255;
    rColorStrength = rColorStrength/2.;
    bColorStrength = 0;
  }
  if(gColorStrength>bColorStrength && bColorStrength>rColorStrength){
    gColorStrength = 255;
    bColorStrength = bColorStrength/2.;
    rColorStrength = 0;
  }
  //blue
  if(bColorStrength>rColorStrength && rColorStrength>gColorStrength){
    bColorStrength = 255;
    rColorStrength = rColorStrength/2.;
    gColorStrength = 0;
  }
  if(bColorStrength>gColorStrength && gColorStrength>rColorStrength){
    bColorStrength = 255;
    gColorStrength = gColorStrength/2.;
    rColorStrength = 0;
  }
 
  analogWrite(redPin, rColorStrength);
  analogWrite(greenPin, gColorStrength);
  analogWrite(bluePin, bColorStrength);
 
  delay(250);
   

}


Thanks

Go Up