To start, I connected 3 potentiometers to read input from Analog pins 0-2. Initially, I used the provided "Coffee-cup Color Mixer" code to test. The prewritten code enabled me to adjust the brightness of each LED individually.
I then modified the code to change the role of each potentiometer, so that one potentiometer controlled the rate of blinking and another controlled the brightness of all 3 LEDs. The third potentiometer adjusted which LED of the 3 was the one blinking.
/*
* Code adapted from "Coffee-cup" Color Mixer:
* Code for mixing and reporting PWM-mediated color
* Assumes Arduino 0004 or higher, as it uses Serial.begin()-style communication
*/
// 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;
// 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 = true; // 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)*2; // Controls rate of blinking
bVal = analogRead(bIn)/4; //Controls overall brightness of all 3 LEDs
cVal = analogRead(cIn); // Controls which LED is blinking
if (cVal < 255){
analogWrite(aOut, 255); // Only Red
delay(aVal);
analogWrite(aOut, bVal/16);
analogWrite(bOut, bVal/8);
analogWrite(cOut, bVal/8);
}
else if (cVal < 509){ //
analogWrite(bOut, 255);
delay(aVal);
analogWrite(bOut, bVal/16);
analogWrite(aOut, bVal/8);
analogWrite(cOut, bVal/8);
}
else{
analogWrite(cOut, 255);
delay(aVal);
analogWrite(cOut, bVal/16);
analogWrite(aOut, bVal/8);
analogWrite(bOut, bVal/8);
}
Serial.print(aVal/1000);
Serial.print('\n');
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