Here is the code for the colour detector build. If code does not work you may need to indent the code by using the tab spacing button before lines of code. To add code to own Arduino projects just copy the text.
#include <LiquidCrystal.h> const int buttonPin=13; //button values int buttonState = 0; const int rs = 7, en = 6, d4 = 5, d5 = 4, d6 = 3, d7 = 2; //pins for LCD LiquidCrystal lcd(7, 6, 5, 4, 3, 2); const int pingPin = A1; // Trigger Pin of Ultrasonic Sensor const int echoPin = A0; // Echo Pin of Ultrasonic Sensor #define s0 8 //colour sensor pins #define s1 9 #define s2 10 #define s3 11 #define out 12 int Reddata = 0; //values for RGB before starting int Bluedata = 0; int Greendata = 0; int Bright = 100; //brightness value float Yvalue = 0; // Y value before check float NewY = 0; // checking Y values float NewY2 = 0; //===================================================================================================\\ void setup() { pinMode(buttonPin, INPUT); //button pin mode pinMode(s0,OUTPUT); //colour sensor pin modes pinMode(s1,OUTPUT); pinMode(s2,OUTPUT); pinMode(s3,OUTPUT); pinMode(out,INPUT); Serial.begin(9600); digitalWrite(s0,HIGH); digitalWrite(s1,HIGH); lcd.begin(16, 2); lcd.print("Detected Colour"); //permanent text on lcd screen } //===================================================================================================\\ void loop() { buttonState = digitalRead(buttonPin); if (buttonState == HIGH) { digitalWrite(s2, LOW); //to get colour values digitalWrite(s3, LOW); Reddata = pulseIn(out, LOW); digitalWrite(s2, LOW); digitalWrite(s3, HIGH); Bluedata = pulseIn(out, LOW); digitalWrite(s2, HIGH); digitalWrite(s3, HIGH); Greendata = pulseIn(out, LOW); long duration, mm, cm; pinMode(pingPin, OUTPUT); digitalWrite(pingPin, LOW); delayMicroseconds(2); digitalWrite(pingPin, HIGH); delayMicroseconds(10); digitalWrite(pingPin, LOW); pinMode(echoPin, INPUT); duration = pulseIn(echoPin, HIGH); cm = microsecondsToCentimeters(duration); mm = 10*cm; Yvalue = 3.015618902 + 0.005266335*(Bright) - 0.068545624*(mm) + 0.21677064*(Reddata) + 0.08469257*(Bluedata) - 0.097166029*(Greendata); Serial.print("R: "); Serial.print(Reddata); Serial.print(" G: "); Serial.print(Greendata); Serial.print(" B: "); Serial.print(Bluedata); Serial.print(" Bright: 100"); Serial.print(" Distance: "); Serial.print(mm); Serial.print("mm"); Serial.print(" Initial Y: "); Serial.print(Yvalue); Serial.println(); if (Yvalue < 2) //start of checking { lcd.setCursor(0, 1); lcd.print("white "); } if (Yvalue > 2 && Yvalue < 3) { NewY = 3.015618902 + 0.005266335*(Bright) - 0.068545624*(mm) + 0.21677064*(Reddata) + 0.08469257*(Bluedata); if(NewY < 4) { lcd.setCursor(0, 1); lcd.print("white "); } if(NewY > 4) { NewY2 = 3.015618902 + 0.005266335*(Bright) - 0.068545624*(mm) + 0.08469257*(Bluedata); if(NewY2 > 0 && NewY2 < 2) { lcd.setCursor(0, 1); lcd.print("yellow "); } if(NewY2 > 2 && NewY2 < 2.7) { lcd.setCursor(0, 1); lcd.print("orange "); } if(NewY2 > 2.7) { lcd.setCursor(0, 1); lcd.print("red "); } } } if (Yvalue > 3 && Yvalue < 4) { NewY = 3.015618902 + 0.005266335*(Bright) - 0.068545624*(mm) + 0.21677064*(Reddata) + 0.08469257*(Bluedata); if(NewY < 4) { lcd.setCursor(0, 1); lcd.print("white "); } if(NewY > 4 && NewY < 4.5) { lcd.setCursor(0, 1); lcd.print("yellow "); } if(NewY > 4.5 && NewY < 5) { lcd.setCursor(0, 1); lcd.print("orange "); } if(NewY > 5) { lcd.setCursor(0, 1); lcd.print("red "); } } if (Yvalue > 4 && Yvalue < 5) { NewY = 3.015618902 + 0.005266335*(Bright) - 0.068545624*(mm) + 0.21677064*(Reddata) + 0.08469257*(Bluedata); if (NewY < 6) { lcd.setCursor(0, 1); lcd.print("green "); } if (NewY > 6) { lcd.setCursor(0, 1); lcd.print("purple "); } } if (Yvalue > 5 && Yvalue < 6) { NewY = 3.015618902 + 0.005266335*(Bright) - 0.068545624*(mm) + 0.21677064*(Reddata) + 0.08469257*(Bluedata); if (NewY > 9) { lcd.setCursor(0, 1); lcd.print("purple "); } if (NewY < 9) { NewY2 = 3.015618902 + 0.005266335*(Bright) - 0.068545624*(mm) + 0.21677064*(Reddata); if (NewY2 < 5.8) { lcd.setCursor(0, 1); lcd.print("green "); } if (NewY2 > 5.8) { lcd.setCursor(0, 1); lcd.print("blue "); } } } if (Yvalue > 6 && Yvalue < 7) { NewY = 3.015618902 + 0.005266335*(Bright) - 0.068545624*(mm) + 0.21677064*(Reddata) + 0.08469257*(Bluedata); if (NewY > 10) { lcd.setCursor(0, 1); lcd.print("purple "); } if (NewY < 10) { NewY2 = 3.015618902 + 0.005266335*(Bright) - 0.068545624*(mm) + 0.21677064*(Reddata); if (NewY2 < 6.5) { lcd.setCursor(0, 1); lcd.print("green "); } if (NewY2 > 6.5) { lcd.setCursor(0, 1); lcd.print("blue "); } } } if (Yvalue > 7 && Yvalue < 8) { NewY = 3.015618902 + 0.005266335*(Bright) - 0.068545624*(mm) + 0.21677064*(Reddata) + 0.08469257*(Bluedata); if (NewY < 11) { lcd.setCursor(0, 1); lcd.print("purple "); } if (NewY > 11) { lcd.setCursor(0, 1); lcd.print("black "); } } if (Yvalue > 8) { lcd.setCursor(0, 1); lcd.print("black "); } delay(2000); } } long microsecondsToCentimeters(long microseconds) { return microseconds / 29 / 2; }