Regular code (2a_Control3LEDsWith3Pots.txt)
/*
* "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 = 11; // LEDs connected to digital pins 9, 10 and 11
int bOut = 10; // (Connect cathodes to digital ground)
int cOut = 9;
// Values
int aVal = 0; // Variables to store the input from the potentiometers
int bVal = 0;
int cVal = 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
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) / 4; // read input pins, convert to 0-255 scale
bVal = analogRead(bIn) / 4;
cVal = analogRead(cIn) / 4;
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.
prevCheckSum = checkSum; // Update the values
}
}
Another solution:
/*
* "Coffee-cup" Color Mixer:
* Code for mixing and reporting PWM-mediated color
* Assumes Arduino 0004 or higher, as it uses Serial.begin()-style communication
*
* 3 Potentiometers
* a) controls base level of the LEDs intensity
* b) controls frequency of level adjustement
* c) controls period of blinks
*
* 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 = 11; // LEDs connected to digital pins 9, 10 and 11
int bOut = 10; // (Connect cathodes to digital ground)
int cOut = 9;
// Values
float level = 0; // Variables to store the input from the potentiometers
// Variables for comparing values between loops
int s_counter=0; // switch counter
float mult = 0; // sinusoid multiplier
long wait = (100); // Delay between most recent pot adjustment and output
int i;
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++;
if (wait == 0) {
level = analogRead(aIn) / 4; // read input pins, convert to 0-255 scale
mult = abs(sin(3.1415*float(analogRead(bIn))/255.0*(s_counter%1000)/1000.));
s_counter++;
int aVal = (s_counter%3 == 0)?level*mult:0;
int bVal = (s_counter%3 == 1)?level*mult:0;
int cVal = (s_counter%3 == 2)?level*mult:0;
analogWrite(aOut, aVal); // Send new values to LEDs
analogWrite(bOut, bVal);
analogWrite(cOut, cVal);
wait = long(analogRead(cIn)/4)*200+500; // delay between blinks
} else {
wait--;
}
}