Okay so i have this code with this project that i want to build.
It is a color sorter.
Problem here is that i don't use the same color sensor but a different one, this one:
And is hell... i cannot get the colosrs that i want becomes everything overlap.
I've only added two colors because everytime that i try to add more to my if statements then nothing works...
Any tips?!
#include <Servo.h>
#include <Wire.h>
#include "Adafruit_TCS34725.h"
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_700MS, TCS34725_GAIN_1X);
Servo topServo;
Servo bottomServo;
int color;
void setup(void)
{
Serial.begin(9600);
topServo.attach(7);
bottomServo.attach(8);
if (tcs.begin())
{
Serial.println("Found sensor");
} else
{
Serial.println("No TCS34725 found ... check your connections");
while (1);
}
}
void loop()
{
topServo.write(115);
delay(500);
for (int i = 115; i > 65; i--) //Picking up a candy to read.
{
topServo.write(i);
delay(2);
}
uint16_t r, g, b, c, colorTemp, lux; //Reading the RGB for the candy.
tcs.getRawData(&r, &g, &b, &c);
colorTemp = tcs.calculateColorTemperature(r, g, b);
lux = tcs.calculateLux(r, g, b);
Serial.print("R: "); Serial.print(r, DEC); Serial.print(" ");
Serial.print("G: "); Serial.print(g, DEC); Serial.print(" ");
Serial.print("B: "); Serial.print(b, DEC); Serial.print(" ");
Serial.println(" ");
if (r > 7000) //If red is dominante then do something.
{
color = 1;
}
else if (g > 6000) //If green is dominante then do something.
{
color = 2;
}
else
{
color = 0;
}
delay(5000);
switch (color)
{
case 1:
bottomServo.write(50); //Change the path and then drop the candy.
delay(300);
for (int i = 65; i > 29; i--)
{
topServo.write(i);
delay(2);
}
delay(200);
for (int i = 29; i < 115; i++)
{
topServo.write(i);
delay(2);
}
break;
case 2:
bottomServo.write(90); //Change the path and then drop the candy.
delay(300);
for (int i = 65; i > 29; i--)
{
topServo.write(i);
delay(2);
}
delay(200);
for (int i = 29; i < 115; i++)
{
topServo.write(i);
delay(2);
}
break;
case 0:
bottomServo.write(70);
delay(300);
for (int i = 65; i > 29; i--)
{
topServo.write(i);
delay(2);
}
delay(200);
for (int i = 29; i < 115; i++)
{
topServo.write(i);
delay(2);
}
break;
}
//Read again.
tcs.getRawData(&r, &g, &b, &c);
colorTemp = tcs.calculateColorTemperature(r, g, b);
lux = tcs.calculateLux(r, g, b);
Serial.print("R: "); Serial.print(r, DEC); Serial.print(" ");
Serial.print("G: "); Serial.print(g, DEC); Serial.print(" ");
Serial.print("B: "); Serial.print(b, DEC); Serial.print(" ");
Serial.println(" ");
}