HELP!!!!!!!!!!!!! problem with color sensor and servo motor code

Help! Anyone

I need a code for when the color sensor detects a certain color like Red or Green or Blue or Clear It will tell the servo to move to a certain location i really need help. i have put some code but it was wrong
it doesnt move fast enough and once a color is detected it moves only to 1 point of location even the nxt color was detected it just move backwards. Please i really do Need Help

I'm a beginner and i really want to learn. Please Help Me! Anyone! Please This Project is worth half of My Life in This Semester :smiley:

A Reply and Help is Appreciated

 #include <Wire.h>
#include <Math.h>
#include <Servo.h>
#include <Arduino.h>

byte i2cWriteBuffer[10];
byte i2cReadBuffer[10];

#define SensorAddressWrite 0x29 //
#define SensorAddressRead 0x29 // 
#define EnableAddress 0xa0 // register address + command bits
#define ATimeAddress 0xa1 // register address + command bits
#define WTimeAddress 0xa3 // register address + command bits
#define ConfigAddress 0xad // register address + command bits
#define ControlAddress 0xaf // register address + command bits
#define IDAddress 0xb2 // register address + command bits
#define ColorAddress 0xb4 // register address + command bits

/*  
Send register address and the byte value you want to write the magnetometer and 
loads the destination register with the value you send
*/
void Writei2cRegisters(byte numberbytes, byte command)
{
    byte i = 0;

    Wire.beginTransmission(SensorAddressWrite);   // Send address with Write bit set
    Wire.write(command);                          // Send command, normally the register address 
    for (i=0;i<numberbytes;i++)                       // Send data 
      Wire.write(i2cWriteBuffer[i]);
    Wire.endTransmission();

    delayMicroseconds(150);      // allow some time for bus to settle      
}

/*  
Send register address to this function and it returns byte value
for the magnetometer register's contents 
*/
byte Readi2cRegisters(int numberbytes, byte command)
{
   byte i = 0;

    Wire.beginTransmission(SensorAddressWrite);   // Write address of read to sensor
    Wire.write(command);
    Wire.endTransmission();

    delayMicroseconds(150);      // allow some time for bus to settle      

    Wire.requestFrom(SensorAddressRead,numberbytes);   // read data
    for(i=0;i<numberbytes;i++)
      i2cReadBuffer[i] = Wire.read();
    Wire.endTransmission();   

    delayMicroseconds(100);      // allow some time for bus to settle      
}  

void init_TCS34725(void)
{
  i2cWriteBuffer[0] = 0x10;
  Writei2cRegisters(1,ATimeAddress);    // RGBC timing is 256 - contents x 2.4mS =  
  i2cWriteBuffer[0] = 0x00;
  Writei2cRegisters(1,ConfigAddress);   // Can be used to change the wait time
  i2cWriteBuffer[0] = 0x00;
  Writei2cRegisters(1,ControlAddress);  // RGBC gain control
  i2cWriteBuffer[0] = 0x03;
  Writei2cRegisters(1,EnableAddress);    // enable ADs and oscillator for sensor  
}

void get_TCS34725ID(void)
{
  Readi2cRegisters(1,IDAddress);
  if (i2cReadBuffer[0] = 0x44)
    Serial.println("TCS34725 is present");    
  else
    Serial.println("TCS34725 not responding");    
}

/*
Reads the register values for clear, red, green, and blue.
*/
int ledpin=11;

void get_Colors(void)
{
  unsigned int clear_color = 0;
  unsigned int red_color = 0;
  unsigned int green_color = 0;
  unsigned int blue_color = 0;

  Servo myservo;
  myservo.attach(9);
  
  Readi2cRegisters(8,ColorAddress);
  clear_color = (unsigned int)(i2cReadBuffer[1]<<8) + (unsigned int)i2cReadBuffer[0];
  red_color = (unsigned int)(i2cReadBuffer[3]<<8) + (unsigned int)i2cReadBuffer[2];
  green_color = (unsigned int)(i2cReadBuffer[5]<<8) + (unsigned int)i2cReadBuffer[4];
  blue_color = (unsigned int)(i2cReadBuffer[7]<<8) + (unsigned int)i2cReadBuffer[6];

  // send register values to the serial monitor 
/*
  Serial.print("clear color=");
  Serial.print(clear_color, DEC);    
  Serial.print(" red color=");
  Serial.print(red_color, DEC);    
  Serial.print(" green color=");
  Serial.print(green_color, DEC);    
  Serial.print(" blue color=");
  Serial.println(blue_color, DEC);
*/

 // Basic RGB color differentiation can be accomplished by comparing the values and the largest reading will be 
 // the prominent color

  if((red_color>blue_color) && (red_color>green_color))
  {
    Serial.println("detecting red");
     myservo.write(90);  // set servo to mid-point
  }
  else if((green_color>blue_color) && (green_color>red_color))
  {
    Serial.println("detecting green");
    myservo.write(270);
  }
  else if((blue_color>red_color) && (blue_color>green_color))
  {
    Serial.println("detecting blue");
    myservo.write(180);
  }a
  else
  {
    Serial.println("color not detectable");
    myservo.write(180);
  }
}  

void setup() 
{
  Wire.begin();
  Serial.begin(9600);  // start serial for output
  pinMode(ledpin,OUTPUT);
  init_TCS34725();
  get_TCS34725ID();     // get the device ID, this is just a test to see if we're connected
}

void loop()
{
    digitalWrite(ledpin,HIGH);
    get_Colors();
    delay(1000);
}

code for color and servo.txt (4.51 KB)

You have some Serial.print statements in there. What are those telling you? Are you detecting a color at all? Having the servo created inside the color function concerns me. I don't know what the servo will do when it goes out of scope.

You've tried to describe the action of the servo, but I'm not quite following this part.

it doesnt move fast enough and once a color is detected it moves only to 1 point of location even the nxt color was detected it just move backwards

Where does the servo actually go?

Hi Thank You For An Awesome Response! I Appreciate it!

Yes! i'm detecting colors and it was an accurate one but

i coded there the servo.angle to 90 , 180, 270 when it initiates the loop the servo turns from the point where detection of e.g red it goes to 90 then when nxt one when it detects like green or blue it just turns backwards and not on the point where it should go.

i actually want my servo once it detects a color it would go to the location and then stop then when another loop turns it goes to the another point when a new color is detected.

im really a beginner so i don't have any other clues to do this. can you help me please. but for now i want the servo to recognize the point of location when the color sensor tells him.

TY bro!

Try moving this code from the get_Colors function. I don't know enough about the Servo library to know if it really matters, but what you have is very unorthidox.

Servo myservo;
  myservo.attach(9);

Move the first line to global scope at the top up where you have i2cWriteBuffer and i2cReadBuffer defined.
Move the second line to the setup() function. Anywhere in setup would be fine.

If that doesn't change anything we can look deeper.

Ok Bro! I'll Give it a Try Right Now.

#7 below:

http://forum.arduino.cc/index.php/topic,148850.0.html

Thank You Kat for reminding me the rules , im gonna have to do it right next time i post a code

forelectronics02:
Thank You Kat for reminding me the rules , im gonna have to do it right next time i post a code

You can go back and modify your previous post to use code boxes via the edit option in the lower right of your post.

Hello @delta_G it works bro, but can you tell me how am i going to set the motor to be accurate and stops then only works when it detects a color? All The Best! Bro You Rock!

forelectronics02:
Hello @Delta_G it works bro, but can you tell me how am i going to set the motor to be accurate and stops then only works when it detects a color? All The Best! Bro You Rock!

If you knew me, you'd be a lot more likely to call me sir than bro. Please stop that. I am nobody's "bro".

I'm not sure what you mean by your question. What do you want to change about how it works?