Hello dear all,
I am a new in this forum and here is my first topic.
I am planning to make color sorter with Arduino. I am aiming to detect 4 colors of candies and sort them with using servo. I have couple questions about it
- I bought GY-31 Color sensor which is similar with TSC3200 but it has 10 pins. What is the purpose of adding one more VCC and GND pin?
2)I found sample code for it and edited some parts to understand process. It actually works fine but in this code we don’t use LED pin in whole code. But what’s the purpose of LED pin on color sensor?
(Here is the code that I edit)
#include <TimerOne.h>
#define S0 6
#define S1 5
#define S2 4
#define S3 3
#define OUT 2
int redPin = 9;
int greenPin = 10;
int bluePin =11;
//#define COMMON_ANODE
int g_count = 0; // count the frequecy
int g_array[3]; // store the RGB value
int g_flag = 0; // filter of RGB queue
float g_SF[3]; // save the RGB Scale factor
// Init TSC230 and setting Frequency.
void TSC_Init()
{
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
pinMode(OUT, INPUT);
digitalWrite(S0, LOW); // OUTPUT FREQUENCY SCALING 2%
digitalWrite(S1, HIGH);
}
// Select the filter color
void TSC_FilterColor(int Level01, int Level02)
{
if(Level01 != 0)
Level01 = HIGH;
if(Level02 != 0)
Level02 = HIGH;
digitalWrite(S2, Level01);
digitalWrite(S3, Level02);
}
void TSC_Count()
{
g_count ++ ;
}
void TSC_Callback()
{
switch(g_flag)
{
case 0:
Serial.println("->WB Start");
TSC_WB(LOW, LOW); //Filter without Red
break;
case 1:
Serial.print("->Frequency R=");
Serial.println(g_count);
g_array[0] = g_count;
TSC_WB(HIGH, HIGH); //Filter without Green
break;
case 2:
Serial.print("->Frequency G=");
Serial.println(g_count);
g_array[1] = g_count;
TSC_WB(LOW, HIGH); //Filter without Blue
break;
case 3:
Serial.print("->Frequency B=");
Serial.println(g_count);
Serial.println("->WB End");
g_array[2] = g_count;
// ledYak();
TSC_WB(HIGH, LOW); //Clear(no filter)
break;
default:
g_count = 0;
break;
}
}
void TSC_WB(int Level0, int Level1) //White Balance
{
g_count = 0;
g_flag ++;
TSC_FilterColor(Level0, Level1);
Timer1.setPeriod(1000000); // set 1s period
}
void setup()
{
TSC_Init();
Serial.begin(9600);
Timer1.initialize(); // defaulte is 1s
Timer1.attachInterrupt(TSC_Callback);
attachInterrupt(0, TSC_Count, RISING);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
delay(4000);
for(int i=0; i<3; i++)
Serial.println(g_array[i]);
g_SF[0] = 255.0/ g_array[0]; //R Scale factor
g_SF[1] = 255.0/ g_array[1] ; //G Scale factor
g_SF[2] = 255.0/ g_array[2] ; //B Scale factor
Serial.println(g_SF[0]);
Serial.println(g_SF[1]);
Serial.println(g_SF[2]);
}
void ledYak(){
/*analogWrite(redPin,255-g_array[0] * g_SF[0]);
analogWrite(greenPin,255-g_array[1] * g_SF[1]);
analogWrite(bluePin,255-g_array[2] * g_SF[2]);
*/
{
if((g_array[0]>g_array[1])&&(g_array[0]>g_array[2]))
{
Serial.println("Kirmizi");
analogWrite(redPin,0);
analogWrite(greenPin,255);
analogWrite(bluePin,255);
}
else if(g_array[1]>g_array[2])
{
Serial.println("Yesil");
analogWrite(redPin,255);
analogWrite(greenPin,0);
analogWrite(bluePin,255);
}
else
{
Serial.println("Blue");
analogWrite(redPin,255);
analogWrite(greenPin,255);
analogWrite(bluePin,0);
}
}
}
void loop()
{
g_flag = 0;
for(int i=0; i<3; i++)
Serial.println(int(g_array[i] * g_SF[i]));
ledYak();
delay(4000);
}
- For the next part of project, I guess I will need 2 servo motors but what kind of servo motors should I use? Do you have specific model advice? For example mini or medium or??
Thank you for your helps.(Sorry about my english grammer mistakes)