Description:
I created the 3-pot LED control circuit and used the given code in the lab for controlling the brightness of each LED with each pot.
As an optional part, I created what I call a "Rainbow Maker" by using one pot to control the 3 LEDs so that it fades from red to green to blue to red.
Components used:
-
red LED
-
green LED
-
blue LED
-
3 potentiometers
-
3 220 ohm resistors
Code:
/*
* Rainbow Maker
* Jennifer Wang
* September 14, 2011
*
* Control 3 LEDs with 1 potentiometer, blending colors in a rainbow from red
* to orange to yellow to green to blue to indigo to violet back to red
*
*/
int potPin = 2; // select the input pin for the potentiometer
int redPin = 9; // Red LED, connected to digital pin 9
int greenPin = 10; // Green LED, connected to digital pin 10
int bluePin = 11; // Blue LED, connected to digital pin 11
int val = 0; // variable to store the value coming from the sensor
int redVal = 0; // calculated value of red LED brightness
int greenVal = 0; // calculated value of green LED brightness
int blueVal = 0; // calculated value of blue LED brightness
void setup() {
Serial.begin(9600);
}
void loop() {
val = analogRead(potPin); // read the value from the sensor, between 0 - 1024
Serial.println(val);
// NOTE: coefficients of linear equations to determine LED brightness are rounded,
// so border values are explicitly set
// 1024/7 colors (ROYGBIV) = 146
// red when val == 0 (red LED only)
// orange when val == 146
// yellow when val == 292
// green when val == 438 (green LED only)
// blue when val == 584
// indigo when val == 730 (blue LED only)
// violet when val == 876
// red when val == 1024
// from red to green
if (val >= 0 & val < 438) {
redVal = 255 - 255/438.0*val;
greenVal = 255/438.0*val;
blueVal = 0;
}
// green
else if (val == 438) {
redVal = 0;
greenVal = 255;
blueVal = 0;
}
// from green to indigo
else if (val > 438 & val < 730) {
redVal = 0;
greenVal = 637 - 255/292.0*val;
blueVal = -382 + 255/292.0*val;
}
// blue indigo
else if (val == 730) {
redVal = 0;
greenVal = 0;
blueVal = 255;
}
// from indigo to red
else if (val > 730 & val < 1024) {
redVal = -633 + 255/294.0*val;
greenVal = 0;
blueVal = 888 - 255/294.0*val;
}
// back to red
else if (val == 1024) {
redVal = 255;
greenVal = 0;
blueVal = 0;
}
analogWrite(redPin, redVal);
analogWrite(greenPin, greenVal);
analogWrite(bluePin, blueVal);
Serial.println();
Serial.println(redVal);
Serial.println(greenVal);
Serial.println(blueVal);
Serial.println();
}