Descriptoin
After testing a single pot fading and blinking, I tested controlling brightness/blinking LED through 2 potentiometers.(opt1) I also checked color mixing through manipulating the potentiomenters.(opt2)
Components
3 LED, 3 Resistors, 2 Potentiometers, Arduino Board, Breadboard, Wires
Arduino Code
opt1)
int pot1Pin = 0; // select the input pin for the potentiometer 1
int pot2Pin = 1; // select the input pin for the potentiometer 2
int pot1Val = 0; // variable to store the value coming from pot 1
int pot2Val = 0; // variable to store the value coming from pot 2
int redPin = 9; // the pin for the red LED
int greenPin = 10; // the pin for the green LED
int bluePin = 11; // the pin for the blue LED
void setup() {
pinMode(redPin, OUTPUT); // declare the led1Pin as an OUTPUT
pinMode(greenPin, OUTPUT); // declare the led2Pin as an OUTPUT
pinMode(bluePin, OUTPUT); //
}
void loop() {
pot1Val = analogRead(pot1Pin); // read the value from pot 1, between 0 - 1024, for dimming
pot2Val = analogRead(pot2Pin); // read the value from pot 2, between 0 - 1024, for blinking
analogWrite(redPin, pot1Val/4); // dim LED to value from pot1
analogWrite(greenPin, pot1Val/4);
analogWrite(bluePin, pot1Val/4);
delay(pot2Val); // stop the program for some time, meaning, LED is on for this time
analogWrite(redPin, 0); // dim LED to completely dark (zero)
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
delay(pot2Val); // stop the program for some time, meaning, LED is OFF for this time
}
opt2)
int aIn = 1; // the inputpin for potentiometer 1
int bIn = 2; // the inputpin for potentiometer 2
int rOut = 9; // goes to Red LED
int gOut = 10; // goes to green LED
int aVal = 0; // Variables to store the input from the potentiometer1
int bVal = 0; // Variables to store the input from the potentiometer2
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 redLED
pinMode(gOut, OUTPUT); // sets the digital pins as greenLED
Serial.begin(9600); // Open serial communication for reporting
}
void loop()
{
i += 1; // Count loop
aVal = analogRead(aIn) / 4; // read input pin1, convert to 0-255 scale
bVal = analogRead(bIn) / 4; // read input pin2, convert to 0-255 scale
analogWrite(rOut, aVal); // Send new values to RedLED
analogWrite(gOut, bVal); // Send nte vlauese to BlueLED
if (i % wait == 0) // If enough time has passed...
{
checkSum = aVal+bVal; // ...add up the 2 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");
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);
}
}
}