Description
Controls RGB values with analog input from one potentiometer, and controls the brightness with analog input from another potentiometer.
Components Used
red LED
blue LED
green LED
(3) 220 resistors
(2) potentiometers
Arduino Code
//*
* Adapted from code by:
* Clay Shirky <clay.shirky@nyu.edu>
* Found at:
* http://www.arduino.cc/en/Tutorial/LEDCross-fadesWithPotentiometer
*/
// INPUT: Potentiometer should be connected to 5V and GND
int pot1Pin = 0; // select the input pin for the potentiometer 1
int pot2Pin = 5; // select the input pin for the potentiometer 2
float pot1Val = 0; // variable to store the value coming from pot 1
float pot2Val = 0; // variable to store the value coming from pot 2
// OUTPUT: Use digital pins 9-11, the Pulse-width Modulation (PWM) pins
// LED's cathodes should be connected to digital GND
int redPin = 9; // Red LED, connected to digital pin 9
int grnPin = 10; // Green LED, connected to digital pin 10
int bluPin = 11; // Blue LED, connected to digital pin 11
// Program variables
int redVal = 0; // Variables to store the values to send to the pins
int grnVal = 0;
int bluVal = 0;
void setup()
{
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(grnPin, OUTPUT);
pinMode(bluPin, OUTPUT);
}
// Main program
void loop()
{
pot1Val = analogRead(pot1Pin); // read the value of pot 1 (for brightness), between 0 - 1024
pot1Val = (pot1Val/1024); // turn the value of pot 1 into a decimal
pot2Val = analogRead(pot2Pin); // read the value of pot 2 (for crossfades), between 0 - 1024
if (pot2Val < 341) // Lowest third of the potentiometer's range (0-340)
{
pot2Val = ((pot2Val * 3) / 4); // Normalize to 0-255
redVal = (256 - pot2Val) * pot1Val; // Red from full to off
grnVal = pot2Val * pot1Val; // Green from off to full
bluVal = 0; // Blue off
}
else if (pot2Val < 682) // Middle third of potentiometer's range (341-681)
{
pot2Val = ( (pot2Val-341) * 3) / 4; // Normalize to 0-255
redVal = 0; // Red off
grnVal = (256 - pot2Val) * pot1Val; // Green from full to off
bluVal = pot2Val * pot1Val; // Blue from off to full
}
else // Upper third of potentiometer"s range (682-1023)
{
pot2Val = ( (pot2Val-683) * 3) / 4; // Normalize to 0-255
redVal = pot2Val * pot1Val; // Red from off to full
grnVal = 0; // Green off
bluVal = (256 - pot2Val) * pot1Val; // Blue from full to off
}
analogWrite(redPin, redVal); // Write values to LED pins
analogWrite(grnPin, grnVal);
analogWrite(bluPin, bluVal);
}
Image
Alana's Cloud Base (Light)
Alana's Cloud Base (Dark)
Comments
looks good, alana!
looks good, alana!