I am sorry I am new to this, thanks for sharing the link!
Arduino code:
#include <Servo.h>
#include <Wire.h> //Libraries for I2C
byte rec = 0x00;
int green,red,blue,cle;
int gh,gl;
int rh,rl;
int bh,bl;
int ch,cl;
double X,Y,Z,x,y,z;
Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
void setup() {
Wire.begin(); //join i2c bus (address optional for master)
Serial.begin(9600);//start serial communication at 9600bps
myservo.attach(12); // attaches the servo on pin 12 to the servo object
myservo.write(pos);
}
void loop() {
for (pos = 0; pos <= 180; pos +=45) { // goes from 0 degrees to 180 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(100);
Wire.beginTransmission(0x39); //Start I2C Address for Color sensor
Wire.write(0x80);
Wire.write(0x03); // Enable ADC
Wire.endTransmission();//Stop transmitting
delay(2000);//Delay 100 ms.
Red();//Function Red Value
Green();//Function Green Value
Blue();//Function Blue Value
Clear();//Function Clear Value
//Serial.write("Red: ");
Serial.println(red, DEC);// Show Red Value to Serial monitor
//Serial.write("Green: ");
Serial.println(green, DEC);// Show Green Value to Serial monitor
//Serial.write("Blue: ");
Serial.println(blue, DEC); // Show Blue Value to Serial monitor
//Serial.print('\n');
delay(100);
} // waits 15ms for the servo to reach the position
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
myservo.detach();
while(1) {
//do nothing in here to stop color sensor
}
}
/********************************************************************************/
/*****************Function Green Value******************************************/
//code to read color values from company website http://www.gravitech.us/i2c16coleser.html
void Green()
{
Wire.beginTransmission(0x39);//Start I2C Address for Color sensor
Wire.write(0x90); //Address Green lower Data for Using Read Byte Protocol
Wire.endTransmission();//Stop transmitting
Wire.beginTransmission(0x39);//Start I2C Address for Color sensor
Wire.requestFrom(0x39, 1);//Request Green lower Data for color sensor
gl = Wire.read(); //receive a byte as character
Wire.endTransmission();//Stop transmitting
Wire.beginTransmission(0x39);//Start I2C Address for Color sensor
Wire.write(0x91);//Address Green upper Data for Using Read Byte Protocol
Wire.endTransmission();//Stop transmitting
Wire.beginTransmission(0x39);//Start I2C Address for Color sensor
Wire.requestFrom(0x39, 1);//Request Green upper Data for color sensor
gh = Wire.read();//receive a byte as character
Wire.endTransmission();//Stop transmitting
green = (256*gh)+gl;//Result returned in character
}
/*****************Function Red Value******************************************/
void Red()
{
Wire.beginTransmission(0x39);//Start I2C Address for Color sensor
Wire.write(0x92);//Address Red lower Data for Using Read Byte Protocol
Wire.endTransmission();//Stop transmitting
Wire.beginTransmission(0x39);//Start I2C Address for Color sensor
Wire.requestFrom(0x39, 1);//Request Red lower Data for color sensor
rl = Wire.read();//receive a byte as character
Wire.endTransmission();//Stop transmitting
Wire.beginTransmission(0x39);//Start I2C Address for Color sensor
Wire.write(0x93);//Address Red upper Data for Using Read Byte Protocol
Wire.endTransmission();//Stop transmitting
Wire.beginTransmission(0x39);//Start I2C Address for Color sensor
Wire.requestFrom(0x39, 1);//Request Red upper Data for color sensor
rh = Wire.read();//receive a byte as character
Wire.endTransmission();//Stop transmitting
red = (256*rh)+rl;//Result returned in character
}
/*****************Function Blue Value******************************************/
void Blue()
{
Wire.beginTransmission(0x39);//Start I2C Address for Color sensor
Wire.write(0x94);//Address Blue lower Data for Using Read Byte Protocol
Wire.endTransmission();//Stop transmitting
Wire.beginTransmission(0x39);//Start I2C Address for Color sensor
Wire.requestFrom(0x39, 1);//Request Blue lower Data for color sensor
bl = Wire.read();//receive a byte as character
Wire.endTransmission();//Stop transmitting
Wire.beginTransmission(0x39);//Start I2C Address for Color sensor
Wire.write(0x95);//Address Blue upper Data for Using Read Byte Protocol
Wire.endTransmission();//Stop transmitting
Wire.beginTransmission(0x39);//Start I2C Address for Color sensor
Wire.requestFrom(0x39, 1);//Request Blue upper Data for color sensor
bh = Wire.read();//receive a byte as character
Wire.endTransmission();//Stop transmitting
blue = (256*bh)+bl;//Result returned in character
}
/*****************Function Clear Value******************************************/
void Clear()
{
Wire.beginTransmission(0x39);//Start I2C Address for Color sensor
Wire.write(0x96);//Address Clear lower Data for Using Read Byte Protocol
Wire.endTransmission();//Stop transmitting
Wire.beginTransmission(0x39);//Start I2C Address for Color sensor
Wire.requestFrom(0x39, 1);//Request Clear lower Data for color sensor
cl = Wire.read();//receive a byte as character
Wire.endTransmission();//Stop transmitting
Wire.beginTransmission(0x39);//Start I2C Address for Color sensor
Wire.write(0x97);//Address Clear upper Data for Using Read Byte Protocol
Wire.endTransmission();//Stop transmitting
Wire.beginTransmission(0x39);//Start I2C Address for Color sensor
Wire.requestFrom(0x39, 1);//Request Clear upper Data for color sensor
ch = Wire.read();//receive a byte as character
Wire.endTransmission();//Stop transmitting
cle = (256*ch)+cl;//Result returned in character
}
/**************************END**********************************/
Processing:
import processing.serial.*; // add the serial library
Serial myPort; // the serial port to monitor
//int a1, a2, a3, b1, b2, b3, c1, c2, c3, d1, d2, d3, e1, e2, e3;
int Y=1;
color a, b, c, d, e;
void setup() {
size(800, 400); // set the window size, size() also sets width and height variables
printArray(Serial.list()); // list all available serial ports
myPort = new Serial(this, Serial.list()[1], 9600); // define input port
myPort.clear(); // clear the port of any initial junk
//noLoop();
}
void draw () {
if (myPort.available () > 0) { // make sure port is open
String inString = myPort.readStringUntil('\n');
if (inString != null) { // ignore null strings
inString = trim(inString); // trim off any whitespace
String[] array = splitTokens(inString, "\t"); // extract values into an array
// proceed only if correct # of values extracted from the string:
if (array.length >= 15) {
int a1 = int(array[0]);
int a2 = int(array[1]);
int a3 = int(array[2]);
int b1 = int(array[3]);
int b2 = int(array[4]);
int b3 = int(array[5]);
int c1 = int(array[6]);
int c2 = int(array[7]);
int c3 = int(array[8]);
int d1 = int(array[9]);
int d2 = int(array[10]);
int d3 = int(array[11]);
int e1 = int(array[12]);
int e2 = int(array[13]);
int e3 = int(array[14]);
print(a1);
print(a2);
print(a3);
print(b1);
print(b2);
print(b3);
print(c1);
print(c2);
print(c3);
print(d1);
print(d2);
print(d3);
print(e1);
print(e2);
print(e3);
a=color(a1,a2,a3);
b=color(b1,b2,b3);
c=color(c1,c2,c3);
d=color(d1,d2,d3);
e=color(e1,e2,e3);
ombre(0, 0, 800, 100 , a, b, Y); //rectange 1
ombre(0, 100, 800, 100 , b, c, Y); //rectange 2
ombre(0, 200, 800, 100 , c, d, Y); //rectange 3
ombre(0, 300, 800, 100 , d, e, Y); //rectange 4
}
}
}
}
void ombre(int x, int y, float w, float h, color c1, color c2, int axis) { //function to create ombre
noFill();
if(axis == Y) { //creates gradient for colors in respective rectanges on y axis
for( int i = y; i <= y+h; i++) { //colors stay in the rectange borders
float range = map(i,y,y+h,0,1); //map(value, start1, stop1, start2, stop2) creates range for colors
color c = lerpColor(c1, c2, range); //calculates colors in between two colors
stroke(c); //sets color to draw lines
line(x,i,x+w,i); // first x point, first y point, second x point, second y point
}
}
}