M&M sorting machine with a Adafruit TCS34725 RGB Sensor

Today at 02:44 pm
Hey guys,

im making a pneumatic M&M sorting machine for a school project this is what i have so far. I can't figure out how power the else if funtions with with the right colour. and i keep getting errormessags at the if and if else funtion. does any one have some tips or adjustments? I want the code to take the right action after the sensor reads the colour, however i dont know how to make my code do that. ive been googeling and watching how to youtube videos for hours now :cry:

thanks in advance!!!

#include <Wire.h>
#include "Adafruit_TCS34725.h"

byte gammatable(256);

Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);

void setup()
{

Serial.begin(9600);
Serial.println("GOEIE MIDDAG!");
if (tcs.begin()){      //checken is there is a sensor
Serial.println("sensor gevonden");
} else {
Serial.println("mongool je hebt het verkeerd gedaan");
while (1); //programma stopt
}




//PINS 2,3,4,5 --> S0-S3  
//PIN 6 --> Signal OUT
pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT); 
pinMode(7, INPUT); 
pinMode(A4, INPUT);
pinMode(A5, INPUT);


digitalWrite(2,HIGH); 
digitalWrite(3,LOW);  

}


}

void loop()

{
uint16_t clear, red, green, blue;  //variable aantonen
delay (60);

tcs.getRawData(&red, &green, &blue, &clear);

Serial.print("C:\t"); Serial.print(clear); //kleuren weergeven
Serial.print("\tR:\t"); Serial.print(red);
Serial.print("\tG:\t"); Serial.print(green);
Serial.print("\tB:\t"); Serial.print(blue);

uint32_t sum = clear;           //hex code omzetten 
float r, g, b;
r = rood; r /= sum;
g = groen; g /= sum;
b = blauw; b /= sum;
r *= 256; g *= 256; b *= 256;
Serial.print("\t");
Serial.print((int)r, HEX); Serial.print((int)g, HEX); Serial.print((int)b, HEX);
Serial.println();

}

{ if (R<256 && B<256 && G<256){

Serial.println("red");
digitalWrite(1, HIGH); //cilinder in
delay(1000);
digitalWrite(2, HIGH); //cillinder out
digitalWrite(3, HIGH); //Ventiel aansturen voor groene M&M
delay(1000);
}


else if (R<256 && B<256 && G<256){
Serial.println("green");
digitalWrite(1, HIGH); //cilinder in
delay(1000);
digitalWrite(2, HIGH); //cillinder out
digitalWrite(4, HIGH); //Ventiel aansturen voor groene M&M
delay(1000);
}



else if (R<256 && B<256 && G<256){

Serial.println("blue");
digitalWrite(1, HIGH); //cilinder in
delay(1000);
digitalWrite(2, HIGH); //cillinder out
digitalWrite(5, HIGH); //Ventiel aansturen voor blauw M&M
delay(1000);
}

else if (R<256 && B<256 && G<256){
Serial.println("no colour");
digitalWrite(1, HIGH); //cilinder in
delay(1000);
digitalWrite(2, HIGH); //cillinder out
digitalWrite(6, HIGH); //ventiel aansturen voor geen kleur
delay(1000);
}
}
delay(1000);
}

[code]

(deleted)

I know I deleted it on the other forum!

Please edit your post to add code tags ("</>"), and explain the problem.

See the "How to use this forum" post for instructions.

For a starter, what is this code doing for you? Seems pretty useless:

for (int i=0; i<256; i++) {
float x = i;
x /=255;
x = pow(x, 2.5);
x *= 255;

}

I got that code out of this video: Adafruit TCS34725 RGB Sensor Demo - YouTube i tought it was necessary.....

No one is going to help you if you post nonsense code that someone else has written and you don't understand the code yourself. It's the worst way to learn anything. Go back to basics, learn simple things and understand the code you write yourself. That's the point where everybody can help you.

I know that, that is the best way however I tought I would give it a shot and try my best. It is a combination of my own code and someone else his code, the sensor code is his.
And since school didn't learn us how to code it im looking for alternative ways of learning how it works through this forum.

On this forum, we strongly recommend that you study the program examples that come with the Arduino, and the many tutorials that we and others post, in order to learn the programming language and special features of the Arduino.

Forget about YouTube.

When you understand those examples, you might be ready to start on a sorting machine.

I looked at the video. The code I quoted, you didn't copy it completely! It was about gamma correction. Google that! But the problem remains. You won't learn much if you post code here, code that you don't understand yourself. What were you thinking would happen? People would correct your code, you would get it running, you would have learned how everything works? That won't happen if you don't break your whole project into tiny bits that you understand and are able to code yourself. In the tiny bits it's easier to actually learn something even if someone shows you how to write the code.

That said, colour recognition is fun and interesting. I've tried to do it myself, using only an LDR and an RGB LED. Works pretty well. I wish you luck.

I would love to learn more about codeing thats why im studying engineering however school gave me this assigment after teaching us how to make a led blink.... the only info they gave u was that we had to use a tcs34725 RGB sensor. if been watching videos on rgb sensors and sorting machines for hours now and made this code, i was hoping any one could help me correct it.

i started with a sequential function chart diagram and wrote the actions. but I cant figure out how to transfer the RGB sensor readings to the correct data and read it out correctly. thanks for the tips tho really appreciate it. I know it is way out of my league but the teachers gave me this assigment and im tying my best to get it to work got the project book infront of me to assist me but the sensor is really hard

You have two } at the end of setup; that's one too many.

You assign the non-existing variables rood, groen and blauw to r, g and b; you use the non-existing variables red, green and blue in tcs.getRawData(...).

And be consistent in the naming; rood is the same as red etc (I know that, I assume you also know that), so use one of them throughout your code.

Variable r is not the same as variable R; C/C++ is case sensitive. Same for the other one-letter variables.

The below line (and everything following that) is outside any function; you probably closed loop() too early with a }

{ if (R < 256 && B < 256 && G < 256) {

Thank you so much i don't have error messages any more. yess i changed rood into red for the website so it is easyer to understand, im gonna fight some more with this code and find out how the sensor works