Lab 3 :: Color Mixing with Potentiometers
Description
Three potentiometers are mapped independently to three LEDs to control their brightness (for mixing) and blinking rate. Fastest blinking occurs when a single LED is on, at its lowest brightness. Slowest blinking occurs when all three LEDs are on, at their maximum brightness.
Components Used
1- Arduino Uno
1- Breadboard
3- 220 Ω Resistor
3- Potentiometers
1- Green LED
1- Blue LED
1- Red LED
Cotton
Code
/*
* Each pot is mapped to one LED to control its brightness and blinking rate.
* Modification of the following:
* http://www.arduino.cc/en/Tutorial/AnalogInput
* Adapted by Ajeeta Dhole on 2/17/2013.
*/
int greenPotPin = 2; // select the input pin for the potentiometer controlling the green LED
int bluePotPin = 1; // select the input pin for the potentiometer controlling the blue LED
int redPotPin = 0; // select the input pin for the potentiometer controlling the red LED
int greenVal = 0; // variable to store the value coming from pot controlling the green LED
int blueVal = 0; // variable to store the value coming from pot controlling the blue LED
int redVal = 0; // variable to store the value coming from pot controlling the red LED
int greenLEDPin = 9; // select the pin for the green LED
int blueLEDPin = 10; // select the pin for the blue LED
int redLEDPin = 11; // select the pin for the red LED
void setup() {
pinMode(greenLEDPin, OUTPUT); // declare the greenLEDPin as an OUTPUT
pinMode(blueLEDPin, OUTPUT); // declare the blueLEDPin as an OUTPUT
pinMode(redLEDPin, OUTPUT); // declare the redLEDPin as an OUTPUT
}
void loop() {
greenVal = (analogRead(greenPotPin)/4)*2/3; // read from pot controlling the green LED, and convert to 255 scale. scale brightness down to 2/3 of original to (approximately) match brightness of red LED.
blueVal = (analogRead(bluePotPin)/4)*1/3; // read from pot controlling the blue LED, and convert to 255 scale. scale brightness down to 1/3 of original to (approximately) match brightness of red LED.
redVal = analogRead(redPotPin)/4; // read from pot controlling the red LED, and convert to 255 scale
analogWrite(blueLEDPin, blueVal); // dim LED proportional to the value from pot controlling the blue LED
analogWrite(redLEDPin, redVal); // dim LED proportional to the value from pot controlling the red LED
analogWrite(greenLEDPin, greenVal); // dim LED proportional to the value from pot controlling the green LED
// delay is cumulative sum of values from the three pots.
// fastest blinking occurs when a single LED is on at its lowest brightness.
// slowest blinking occurs when all three LEDs are on at their maximum brightness.
delay(blueVal); // stop the program for some time
delay(redVal); // stop the program for some time
delay(greenVal); // stop the program for some time
digitalWrite(blueLEDPin, 0); // turn the blueLEDPin off
digitalWrite(redLEDPin, 0); // turn the redLEDPin off
digitalWrite(greenLEDPin, 0); // turn the greenLEDPin off
delay(blueVal); // stop the program for some time
delay(redVal); // stop the program for some time
delay(greenVal); // stop the program for some time
}
- Login to post comments