color sorter using arduino




CIRCUIT



PROGRAM
#include <Servo.h> 
Servo myservo1; 

#define motor A3
#define hall A5
const int s0 = 8;
const int s1 = 10;
const int s2 = 11;
const int s3 = 12;
const int out = 9;
int red = 0; 
int green = 0;
int blue = 0;
int position1=1;
void setup()
{  
  Serial.begin(9600);
  pinMode(motor, OUTPUT);
  pinMode(hall,INPUT_PULLUP);  
  pinMode(s0, OUTPUT);
  pinMode(s1, OUTPUT);
  pinMode(s2, OUTPUT);
  pinMode(s3, OUTPUT);
  pinMode(out, INPUT);
  digitalWrite(s0, HIGH);
  digitalWrite(s1, LOW);
   myservo1.attach(A0);
goto_green_box();//delay(500);
//goto_box_1();goto_box_2();goto_box_3();goto_box_4();goto_box_5();
}

void loop()
{
do
{
 motor_on();//    
}while(digitalRead(hall));
delay(100);
motor_off();Serial.println("ok");
delay(1500);
  color();
  if (green < red && green < blue)
  {
    Serial.println(" Green");    goto_green_box();
   
  }
  else if (blue < red && blue < green && blue < 400)
  {
Serial.println("blueeee");
  if(red>250)
   {
    Serial.println("blue");goto_blue_box();
   }
   else if(red<200 && blue<200 )
   {
    Serial.println("indigo");goto_indigo_box();
   }
  }

  else if (red < blue && red < green && red <250)
  {
    Serial.println("rdddd");
   if (red < 110 && green >140   && blue>150)
    {
    Serial.println("orange");goto_orange_box();
    }
    else if (red < 130 && green <130 && blue <130)
    {
    Serial.println("yellow");goto_yellow_box();
    }
    else if(red<200 && blue<200 && green>200 )
   {
    Serial.println("indigo");goto_indigo_box();
   }
  }

motor_on();
delay(100);
}
void servo_rotationtime(void)
{
delay(1000);  
}
void goto_green_box(void)//green
{
myservo1.write(5);   servo_rotationtime();
}
void goto_yellow_box(void)
{
myservo1.write(45);   servo_rotationtime();
}
void goto_orange_box(void)
{
myservo1.write(85);   servo_rotationtime();
}
void goto_blue_box(void)
{
myservo1.write(125);   servo_rotationtime();
}
void goto_indigo_box(void)
{
myservo1.write(170);   servo_rotationtime();
}

void motor_on(void)
{
  digitalWrite(motor, HIGH);  
}
void motor_off(void)
{
  digitalWrite(motor, LOW);  
}
void check_position(void)
{
position1=digitalRead(hall);  
}
void color()
{
  digitalWrite(s2, LOW);
  digitalWrite(s3, LOW);    delay(1);
  //count OUT, pRed, RED
  red = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
  digitalWrite(s3, HIGH);  delay(1);
  //count OUT, pBLUE, BLUE
  blue = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
  digitalWrite(s2, HIGH);  delay(1);
  //count OUT, pGreen, GREEN
  green = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
   Serial.print("R=");  Serial.print(red);  Serial.print("--G=");  Serial.print(green);  Serial.print("--B=");  Serial.print(blue);
 Serial.println("");
}