The LEDs are controlled by three different pots. The first pot controls the color displayed and transitions through the colors of the rainbow (roygbiv - red orange yellow green blue indigo violet). The second pot controls the blinking rate. The third pot (acts as a binary switch) turns the blinking on or off ('cause it gets annoying).
I wanted to have the colors progress from one color of the rainbow to the next but I was having problems with the translation and couldn't get it to work correctly, so for now it just instantly goes from red to orange rather than progressively (which would be ideal).
/*
* "Coffee-cup" Color Mixer:
* Code for mixing and reporting PWM-mediated color
* Assumes Arduino 0004 or higher, as it uses Serial.begin()-style communication
*
* The LEDs are controlled by three different pots.
* The first pot controls the color displayed and transitions through the colors of
* the rainbow (roygbiv - red orange yellow green blue indigo violet).
* The second pot controls the blinking rate.
* The third pot (acts as a binary switch) turns the blinking on or off
* ('cause it gets annoying).
*/
// 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;
int pot1Val = 0;
int pot2Val = 0;
int pot3Val = 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(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
pot1Val = analogRead(aIn) / 4; // read input pins, convert to 0-255 scale
pot2Val = analogRead(bIn) / 4;
pot3Val = analogRead(cIn) / 4;
// use frst pot (left-most) to go through colors of the rainbow (roygbiv)
// red
if (pot1Val <= 36) {
aVal = 255; //red 255
bVal = 0; //green 0
cVal = 0; //blue 0
}
// orange
else if ((pot1Val > 36) && (pot1Val <= 72)) {
aVal = 255; //red 255
bVal = 165; //green 165
cVal = 0; //blue 0
}
// yellow
else if ((pot1Val > 72) && (pot1Val <= 108)) {
aVal = 255; //red 255
bVal = 255; //green 255
cVal = 0; //blue 0
}
// green
else if ((pot1Val > 108) && (pot1Val <= 144)) {
aVal = 0; //red 0
bVal = 255; //green 255
cVal = 0; //blue 0
}
// blue
else if ((pot1Val > 144) && (pot1Val <= 180)) {
aVal = 0; //red 0
bVal = 0; //green 0
cVal = 255; //blue 255
}
// indigo
else if ((pot1Val > 180) && (pot1Val <= 216)) {
aVal = 75; //red 75
bVal = 0; //green 0
cVal = 130; //blue 130
}
// violet
else if ((pot1Val > 216) && (pot1Val <= 255)) {
aVal = 238; //red 238
bVal = 130; //green 130
cVal = 238; //blue 238
}
// use second pot (from the left) to control blinking speed of all 3 leds at once
// use third pot (from left) to turn blinking on and off
if (pot3Val >= 128) {
delay(pot2Val); // stop the program for some time, meaning, LED is on for this time
analogWrite(aOut, 0); // dim LED to completely dark (zero)
analogWrite(bOut, 0); // dim LED to completely dark (zero)
analogWrite(cOut, 0); // dim LED to completely dark (zero)
delay(pot2Val); // stop the program for some time, meaning, LED is OFF for this time
}
analogWrite(aOut, aVal); // Send new values to LEDs
analogWrite(bOut, bVal);
analogWrite(cOut, cVal);
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);
}
}
}
Comments
nice work! it is a bit
nice work! it is a bit tricky to go from a continuous pot value to an rgb value. You'd need some sort of function to do that for you. But overall, excellent job with the design and code!