Need help joining colour sorting code and servos code. Help truly appreciated

Hi all.

My name is Jeff and i’m new here.
For my school project i need to build a Colour Sorting Robot which can separate the colour of balls. So for the moment, with the use of Arduino UNO and TCS 3200 colour sensor I had managed to programme the arduino in such a way that it can read and recognise the RGB value. of n object

with the code like this

#include <TimerOne.h>

#define S0     6
#define S1     5
#define S2     4
#define S3     3
#define OUT    2

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;
         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);  
 
  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 loop()
{
   g_flag = 0;
   for(int i=0; i<3; i++)
    Serial.println(int(g_array[i] * g_SF[i]));
   delay(4000);
 
}

But now the problem I face is after read the colour RGB Value how do we proceed to make the sorting part works like the one shown in this link?

and

Please help
If someone could help me on sorting part of programming my code above, It very appreciated… What is the new material needed beside the one as follow. All help is appreciated. Thanks in advance.

Kind regards,
Jeff

jeff277:

Hi Jeff Note that in boolean algebra '!=0' is the same as '=1' and also the same as 'HIGH', (0 = LOW), so

  if(Level01 != 0)
    Level01 = HIGH;

does absolutely nothing. As does

 if(Level02 != 0)
    Level02 = HIGH;

Comment all 4 lines out and you'll see it makes no difference to the program.

HI sir Henry Best, so how am I gonna makes it work?

jeff277:
HI sir Henry Best, so how am I gonna makes it work?

You say you’re using balls, so I presume you want your sorter to be like the one in the second link you gave. You’ll need one servo to turn your ‘sorter’ and another to release the balls, one at a time, into the ‘sorter’. Based on the colour reading you get from your current code, you will have to turn the ‘sorter’ servo from the mid position (90o) to either 180o or 0o. When it’s in the correct position, you actuate the ‘release’ servo to allow the ball to roll into the sorter and down into the correct tray. You’ll probably find the coding easier if you return the ‘sorter’ to the mid (90o) position between each ball.

Henry_Best:

jeff277:
HI sir Henry Best, so how am I gonna makes it work?

You say you’re using balls, so I presume you want your sorter to be like the one in the second link you gave. You’ll need one servo to turn your ‘sorter’ and another to release the balls, one at a time, into the ‘sorter’. Based on the colour reading you get from your current code, you will have to turn the ‘sorter’ servo from the mid position (90o) to either 180o or 0o. When it’s in the correct position, you actuate the ‘release’ servo to allow the ball to roll into the sorter and down into the correct tray. You’ll probably find the coding easier if you return the ‘sorter’ to the mid (90o) position between each ball.

Hi Sir, I will need to make my robot function automatically with a 5V battery. Can sir help me with the sorting code like the one in link 2?I mean the arduino code for it to work like the one describe by sir as I not really good in the programming part of arduino

How many different colour balls are you using?

Henry_Best:
How many different colour balls are you using?

HI morning Sir Henry, I am using 3 colours. The set up will be something like the one at video link 1 which have basket to collect red ball, blue ball and green ball…

jeff277:
so how am I gonna makes it work?

You will build the hardware and write the software yourself. If you want somebody else to do it for you you’re in the wrong place, although there is a Gigs & Collaborations section where you can ask people to do that. Note that most people would expect to be paid for working for you.

What you’re trying to do doesn’t sound especially difficult and there’s no reason why you couldn’t write it for yourself, if you’re willing to put the effort in. However, it’s quite ambitious as a first project and I suggest you should tackle it in small steps. As a starting point, get familiar with uploading the standard example sketches to your Arduino and how to see the serial output from them. Then learn how to connect and power a servo and how to write a sketch to control a single servo. It’s quite easy and there are lots of examples. Then you will need to learn how to connect your colour sensors and read sensor values into your sketch, and then how to identify the colour from the sensor values. Hopefully you already know how to do this or already have working examples that show you how to do it, because working this out yourself from scratch would be very ambitious. Finally, once you have all the parts working you need to write the overall sketch which implements the control logic that Henry_Best describes.

If you run into trouble along the way you can always come here for help and advice, but don’t expect us to do the work for you.

PeterH:

jeff277: so how am I gonna makes it work?

You will build the hardware and write the software yourself. If you want somebody else to do it for you you're in the wrong place, although there is a Gigs & Collaborations section where you can ask people to do that. Note that most people would expect to be paid for working for you.

What you're trying to do doesn't sound especially difficult and there's no reason why you couldn't write it for yourself, if you're willing to put the effort in. However, it's quite ambitious as a first project and I suggest you should tackle it in small steps. As a starting point, get familiar with uploading the standard example sketches to your Arduino and how to see the serial output from them. Then learn how to connect and power a servo and how to write a sketch to control a single servo. It's quite easy and there are lots of examples. Then you will need to learn how to connect your colour sensors and read sensor values into your sketch, and then how to identify the colour from the sensor values. Hopefully you already know how to do this or already have working examples that show you how to do it, because working this out yourself from scratch would be very ambitious. Finally, once you have all the parts working you need to write the overall sketch which implements the control logic that Henry_Best describes.

If you run into trouble along the way you can always come here for help and advice, but don't expect us to do the work for you.

I have done with the colour recognising part but I dont have any idea how to make servos work together with colour sensing part... I face the problem on how should I implement the servos code and make it work with colour sorting that why I come here to ask for help..I am just 17 and dont really know much about Arduino. I had tried many times but coudnt fgure it out on how it works. So I just come to ask or help..

This is quite a tricky project for a first one. Echoing PeterH, I'd suggest you try a couple of precursor projects to gather the skills you need.

  1. Get your program to serial.Print a message to tell you what the sensor sees: "red" "blue" "green" "nothing"

  2. Fix a pointer (pencil, chopstick, whatever) onto a servo horn and make the pointer point to a different place for each colour the sensor sees using servo.write. Initially perhaps, just mark each location with a piece of tape with the appropriate colour written on it.

Those two things are fairly simple to do based on the code you have and some servo example code, but they would allow you to do the fundamentals of the machine in your second youtube link. Then it's "just" a question of building the mechanicals. You could put the balls into your sorter by hand to start with and add some kind of release mechanism driven by another servo later.

You're going to have to write some code though - you'll get a lot of help here, but only rarely will folks actually write complete programs for you and I don't expect that to happen here.

wildbill: This is quite a tricky project for a first one. Echoing PeterH, I'd suggest you try a couple of precursor projects to gather the skills you need.

  1. Get your program to serial.Print a message to tell you what the sensor sees: "red" "blue" "green" "nothing"

  2. Fix a pointer (pencil, chopstick, whatever) onto a servo horn and make the pointer point to a different place for each colour the sensor sees using servo.write. Initially perhaps, just mark each location with a piece of tape with the appropriate colour written on it.

Those two things are fairly simple to do based on the code you have and some servo example code, but they would allow you to do the fundamentals of the machine in your second youtube link. Then it's "just" a question of building the mechanicals. You could put the balls into your sorter by hand to start with and add some kind of release mechanism driven by another servo later.

You're going to have to write some code though - you'll get a lot of help here, but only rarely will folks actually write complete programs for you and I don't expect that to happen here.

Yes..I face the problem joining 2 code..colour recognising and the servos movement code....I need some help to join the code so that it can work..I have tried but fail..that why I need help...

Really need someone help me joining the colour sorting code and servos code. All help will be much appreciated

jeff277: Really need someone help me joining the colour sorting code and servos code.

The colour reading code is pretty complex and it's obviously not written by somebody with the level of understanding implied by your posts here.

I'm not going to do your homework for you. If you make an honest attempt and run into problems, perhaps I or somebody else here will be able to make suggestions about how you could tackle the problems. But if you think that you can just post up somebody else's example code and get an expert to modify it to suit your requirements for free then I think you are going to be disappointed. You've had two reasonably detailed replies now suggesting how you should break the problem down into manageable parts and then tackle them. But at the end of the day YOU need to do it.