Hello everyone!
I am using TCS-230 Library for my Color Sensor TCS-230
Here is Library Link: GitHub - MajicDesigns/MD_TCS230: TCS230 TCS3200 RGB Color Sensor Library
My Question is when i'm using Library i can't move Servo motor, Why ??? Even all pins are OK (~PWM)
Please anyone who know about Color Sensor & TCS-230 Library ... Help
CODE:
// TCS230 sensor calibration and color readings
//
// Input and output using the Serial console.
//
#include <MD_TCS230.h>
#include <FreqCount.h>
#include <Servo.h>
Servo topServo;
Servo bottomServo;
#define BLACK_CAL 0
#define WHITE_CAL 1
#define READ_VAL 2
static int Red, Green, Blue;
// Pin definitions
#define S2_OUT 12
#define S3_OUT 13
#define OE_OUT 8 // LOW = ENABLED
#define S0_OUT 4
#define S1_OUT 7
//uint8_t setFrequency(uint8_t);
//bool setEnable(bool);
MD_TCS230 CS(S2_OUT, S3_OUT, OE_OUT);
//MD_TCS230 CS(S2_OUT, S3_OUT, S0_OUT, S1_OUT, OE_OUT);
char onOff;
void setup()
{
Serial.print(F("\n[TCS230 Calibrator Example]"));
Serial.begin(9600);
CS.begin();
// Servo's PIN Configuration
topServo.attach(11);
bottomServo.attach(6);
/*
pinMode(S2_OUT, OUTPUT);
pinMode(S3_OUT, OUTPUT);
pinMode(S0_OUT, OUTPUT);
pinMode(S1_OUT, OUTPUT);
*/
//pinMode(5, INPUT);
// Servo's Initial Position
topServo.write(110);
bottomServo.write(90);
onOff = 'o';
//setFrequency(TCS230_FREQ_HI);
//setEnable(true);
digitalWrite(OE_OUT, LOW);
}
char getChar()
// blocking wait for an input character from the input stream
{
while (Serial.available() == 0)
;
return (toupper(Serial.read()));
}
void clearInput()
// clear all characters from the serial input
{
while (Serial.read() != -1)
;
}
uint8_t fsmReadValue(uint8_t state, uint8_t valType, uint8_t maxReads)
// Finite State Machine for reading a value from the sensor
// Current FSM state is passed in and returned
// Type of value being read is passed in
{
static uint8_t selChannel;
static uint8_t readCount;
static sensorData sd;
switch (state)
{
case 0: // Prompt for the user to start
Serial.print(F("\n\nReading value for "));
switch (valType)
{
case BLACK_CAL: Serial.print(F("BLACK calibration")); break;
case WHITE_CAL: Serial.print(F("WHITE calibration")); break;
case READ_VAL: Serial.print(F("DATA")); break;
default: Serial.print(F("??")); break;
}
Serial.print(F("\nPress any key to start ..."));
state++;
break;
case 1: // Wait for user input
if (valType != READ_VAL)
getChar();
clearInput();
state++;
break;
case 2: // start the reading process
Serial.println("Reading...");
// Move TOP Servo from INITIAL to SENSOR location
if (valType == READ_VAL) {
/*
for(int i = 166; i >= 110; i-- ){
topServo.write(i);
delay(10);
Serial.println("Reading + Top Servo");
}
*/
Serial.println("Move Top Motor to Sensor Position. Then Sensor Reads");
topServo.write(110);
delay(1000);
}
CS.read();
state++;
break;
case 3: // wait for a read to complete
if (CS.available())
{
sensorData sd;
colorData rgb;
switch (valType)
{
case BLACK_CAL:
CS.getRaw(&sd);
CS.setDarkCal(&sd);
/*
Serial.print("Black Calibration Data: ");
Serial.print(sd.value[TCS230_RGB_R]);
Serial.print(" ");
Serial.print(sd.value[TCS230_RGB_R]);
Serial.print(" ");
Serial.print(sd.value[TCS230_RGB_R]);
delay(2000);
*/
break;
case WHITE_CAL:
CS.getRaw(&sd);
CS.setWhiteCal(&sd);
break;
case READ_VAL:
CS.getRGB(&rgb);
Red = rgb.value[TCS230_RGB_R];
Green = rgb.value[TCS230_RGB_G];
Blue = rgb.value[TCS230_RGB_B];
Serial.print(F("\nRGB is ["));
Serial.print(rgb.value[TCS230_RGB_R]);
Serial.print(F(","));
Serial.print(rgb.value[TCS230_RGB_G]);
Serial.print(F(","));
Serial.print(rgb.value[TCS230_RGB_G]);
Serial.print(F("]"));
Serial.println("NUMBERS CHECKED");
checkWhereToMoveBottomServo(Red, Green, Blue);
//delay(1000);
// Drop The Skittle
for (int i = 110; i >= 69; i-- ) {
topServo.write(i);
delay(10);
Serial.println("TOP MOTOR MOVING TO DROP SKITTLE");
}
delay(500);
// Moving Back to Initial Position
for (int i = 69; i <= 166; i++ ) {
topServo.write(i);
delay(10);
Serial.println("TOP MOTOR MOVING Back To Initial Position");
}
break;
}
state++;
}
break;
default: // reset fsm
state = 0;
break;
}
return (state);
}
void checkWhereToMoveBottomServo(int R, int G, int B) {
Serial.println("BOTTOM SERVO MUST ROTATE");
if (R > G && R > B) {
Serial.println("Move BOTTOM SERVO for RED");
bottomServo.write(50);
}
else if (G > R && G > B) {
bottomServo.write(90);
}
else if (B > R && B > G) {
bottomServo.write(130);
}
else {
Serial.println("CAN'T MOVE BOTTOM MOTOR");
}
delay(1000);
}
void loop()
{
static uint8_t runState = 0;
static uint8_t readState = 0;
// Turn Your Machine ON
if (Serial.available() > 0) {
onOff = Serial.read();
}
if (onOff == 's' || onOff == 'S') {
switch (runState)
{
case 0: // calibrate black
readState = fsmReadValue(readState, BLACK_CAL, 2);
if (readState == 0) runState++;
break;
case 1: // calibrate white
readState = fsmReadValue(readState, WHITE_CAL, 2);
if (readState == 0) runState++;
break;
case 2: // read color
Serial.println("topServo.write(69)");
bottomServo.write(69);
delay(2000);
readState = fsmReadValue(readState, READ_VAL, 1);
break;
default:
runState = 0; // start again if we get here as something is wrong
}
}
else {
onOff = 'o';
}
}