100% work means, the servo didnt always do the job to eliminate bottle with different color
and here's the full code
// constants won't change
// TCS230 or TCS3200 pins wiring to Arduino
#include <Servo.h>
Servo ServoMesin;
int S0 = 38;
int S1 = 39;
int S2 = 40;
int S3 = 41;
int sensorOut = 30;
int redFrequency = 0;
int greenFrequency = 0;
int blueFrequency = 0;
int redColor = 0;
int greenColor = 0;
int blueColor = 0;
void setup() {
// initialize digital pin A5 as an output.
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
pinMode(sensorOut, INPUT);
digitalWrite(S0, HIGH);
digitalWrite(S1, HIGH);
ServoMesin.attach(3);
Serial.begin(9600);
}
// the loop function runs over and over again forever
void loop() {
// Setting RED (R) filtered photodiodes to be read
digitalWrite(S2, LOW);
digitalWrite(S3, LOW);
// Reading the output frequency
redFrequency = pulseIn(sensorOut, LOW);
// Remaping the value of the RED (R) frequency from 0 to 255
// You must replace with your own values. Here's an example:
// redColor = map(redFrequency, 70, 120, 255,0);
redColor = map(redFrequency, 39, 103, 0, 255);
redColor = constrain(redFrequency, 0, 255);
// Printing the RED (R) value
Serial.print("R = ");
Serial.print(redColor);
// delay(500);
// Setting GREEN (G) filtered photodiodes to be read
digitalWrite(S2, HIGH);
digitalWrite(S3, HIGH);
// Reading the output frequency
greenFrequency = pulseIn(sensorOut, LOW);
// Remaping the value of the GREEN (G) frequency from 0 to 255
// You must replace with your own values. Here's an example:
// greenColor = map(greenFrequency, 100, 199, 255, 0);
greenColor = map(greenFrequency, 61, 109, 0, 255);
greenColor = constrain(greenFrequency, 0, 255);
// Printing the GREEN (G) value
Serial.print(" G = ");
Serial.print(greenColor);
// delay(500);
// Setting BLUE (B) filtered photodiodes to be read
digitalWrite(S2, LOW);
digitalWrite(S3, HIGH);
// Reading the output frequency
blueFrequency = pulseIn(sensorOut, LOW);
// Remaping the value of the BLUE (B) frequency from 0 to 255
// You must replace with your own values. Here's an example:
// blueColor = map(blueFrequency, 38, 84, 255, 0);
blueColor = map(blueFrequency, 38, 125, 0, 255);
blueColor = constrain(blueFrequency, 0, 255);
// Printing the BLUE (B) value
Serial.print(" B = ");
Serial.print(blueColor);
// delay(500);
// Checks the current detected color and prints
// a message in the serial monitor
if (redColor > 7 && redColor < 30 && greenColor > 17 && greenColor < 46 && blueColor > 14 && blueColor < 39)
{
Serial.println(" - RED detected!");
}
else if (redColor > 22 && redColor < 49 && greenColor > 14 && greenColor < 44 && blueColor > 16 && blueColor < 41)
{
Serial.println(" - GREEN detected!");
ServoMesin.write(5);
delay(500);
ServoMesin.write(60);
delay(500);
}
else if (redColor > 23 && redColor < 55 && greenColor > 11 && greenColor < 46 && blueColor > 7 && blueColor < 33)
{
Serial.println(" - BLUE detected!");
}
else if (redColor > 6 && redColor < 12 && greenColor > 7 && greenColor < 11 && blueColor > 13 && blueColor < 19)
{
Serial.println(" - YELLOW detected!");
ServoMesin.write(5);
delay(500);
ServoMesin.write(60);
delay(500);
}
else
{
Serial.println(" - No color!");
ServoMesin.write(5);
delay(100);
}
}