Hi, im making a color sorting machine but i ran into some problems with the code.
The machine is suposed to detect colored balls and sort them with the servo at different angles.
the tcs230 is working giving the color reads but the servo isnt moving at all.
this is the code im using with my arduino uno.
#include <MD_TCS230.h> #include <FreqCount.h> #include <Servo.h>
Servo myservo;
// Pin definitions #define S2_OUT 12 #define S3_OUT 13 #define OE_OUT 8 // LOW = ENABLED
MD_TCS230 CS(S2_OUT, S3_OUT, OE_OUT);
void setup()
{
myservo.attach(9);
Serial.begin(57600);
Serial.println("[TCS230 Simple NON_BLOCKING Example]");
Serial.println("\nMove the sensor to different color to see the RGB value");
Serial.println("Note: These values are being read in without sensor calibration");
Serial.println("and are likely to be far from reality");
CS.begin();
}
void readSensor()
{
static bool waiting;
if (!waiting)
{
CS.read();
waiting = true;
}
else
{
if (CS.available())
{
colorData rgb;
{
myservo.attach(9);
Serial.begin(57600);
Serial.println("[TCS230 Simple NON_BLOCKING Example]");
Serial.println("\nMove the sensor to different color to see the RGB value");
Serial.println("Note: These values are being read in without sensor calibration");
Serial.println("and are likely to be far from reality");
CS.begin();
}
void readSensor()
{
static bool waiting;
if (!waiting)
{
CS.read();
waiting = true;
}
else
{
if (CS.available())
{
colorData rgb;
CS.getRGB(&rgb);
if (rgb.value[TCS230_RGB_R]<100)
{
myservo.write(60);
delay (15);
Serial.print("ahuevo");
}
}
}
void loop()
{
readSensor();
}
This code works, but the servo doesn't move at all; the sketch only skip the servo line, it reads the delay and prints the word.
The sensor is working fine.
I am pretty sure that the servo movement code should be in the 'if' statement if the color sensor has completed reading, with the Serial.print() statements, not outside it. This is probably not your problem but untidy practice. I have also eliminated the double declare of the rgb variable with one in scope for the else part of the statement.
Also I see no logic to move the servo back to any other position once it has moved the first time, so it will appear to be still.