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
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)