TCS230 Color Sensor Calibration

I was trying to use few lines of code without any libraries, that were made by other people. This code works perfectly fine for me. However, I want to put a calibration code in setup, so that it can calibrate by itself every time I start the robot. Can you please add the calibration code at the start of the code and share it back for me? Thank you :smiley:

Ps. few extra codes in color_left comparing to color_right is some codes that I have failed to make them calibrate.

#include <AFMotor.h>
#include <LiquidCrystal_I2C.h> //Load Liquid Crystal Library
LiquidCrystal_I2C lcd(0x27, 16, 2);

AF_DCMotor motor1(1, MOTOR12_64KHZ);
AF_DCMotor motor2(2, MOTOR12_64KHZ);

int dir;
int forward = 0;
int left = 1;
int right = 2;
int backward = 3;
int wait = 4;

const int ls0 = 22;
const int ls1 = 24;
const int ls2 = 26;
const int ls3 = 28;
const int lout = 30;

const int rs0 = 44;
const int rs1 = 46;
const int rs2 = 48;
const int rs3 = 50;
const int rout = 52;

int red_left = 0;
int green_left = 0;
int blue_left = 0;

int red_right = 0;
int green_right = 0;
int blue_right = 0;

float SF_left[3];
float SF_right[3];
    
void setup()   
{  
  Serial.begin(9600); 
  pinMode(ls0, OUTPUT);
  pinMode(ls1, OUTPUT);
  pinMode(ls2, OUTPUT);
  pinMode(ls3, OUTPUT);
  pinMode(lout, INPUT);
  digitalWrite(ls0, HIGH);
  digitalWrite(ls1, HIGH);

  pinMode(rs0, OUTPUT);
  pinMode(rs1, OUTPUT);
  pinMode(rs2, OUTPUT);
  pinMode(rs3, OUTPUT);
  pinMode(rout, INPUT);
  digitalWrite(rs0, HIGH);
  digitalWrite(rs1, HIGH);

  // turn on motor
  motor1.setSpeed(255);
  motor2.setSpeed(255);
  
  motor1.run(RELEASE);
  motor2.run(RELEASE);

  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0); //Set LCD cursor to upper left corner, column 0, row 0
}  
    
void loop() 
{  
  color_left();
  color_right();

       if (red_right < green_right < 70 && blue_right < green_right < 71 && red_right < 60 && blue_right < 62) 
  {
    lcd.setCursor(0,0);  
    lcd.print("Black Right");
  }  

   else if (red_right > 5 && red_right < 13 && green_right > 5 && green_right < 13 && blue_right > 5 && blue_right < 13) {
    lcd.setCursor(0,0);
    lcd.print("White Right");
  }
  
    else if (green_right < red_right < 41 && green_right < blue_right < 31)  
  {  
   lcd.setCursor(0,0);
   lcd.print("Green right");  
  }  

  
   if (red_left > 5 && red_left < 13 && green_left > 5 && green_left < 13 && blue_left > 5 && blue_left < 13) {
    lcd.setCursor(0,1);
    lcd.print("White Left");
  }



     if (red_left < green_left < 70 && blue_left < green_left < 71 && red_left < 60 && blue_left < 61) {
    lcd.setCursor(0,1);
    lcd.print("Black Left");
  }

    else if (green_left < red_left < 41 && green_left < blue_left < 31)  
  {  
   lcd.setCursor(0,1);
   lcd.print("Green Left");  
  }  
  



  Serial.print("Left R:");
  Serial.print(red_left, DEC);
  Serial.print(" G:");
  Serial.print(green_left, DEC);
  Serial.print(" B:");
  Serial.println(blue_left, DEC);
  Serial.print("Right R:");
  Serial.print(red_right, DEC);
  Serial.print(" G:");
  Serial.print(green_right, DEC);
  Serial.print(" B:");
  Serial.println(blue_right, DEC);

 }  
    
void color_left()
{
  digitalWrite(ls2, LOW);
  digitalWrite(ls3, LOW);
  //count OUT, pRed, RED
  red_left = pulseIn(lout, digitalRead(lout) == HIGH ? LOW : HIGH);
  digitalWrite(ls3, HIGH);
  //count OUT, pBLUE, BLUE
  blue_left = pulseIn(lout, digitalRead(lout) == HIGH ? LOW : HIGH);
  digitalWrite(ls2, HIGH);
  //count OUT, pGreen, GREEN
  green_left = pulseIn(lout, digitalRead(lout) == HIGH ? LOW : HIGH);  

  SF_left[0] = (255 / red_left);
  SF_left[1] = (255 / green_left);
  SF_left[2] = (255 / blue_left);

  red_left = (255 - red_left);
  green_left = (255 - green_left);
  blue_left = (255 - blue_left);
  
 // red_left = (red_left * SF_left[0]);
  //green_left = (green_left * SF_left[1]);
  //blue_left = (blue_left * SF_left[2]);
}

void color_right()
{
  digitalWrite(rs2, LOW);
  digitalWrite(rs3, LOW);
  //count OUT, pRed, RED
  red_right = pulseIn(rout, digitalRead(rout) == HIGH ? LOW : HIGH);
  digitalWrite(rs3, HIGH);
  //count OUT, pBLUE, BLUE
  blue_right = pulseIn(rout, digitalRead(rout) == HIGH ? LOW : HIGH);
  digitalWrite(rs2, HIGH);
  //count OUT, pGreen, GREEN
  green_right = pulseIn(rout, digitalRead(rout) == HIGH ? LOW : HIGH);
}
 if (red_right < green_right < 70 && blue_right < green_right < 71 && red_right < 60 && blue_right < 62)

Have you tested that line?
Does it do what you want it to do?

AWOL:

 if (red_right < green_right < 70 && blue_right < green_right < 71 && red_right < 60 && blue_right < 62)

Have you tested that line?
Does it do what you want it to do?

That exact line is working fine. However, with other two colors, the robot recognizes with Black color. ie) Black, Green, Black, Green... Also, another clue is that only one that is working is the one that is the closest to the start of the program. I also want to invert the value of RGB values, like White to be 255, 255, 255, not 0, 0, 0 (not even at 0 right now). If it isn't possible for now, I just need the code with calibration system at the start.
Thanks :slight_smile: