/*
Arduino Starter Kit example
Project 4 - Color Mixing Lamp
This sketch is written to accompany Project 3 in the Arduino Starter Kit
Parts required:
- one RGB LED
- three 10 kilohm resistors
- three 220 ohm resistors
- three photoresistors
- red green and blue colored gels
created 13 Sep 2012
modified 14 Nov 2012
by Scott Fitzgerald
Thanks to Federico Vanzati for improvements
https://store.arduino.cc/genuino-starter-kit
This example code is part of the public domain.
*/constintgreenLEDPin=9;// LED connected to digital pin 9
constintredLEDPin=10;// LED connected to digital pin 10
constintblueLEDPin=11;// LED connected to digital pin 11
constintredSensorPin=A0;// pin with the photoresistor with the red gel
constintgreenSensorPin=A1;// pin with the photoresistor with the green gel
constintblueSensorPin=A2;// pin with the photoresistor with the blue gel
intredValue=0;// value to write to the red LED
intgreenValue=0;// value to write to the green LED
intblueValue=0;// value to write to the blue LED
intredSensorValue=0;// variable to hold the value from the red sensor
intgreenSensorValue=0;// variable to hold the value from the green sensor
intblueSensorValue=0;// variable to hold the value from the blue sensor
voidsetup(){// initialize serial communications at 9600 bps:
Serial.begin(9600);// set the digital pins as outputs
pinMode(greenLEDPin,OUTPUT);pinMode(redLEDPin,OUTPUT);pinMode(blueLEDPin,OUTPUT);}
voidloop(){// Read the sensors first:
// read the value from the red-filtered photoresistor:
redSensorValue=analogRead(redSensorPin);// give the ADC a moment to settle
delay(5);// read the value from the green-filtered photoresistor:
greenSensorValue=analogRead(greenSensorPin);// give the ADC a moment to settle
delay(5);// read the value from the blue-filtered photoresistor:
blueSensorValue=analogRead(blueSensorPin);// print out the values to the Serial Monitor
Serial.print("raw sensor Values \t red: ");Serial.print(redSensorValue);Serial.print("\t green: ");Serial.print(greenSensorValue);Serial.print("\t Blue: ");Serial.println(blueSensorValue);/*
In order to use the values from the sensor for the LED, you need to do some
math. The ADC provides a 10-bit number, but analogWrite() uses 8 bits.
You'll want to divide your sensor readings by 4 to keep them in range
of the output.
*/redValue=redSensorValue/4;greenValue=greenSensorValue/4;blueValue=blueSensorValue/4;// print out the mapped values
Serial.print("Mapped sensor Values \t red: ");Serial.print(redValue);Serial.print("\t green: ");Serial.print(greenValue);Serial.print("\t Blue: ");Serial.println(blueValue);/*
Now that you have a usable value, it's time to PWM the LED.
*/analogWrite(redLEDPin,redValue);analogWrite(greenLEDPin,greenValue);analogWrite(blueLEDPin,blueValue);}