Color Sensor TCS-230 Library and Servo Motor

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';
}

}

Please use code tags

Means ?

Problem is whenever i use this Library, i always cannot move my Servo. I'm working on my project i-e Skittles Sorting Machine.
Any other method that i can use ? I need to calibrate sensor first and then use different color values.

Please use code tags means do your part of the work and read how to use the forum and post code... :slight_smile:

From the source code documentation

/*
This library has a dependency on the FreqCount library for   frequency counting. FreqCount library is available at  http://www.pjrc.com/teensy/td_libs_FreqCount.html


  ** IMPORTANT NOTE** 
  FreqCount imposes a limitation that the frequency can only be 
  counted on specific pins and limits the use of other pins as 
  follows:
  Board       Input Pin Pins Unusable with analogWrite()
  ----------- --------- --------------------------------  
  Arduino Uno     5      3, 9, 10, 11
  Arduino 2009    5      3, 9, 10, 11
  Arduino Mega   47      9, 10, 44, 45, 46
  Sanguino        1      12, 13, 14, 15
*/

So if you are on a uno you probably have a conflict ont pin11

Yes Sir! i'm using UNO. I have two Servo's on PIN 11 & 6.
I know freqCount Library disables analogWrite(). But my PIN 6 Servo also not working.

bottomServo.write(90) Not working

The FreqCount library on a uno uses timers 1 and 2 as [url=https://github.com/PaulStoffregen/FreqCount/blob/master/util/FreqCountTimers.h you can see in the code[/url]

// Arduino Duemilanove, Diecimila, LilyPad, Mini, Fio, etc
#elif defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)
  #define COUNTER_USE_TIMER1    // T1 is pin 5
  #define TIMER_USE_TIMER2

PWM Relies as well on timers on a uno
Pins 5 and 6: controlled by Timer0
Pins 9 and 10: controlled by Timer1
Pins 11 and 3: controlled by Timer2

As timer1 and 2 and used by the frequency lib, that's why the pins 3,9,10,11 cannot be used for PWM/analogWrite

Now the Servo library uses Timer1 as well on Arduino Uno (and interrupts) so this creates a challenge for your program, as two parts of the code play with Timer1

If you have a mega you could move to other timers - both libraries offers a .h configuration file.

Thankyou sir :slight_smile: Really helped alot

Hi,

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks... Tom... :slight_smile: