Description
I like the the all three lights dimming in a pattern we did in the previous lab assignment so I decided to add that feature to my project this week. So I use the first potentiometer, aIn, to switch between four modes, the light dimming, solid red, solid blue and solid green. In the light dimming mode, second potentiometer, bIn, is in charge of adding blinks, and third potentiometer, cIn, changes how fast the color changes. In the other three modes, bIn also controls blinking and cIn changes brightness. For all the occascaions where delay function is used, I used an if clause to let values less a certain amount make no blinking at all.
Components Used
1- Arduino Pro Uno
3- 22 Ω Resistor
1- Breadboard
3- LED Light
3- Potentiometer
Code
/*
* "Coffee-cup" Color Mixer:
* Code for mixing and reporting PWM-mediated color
* Assumes Arduino 0004 or higher, as it uses Serial.begin()-style communication
*
* 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 aIn = 0; // Potentiometers connected to analog pins 0, 1, and 2
int bIn = 1; // (Connect power to 5V and ground to analog ground)
int cIn = 2;
// Digital pin settings
int aOut = 9; // LEDs connected to digital pins 9, 10 and 11
int bOut = 10; // (Connect cathodes to digital ground)
int cOut = 11;
// Values
int aVal = 0; // Variables to store the input from the potentiometers
int bVal = 0;
int cVal = 0;
// Brightness
int redVal = 255; // Variables to store the values to send to the pins
int greenVal = 1; // Initial values are Red full, Green and Blue off
int blueVal = 1;
// 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(aOut, OUTPUT); // sets the digital pins as output
pinMode(bOut, OUTPUT);
pinMode(cOut, OUTPUT);
Serial.begin(9600); // Open serial communication for reporting
}
void loop()
{
i += 1; // Count loop
aVal = analogRead(aIn) / 256;
bVal = analogRead(bIn) / 10;
cVal = analogRead(cIn) / 25;
if (aVal == 0) {
i += 1; // Increment counter
if (i < 255) // First phase of fades
{
redVal -= 1; // Red down
greenVal += 1; // Green up
blueVal = 1; // Blue low
}
else if (i < 509) // Second phase of fades
{
redVal = 1; // Red low
greenVal -= 1; // Green down
blueVal += 1; // Blue up
}
else if (i < 763) // Third phase of fades
{
redVal += 1; // Red up
greenVal = 1; // Green low
blueVal -= 1; // Blue down
}
else // Re-set the counter, and start the fades again
{
i = 1;
}
analogWrite(bOut, redVal); // Write current values to LED pins
analogWrite(cOut, greenVal);
analogWrite(aOut, blueVal);
if (cVal >= 1) {
delay(cVal);
}
else {
delay(1);
}
if (bVal >= 20) {
delay (bVal);
analogWrite(bOut, 0);
analogWrite(cOut, 0);
analogWrite(aOut, 0);
delay (bVal);
}
}
if (aVal == 1) {
analogWrite(aOut, cVal);
analogWrite(bOut, 0);
analogWrite(cOut, 0);
if (bVal >= 20) {
delay(bVal);
analogWrite(aOut, 0);
delay(bVal);
}
}
if (aVal == 2) {
analogWrite(aOut, 0);
analogWrite(bOut, cVal);
analogWrite(cOut, 0);
if (bVal >= 20) {
delay(bVal);
analogWrite(bOut, 0);
delay(bVal);
}
}
if (aVal == 3) {
analogWrite(aOut, 0);
analogWrite(bOut, 0);
analogWrite(cOut, cVal);
if (bVal >= 20) {
delay(bVal);
analogWrite(cOut, 0);
delay(bVal);
}
}
if (i % wait == 0) // If enough time has passed...
{
checkSum = aVal+bVal+cVal; // ...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("A: "); // ...then print the values.
Serial.print(aVal);
Serial.print("\t");
Serial.print("B: ");
Serial.print(bVal);
Serial.print("\t");
Serial.print("C: ");
Serial.println(cVal);
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