Assignment: Sensing: Potentiometers
Collaborators:
POT in pin 1 controls the brightness of all three LEDs while POT in pin 2 controls the color. The color of the three LEDs together fades from Blue at 0 through Green-Yellow-Orange-Red-Violet and back to Blue at 1023. There are 8 phases of color change, similar to the LED Fade example from Assignment 1, where for each phase, only one LED is changing it's brightness and the other two are on or off.
I'm sure there is a more elegant way to do this using an equation rather than a series of if-then statements, but my feeble calculus skills could not handle the task. If I was ever going to implement by solution in a more rigorous application, say for a retail product, I would hire myself a mathematician to write me my equation.
<!--EndFragment-->
The Code
/*
* One pot dims, the other changes the color of three LEDs
* Modification of the following: http://www.arduino.cc/en/Tutorial/AnalogInput
* ...and code by Clay Shirky <clay.shirky@nyu.edu>
* Modifications by Jessica Voytek, 2009
*/
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 led1Pin = 9; // select the pin for the LED 1
int led2Pin = 10; // select the pin for the LED 2
int led3Pin = 11; // select the pin for the LED 2
int redVal = 0; // Variables to store the values to send to the pins
int greenVal = 0; // Initial values are Blue full, Red and Green off
int blueVal = 255;
int wait = 50; // 50ms (.05 second) delay shorten for faster fades
int DEBUG = 1; // DEBUG counter; if set to 1, will write values back via serial
void setup() {
Serial.begin(9600); // set up the serial ouput on 0004 style
pinMode(led1Pin, OUTPUT); // declare the led1Pin as an OUTPUT
pinMode(led2Pin, OUTPUT); // declare the led2Pin as an OUTPUT
pinMode(led3Pin, OUTPUT); // declare the led2Pin as an 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 color
float luminocity = float(pot1Val)/1024; // read the value from pot 1 and calculate the percentage from 0-1024
float i = float(pot2Val); // make a float out of the pot2Value and assign it to i
float percent = 0.00; // initiate a variable which will hold a float value for the percentage on
int value = 0; // initiate a variable which will explicitly cast the percet to an integer
// calculate the percent-on based on the following 8-part pattern
// repeated below for each part
if (i < 127) {
percent = (i/127) * 255;
} else if (i < 255) {
percent = (1 - ((i - 127)/128)) * 255; // for this one we want the inverse
} else if (i < 383) {
percent = ((i - 255)/128) * 255;
} else if (i < 511) {
percent = (1 - ((i - 383)/128)) * 255; // for this one we want the inverse
} else if (i < 639) {
percent = ((i - 511)/128) * 255;
} else if (i < 767) {
percent = (1 - ((i - 639)/128)) * 255; // for this one we want the inverse
} else if (i < 895) {
percent = ((i - 767)/128) * 255;
} else {
percent = (1 - ((i - 895)/128)) * 255; // for this one we want the inverse
}
value = int(percent);
int redValue;
int greenValue;
int blueValue;
// First part, red comes up
if (i < 127) {
redValue = value;
greenValue = 0;
blueValue = 255;
// Second part, blue goes down
} else if (i < 255) {
redValue = 255;
blueValue = value;
greenValue = 0;
// Third part, green comes up
} else if (i < 383) {
redValue = 255;
blueValue = 0;
greenValue = value;
// Fourth part red goes down
} else if (i < 511) {
redValue = value;
blueValue = 0;
greenValue = 255;
// Fifth part blue comes up
} else if (i < 639) {
redValue = 0;
blueValue = value;
greenValue = 255;
// Sixth part green goes down
} else if (i < 767) {
redValue = 0;
blueValue = 255;
greenValue = value;
// Seventh part red comes up
} else if (i < 895) {
redValue = value;
blueValue = 255;
greenValue = 0;
// Eigth part red goes down
} else {
redValue = value;
blueValue = 255;
greenValue = 0;
}
// Get the final values of each by multiplying by the luminocity
redValue = redValue * luminocity;
greenValue = greenValue * luminocity;
blueValue = blueValue * luminocity;
analogWrite(led1Pin, redValue);
analogWrite(led2Pin, greenValue);
analogWrite(led3Pin, blueValue);
if (DEBUG) { // If we want to read the output
DEBUG += 1; // Increment the DEBUG counter
if (DEBUG > 5) {
DEBUG = 1; // Reset the counter
Serial.print(value); // Print a tab
Serial.print(" "); // Print a tab
Serial.print(percent); // Print a tab
Serial.print("\t"); // Print a tab
Serial.print("R:"); // Indicate that output is red value
Serial.print(redValue); // Print red value
Serial.print("\t"); // Print a tab
Serial.print("G:"); // Repeat for green and blue...
Serial.print(greenValue);
Serial.print("\t");
Serial.print("B:");
Serial.println(blueValue); // println, to end with a carriage return
}
}
delay(wait); // Pause for 'wait' milliseconds before resuming the loop
}
Images