This should get you started. (Please note, I used dummy values for the pins. The LED pins should not be A0 - A2, but on a pin that has the ~ symbol next to it.)
Kinda fancy, but it should work as a good example.
int redPin = A0; //red pin
int greenPin = A1; //green pin
int bluePin = A2; //blue pin
//int S2= 7; //color sensor pin s2 to ard pin 7
//int S3=8; //color sensor pin s3 to ard pin 8
//int outPin=4; //color sensor pin OUT to ard pin 4
// 5 Sensors MAX on an UNO, NANO or similar
// +8 on a MEGA
#define Num_of_Sensors 4
byte Sensors[Num_of_Sensors][3] = {
//{S2, S3, Outpin}
{2, 3, 4}, //Sensor 1
{5, 6, 7}, // Sensor 2
{8, 9, 10}, // Sensor 3
{11, 12, 13} // sensor 4 and so on
};
// JUST MAKE OE TO VCC
//int OE=3; //i have no idea what OE does but it differs from the virtuabotix sensor used in the tutorial. commenters suggest OE must be set to HIGH
byte rColorStrength, gColorStrength, bColorStrength;
byte _sensor = 0;
void setup ()
{
//put your setup code here, to run once:
Serial.begin(115200); //turns on the serial port
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
for (byte i = 0; i < Num_of_Sensors; i++)
{
pinMode(Sensors[i][0], OUTPUT); // S2
pinMode(Sensors[i][1], OUTPUT); // S3
pinMode(Sensors[i][2], INPUT); // output
}
//pinMode(OE, INPUT);
}
void loop ()
{
//digitalWrite(OE, HIGH); // not needed
//put your main code here, to run repeatedly:
//start by reading the red component of the color
//S2 and S3 should be set LOW, as follows:
//Such Fanciness
Color( _sensor , &rColorStrength, &gColorStrength, &bColorStrength); // cycle throught the sensors and collect the colors for each
if ((rColorStrength > gColorStrength) && (gColorStrength > bColorStrength))
{
rColorStrength = 255;
gColorStrength = gColorStrength / 2;
bColorStrength = 0;
}
if (rColorStrength > bColorStrength && bColorStrength > gColorStrength)
{
rColorStrength = 255;
bColorStrength = bColorStrength / 2;
gColorStrength = 0;
}
if (gColorStrength > rColorStrength && rColorStrength > bColorStrength)
{
gColorStrength = 255;
rColorStrength = rColorStrength / 2;
bColorStrength = 0;
}
if (gColorStrength > bColorStrength && bColorStrength > rColorStrength)
{
gColorStrength = 255;
bColorStrength = bColorStrength / 2;
rColorStrength = 0;
}
if (bColorStrength > rColorStrength && rColorStrength > gColorStrength)
{
bColorStrength = 255;
rColorStrength = rColorStrength / 2;
gColorStrength = 0;
}
if (bColorStrength > gColorStrength && gColorStrength > rColorStrength)
{
bColorStrength = 255;
gColorStrength = gColorStrength / 2;
rColorStrength = 0;
}
bColorStrength = float(bColorStrength) * .5;
gColorStrength = float(gColorStrength) * .75;
Serial.print("Sensor: ");
Serial.print(_sensor);
Serial.print(" | ");
Serial.print(rColorStrength);
Serial.print(", ");
Serial.print(gColorStrength);
Serial.print(", ");
Serial.println(bColorStrength);
if(_sensor == 3)
Serial.println("-----------------------");
analogWrite(redPin, rColorStrength);
analogWrite(greenPin, gColorStrength);
analogWrite(bluePin, bColorStrength);
if (_sensor < Num_of_Sensors)
_sensor++;
else
_sensor = 0;
}
void Color(byte idx, byte * R, byte * G, byte * B)
{
const byte colors[3] = {0x0, 0x3, 0x1};
unsigned int pulseWidth[3] = {0};
byte ColorValue;
for (byte C = 0; C < 3; C++)
{
// 0 = RED, 1 = BLUE, 3 = GREEN
digitalWrite(Sensors[idx][0], colors[C] & 0x2); // these strip off the correct bit per color
digitalWrite(Sensors[idx][1], colors[C] & 0x1);
pulseWidth[idx] = pulseIn (Sensors[idx][2], LOW);
ColorValue = (255 - (pulseWidth[idx] / 400) );
switch (C)
{
case 0:
*R = ColorValue;
break;
case 3:
*G = ColorValue;
break;
case 1:
*B = ColorValue;
break;
}
}
}