Description
One potentiometer cross fades 3 LEDs, red, green and blue. A second potentiometer controls the blinking of the LED with the smallest value (from the first pot).
components Used
- 3 - 220 ohm resistors
- 3 LEDs (red, green, blue)
- wiring
- light bulb for the diffuser
- 2 potentiometers
- arduino board
- breadboard
Code
/*
* Modified code by Clay Shirky <clay.shirky@nyu.edu>for making one potentiometer
* control 3 LEDs, red, grn and blue. Added support for a second potentiometer to
* control the blinking of the LED with the smallest value.
*/
//pot for fading colors
int potPin = 4; // pot to fade the LEDs
int potVal = 0; // store the input from the pot
//pot for blinking
int potPinBlink = 5; // pot to blink the LED
int potValBlink = 0; // store the input from the pot
//LED placement
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;
int DEBUG = 1; // Set to 1 to turn on debugging output
void setup()
{
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(grnPin, OUTPUT);
pinMode(bluPin, OUTPUT);
if (DEBUG) { // If we want to see the pin values for debugging...
Serial.begin(9600); // ...set up the serial ouput in 0004 format
}
}
// Main program
void loop()
{
potVal = analogRead(potPin); // read the potentiometer (Fade) value at the input pin
potValBlink = analogRead(potPinBlink); // read the potentiometer (Blink) value at the input pin
if (potVal < 341) // Lowest third of the potentiometer's range (0-340)
{
potVal = (potVal * 3) / 4; // Normalize to 0-255
redVal = 256 - potVal; // Red from full to off
grnVal = potVal; // Green from off to full
bluVal = 1; // Blue off
analogWrite(redPin, redVal); // Write values to LED pins
analogWrite(grnPin, grnVal);
analogWrite(bluPin, bluVal);
delay(potValBlink);
analogWrite(redPin, 0); // Write values to LED pins
analogWrite(grnPin, grnVal);
analogWrite(bluPin, bluVal);
delay(potValBlink);
}
else if (potVal < 682) // Middle third of potentiometer's range (341-681)
{
potVal = ( (potVal-341) * 3) / 4; // Normalize to 0-255
redVal = 1; // Red off
grnVal = 256 - potVal; // Green from full to off
bluVal = potVal; // Blue from off to full
analogWrite(redPin, redVal); // Write values to LED pins
analogWrite(grnPin, grnVal);
analogWrite(bluPin, bluVal);
delay(potValBlink);
analogWrite(redPin, redVal); // Write values to LED pins
analogWrite(grnPin, grnVal);
analogWrite(bluPin, 0);
delay(potValBlink);
}
else // Upper third of potentiometer"s range (682-1023)
{
potVal = ( (potVal-683) * 3) / 4; // Normalize to 0-255
redVal = potVal; // Red from off to full
grnVal = 1; // Green off
bluVal = 256 - potVal; // Blue from full to off
analogWrite(redPin, redVal); // Write values to LED pins
analogWrite(grnPin, grnVal);
analogWrite(bluPin, bluVal);
delay(potValBlink);
analogWrite(redPin, redVal); // Write values to LED pins
analogWrite(grnPin, 0);
analogWrite(bluPin, bluVal);
delay(potValBlink);
}
//analogWrite(redPin, redVal); // Write values to LED pins
//analogWrite(grnPin, grnVal);
//analogWrite(bluPin, bluVal);
if (DEBUG) { // If we want to read the output
DEBUG += 1; // Increment the DEBUG counter
if (DEBUG > 100) // Print every hundred loops
{
DEBUG = 1; // Reset the counter
// Serial output using 0004-style functions
Serial.print("R:"); // Indicate that output is red value
Serial.print(redVal); // Print red value
Serial.print("\t"); // Print a tab
Serial.print("G:"); // Repeat for grn and blu...
Serial.print(grnVal);
Serial.print("\t");
Serial.print("B:");
Serial.println(bluVal); // println, to end with a carriage return
}
}
}
Images
lab3_1
lab3_2