I am using a 28BYJ-48 stepper motor to rotate a disc that holds M&Ms to be detected and sorted by color using the TCS3200 color sensor.
Here is the code:
#include <tcs3200.h>
#include <Servo.h>
#include <Stepper.h>
#define DecisionServoPin 9
#define numColors 6
#define S0 4
#define S1 5
#define S2 7
#define S3 6
#define sensorOut 8
Servo DecisionServo;
tcs3200 colorSensor(S0, S1, S2, S3, sensorOut);
Stepper myStepper(2048, 13, 11, 12, 10);
unsigned long stepperDelay = 1000; // Delay between stepper motor steps (in milliseconds)
unsigned long colorSenseInterval = 500; // Interval between color sensing (in milliseconds)
unsigned long previousStepperTime = 0;
unsigned long previousColorSenseTime = 0;
bool isStepperMoving = false;
int color = 0;
int R;
int B;
int G;
int RedM = 0;
int OrangeM = 0;
int GreenM = 0;
int YellowM = 0;
int BrownM = 0;
int BlueM = 0;
int TotalM = 0;
void setup() {
DecisionServo.attach(DecisionServoPin);
Serial.begin(9600);
}
void loop() {
unsigned long currentMillis = millis();
// Color sensing
if (currentMillis - previousColorSenseTime >= colorSenseInterval) {
colorSensing();
previousColorSenseTime = currentMillis;
}
myStepper.setSpeed(15);
// Stepper motor movement
if (isStepperMoving) {
if (currentMillis - previousStepperTime >= stepperDelay) {
myStepper.step(2048 / 8);
previousStepperTime = currentMillis;
isStepperMoving = false; // Pause after each stepper motor movement
}
} else {
isStepperMoving = true;
}
switch (color) {
case 1:
DecisionServo.write(0);
break;
case 2:
DecisionServo.write(30);
break;
case 3:
DecisionServo.write(60);
break;
case 4:
DecisionServo.write(120);
break;
case 5:
DecisionServo.write(150);
break;
case 6:
DecisionServo.write(180);
break;
case 0:
break;
}
delay(500);
}
void colorSensing() {
R = colorSensor.colorRead('r', 100); //reads color value for red
Serial.print("R= ");
Serial.print(R);
Serial.print(" ");
G = colorSensor.colorRead('g', 100); //reads color value for green
Serial.print("G= ");
Serial.print(G);
Serial.print(" ");
B = colorSensor.colorRead('b', 100); //reads color value for blue
Serial.print("B= ");
Serial.print(B);
Serial.print(" ");
Serial.println();
if (R <= 38 && R >= 35 && G <= 45 && G >= 27 && B <= 52 && B >= 33) {
color = 1; // Red
RedM++;
TotalM++;
Serial.println("Red: " + String(RedM));
} else if (R <= 66 && R >= 62 && B <= 58 && B >= 52 && G <= 52 && G >= 50) {
color = 2; // Orange
OrangeM++;
TotalM++;
Serial.println("Orange: " + String(OrangeM));
} else if ( R <= 45 && R >= 43 && G <= 55 && G >= 52 && B <= 58 && B >= 55) {
color = 3; // Green
GreenM++;
TotalM++;
Serial.println("Green: " + String(GreenM));
} else if (R <= 71 && R >= 62 && B <= 62 && B >= 58 && G <= 62 && G >= 58) {
color = 4; // Yellow
YellowM++;
TotalM++;
Serial.println("Yellow: " + String(YellowM));
} else if (R <= 40 && R >= 35 && B <= 45 && B >= 35 && G <= 43 && G >= 35) {
color = 5; // Brown
BrownM++;
TotalM++;
Serial.println("Brown: " + String(BrownM));
} else if (R <= 45 && R >= 38 && B <= 71 && B >= 66 && G <= 50 && G >= 45){
color = 6; // Blue
BlueM++;
TotalM++;
Serial.println("Blue: " + String(BlueM));
} else {
color = 0; // Undetermined
}
}
void termination() {
myStepper.setSpeed(0);
Serial.println("Total Red: " + String(RedM));
Serial.println("Total Orange: " + String(OrangeM));
Serial.println("Total Green: " + String(GreenM));
Serial.println("Total Yellow: " + String(YellowM));
Serial.println("Total Brown: " + String(BrownM));
Serial.println("Total Blue: " + String(BlueM));
Serial.println("Total: " + String(TotalM));
delay(5000);
}
The sensor detects colors when calibrated but as soon as I make it rotate it fails to detect the colors calibrated. I am looking for a solution, I ruled out ambient light because the sensor is covered, motion blur also because the stepper stops under the sensor.
thanks in advance.