Setting time to active servo after TCS3200 sensing color

I'm new with arduino, trying to make machine to eliminate bottle with different colour.
I've tried to use delay as a timing for servo to work and it doesnt 100% do the job.

Here's my code :

void getColor(){  
  readRGB();

if(red>6  && red<21  && grn>15 && grn<40 && blu>13 && blu<34)
  {
  color = "RED";
  }
else if(red>19 && red<36  && grn>12 && grn<26 && blu>14 && blu<29) 
  {
    color = "GREEN";
    myservo.write(5);
    delay(500);
    myservo.write(60);
    delay(500);
  }
else if(red>25 && red<45  && grn>12 && grn<28 && blu>7  && blu<17)
  {
    color = "BLUE";
  }
else if(red>5  && red<23  && grn>6  && grn<27 && blu>10  && blu<39) 
  {
    color = "YELLOW";
    myservo.write(5);
    delay(500);
    //myservo.write(60);
    delay(500);
  }
else 
  {
    color = "No Color";
    delay(1000);
]    myservo.write(5);
    delay(500);
  }
}


void getMotor()
{

 analogWrite(enB, motorspeed); // mengatur kecepatan motor
 digitalWrite(in3, LOW);
 digitalWrite(in4, HIGH);
 analogWrite(enB,160);
 delay(3000);
}

It is going to be hard to help you since we don't know what " doesnt 100% do the job" means and we don't know what the job actually is.

Please describe, in detail, what the code is supposed to do and how that differs from what the code is actually doing.

A schematic would be useful. Please post a schematic. Written descriptions are always more ambiguous than a drawing. Hand drawn, photographed and posted is fine. Include all pin names/numbers, components, their part numbers and/or values and power supplies.

Well done posting the code in a code blocks. Most new members have to be reminded to read the guidelines. But we do prefer that you post the complete code because snippets often leave out important information like data types, libraries used, object declarations, etc.

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);
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.