[youtube https://www.youtube.com/watch?v=4SgLTb_XeOc?list=UUP3kEXaqtY63GaSKfsqk6WQ]
const int greenLEDPin = 9; //Green pin in the RGB LED
const int redLEDPin = 11; //Red pin in the RGB LED
const int blueLEDPin = 10; //Blue pin in the RGB LED
const int redSensorPin = A0; //Photoresistor no. 1
const int greenSensorPin = A1; //Photoresistor no. 2
const int blueSensorPin = A2; //Photoresistor no. 3
int redValue = 0;
int greenValue = 0;
int blueValue = 0;
//These values can only be from 0 to 255.
int redSensorValue = 0;
int greenSensorValue = 0;
int blueSensorValue = 0;
//These values will be reading from the photoresistors.
void setup() {
Serial.begin(9600);
//Set up the RGB LED pins to be OUTPUT.
pinMode(greenLEDPin, OUTPUT);
pinMode(redLEDPin, OUTPUT);
pinMode(blueLEDPin, OUTPUT);
}
void loop() {
//Set up the XXSensorValues to read from the photoresistors.
redSensorValue = analogRead(redSensorPin);
delay(5);
greenSensorValue = analogRead(greenSensorPin);
delay(5);
blueSensorValue = analogRead(blueSensorPin);
delay(5);
//Print on the serial monitor the values given by the photoresistors.
Serial.print(“Raw Sensor Values \t Red: “);
Serial.print(redSensorValue);
Serial.print(“\t Green: “);
Serial.print(greenSensorValue);
Serial.print(“\t Blue: “);
Serial.print(blueSensorValue);
//XXValue can only be from 0 to 255 because they are define the intensity of the pin on the RGB LED.
redValue = redSensorValue/4;
greenValue = greenSensorValue/4;
blueValue = blueSensorValue/4;
//Print on the serial monitor the values that the LED pin is on.
Serial.print(“Mapped Sensor Values \t Red: “);
Serial.print(redValue);
Serial.print(“\t Green: “);
Serial.print(greenValue);
Serial.print(“\t Blue: “);
Serial.print(blueValue);
//analogWrite is used here to write, instead of HIGH(1) or LOW(0), a certain intensity(0-255) that is more precise.
analogWrite(redLEDPin, redValue);
analogWrite(greenLEDPin, greenValue);
analogWrite(blueLEDPin, blueValue);
}
[youtube https://www.youtube.com/watch?v=QtrTM_tUz3A]