Lab3: Potentiometers (3LEDs & 3POTs)
Description
I made diffusers to mix RGB colors by covering LED lights with transparent plastic tube, as well as a tiny glass. I use 3 independent potentiometers to 3 RGB LEDs to adjust the brightness of each color manually.
Components
1. Arduino UNO
2. Breadboard
3. Red/Red/Brown/Gold Resistor x 3
4. Green/Red/Blue LEDs
5. 3 Potentiometers
6. Transparent Tube x 3
7. Plastic Cub
8. Glass Cube
Code
/*
* Control 3 LEDs with 3 potentiometers
* If the LEDs are different colors, and are directed at diffusing surface (stuck in a
* a Ping-Pong ball, or placed in a paper coffee cup with a cut-out bottom and
* a white plastic lid), the colors will mix together.
*
* When you mix a color you like, stop adjusting the pots.
* The mix values that create that color will be reported via serial out.
*
* Standard colors for light mixing are Red, Green, and Blue, though you can mix
* with any three colors; Red + Blue + White would let you mix shades of red,
* blue, and purple (though no yellow, orange, green, or blue-green.)
*
* Put 220 Ohm resistors in line with pots, to prevent circuit from
* grounding out when the pots are at zero
*/
// Analog pin settings
int RIn = 2; // Potentiometers connected to analog pins 0, 1, and 2
int GIn = 3; // (Connect power to 5V and ground to analog ground)
int BIn = 4;
// Digital pin settings
int ROut = 9; // LEDs connected to digital pins 9, 10 and 11
int GOut = 10; // (Connect cathodes to digital ground)
int BOut = 11;
// Values
int RVal = 0; // Variables to store the input from the potentiometers
int GVal = 0;
int BVal = 0;
// Variables for comparing values between loops
int i = 0; // Loop counter
int wait = (1000); // Delay between most recent pot adjustment and output
int checkSum = 0; // Aggregate pot values
int prevCheckSum = 0;
int sens = 3; // Sensitivity theshold, to prevent small changes in
// pot values from triggering false reporting
// FLAGS
int PRINT = 1; // Set to 1 to output values
int DEBUG = 1; // Set to 1 to turn on debugging output
void setup()
{
pinMode(ROut, OUTPUT); // sets the digital pins as output
pinMode(GOut, OUTPUT);
pinMode(BOut, OUTPUT);
Serial.begin(9600); // Open serial communication for reporting
}
void loop()
{
i += 1; // Count loop
RVal = map(analogRead(RIn), 0, 1023, 0, 255); // / 2; // read input pins, convert to 0-255 scale
GVal = map(analogRead(GIn), 0, 1023, 0, 255); // / 3;
BVal = map(analogRead(BIn), 0, 1023, 0, 255); // 4;
analogWrite(ROut, RVal); // Send new values to LEDs
analogWrite(GOut, GVal);
analogWrite(BOut, BVal);
if (i % wait == 0) // If enough time has passed...
{
checkSum = RVal+GVal+BVal; // ...add up the 3 values.
if ( abs(checkSum - prevCheckSum) > sens ) // If old and new values differ
// above sensitivity threshold
{
if (PRINT) // ...and if the PRINT flag is set...
{
Serial.print("R: "); // ...then print the values.
Serial.print(RVal);
Serial.print("\t");
Serial.print("G: ");
Serial.print(GVal);
Serial.print("\t");
Serial.print("B: ");
Serial.println(BVal);
PRINT = 0;
}
}
else
{
PRINT = 1; // Re-set the flag
}
prevCheckSum = checkSum; // Update the values
if (DEBUG) // If we want debugging output as well...
{
Serial.print(checkSum);
Serial.print("<=>");
Serial.print(prevCheckSum);
Serial.print("\tPrint: ");
Serial.println(PRINT);
}
}
}
- Login to post comments
Drupal theme by Kiwi Themes.