Assignment: Sensing: Potentiometers
Collaborators:
Components Used
- Light Emitting Diodes (LED)--red, green, and blue
- 220 ohm resistors (3)
- Potentiometers (3)
- Arduino board
- Bread board
- Wires
Description:
For part three of this assignment, I chose to wire three potentiometers to each control a different LED. You can see my circuit and the code controlling these below.
I also wrote a novel use for the potentiometer - I re-envisioned it as a color wheel, and treated twisting the dial as moving around the perimeter of the wheel. In practice, this meant twisting the dial caused one color to fade out while another color faded in. The complete cycle (starting from the right) composes a transition from red to blue to green. With a diffuser over the top, the result is a smooth transition between colors e.g. at the beginning red gradually changes to purple which gradualle changes to blue.
Code:
Part 3 (three pots controls three LEDs):
/*
* three pots fade three LEDs independently
* by Ian McDowell
* modified version of AnalogInput
* by DojoDave <http://www.0j0.org>
* http://www.arduino.cc/en/Tutorial/AnalogInput
*/
int potPin = 2; // select the input pin for the potentiometer
int ledPin = 9; // select the pin for the LED
int ledPin2 = 10; // select the second pin
int ledPin3 = 11; // select the third pin
int val = 0; // variable to store the value coming from the potentiometer
int val2 = 0; // variable for pot 2
int val3 = 0; // variable for pot3
void setup() {
Serial.begin(9600);
}
void loop() {
val = analogRead(potPin); // read the value from the first pot, between 0 - 1024
val2 = analogRead(potPin2); // repeat for second pot
val3 = analogRead(potPin3); // repeat for third pot
Serial.println(val);
analogWrite(ledPin, val/4); // analogWrite can be between 0-255
Serial.println(val2);
analogWrite(ledPin2, val2/4); // analogWrite can be between 0-255
Serial.println(val3);
analogWrite(ledPin3, val3/4);
}
Bonus: Use one pot to fade between colors:
/*
* use one pot to fade between colors
*/
int potPin = 2; // set the input pin for the potentiometer
int ledPin = 9; // set the pins for the LEDs
int ledPin2 = 10;
int ledPin3 = 11;
int val = 0; // initialize variable to store the value coming from the sensor
int red = 0; //initilaize variables to store the different color values
int blue = 0;
int green = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
/* we're mapping values to a color wheel
0 is full red
170 is half red, half blue
341 is full blue
511 is half blue, half green
682 is full green
852 is half green, half red
A note on the math:
For the purpose of this assignment, I've divided the sensor input into thirds, each of size 341 (technically 342 for the first and last third)
Dividing 255 (an LED range) by 341 (the the span of a sensor range) we get a multiplier we can use against the pot value to normalize output based on the range
*/
val = analogRead(potPin); // read the value from the sensor, between 0 - 1024
if (val < 341) { //we're in the first third of the sensor range
red = 255 - val*(255.0/341); //red's total brightness is based on the distance from zero (further from zero is less bright)
blue = val*(255.0/341); //blue's total brightness is also based on the distance from zero (further form zero is more bright)
green = 0; //each step only covers a fade between two colors (the third color is off)
}
else if (val < 682) { // we're in the middle third of the sensor range
red = 0; //red is off now
blue = 255 - (val-341)*(255.0/341); //blue gets fainter as we get away from the bottom of this range
green = (val-341)*(255.0/341);//green gets brighter as we get away from the bottom of this range.
}
else { //we're in the last third of the sensor range
red = (255.0/341)*(val-682); // red grows as it gets further from bottom fo this range
blue = 0; // blue is off
green = 255 - (255.0/341)*(val-682); //green fades
}
Serial.println(red); //print the values to serial to read this
Serial.println(blue);
Serial.println(green);
analogWrite(ledPin, red); // write the red value
analogWrite(ledPin2, blue); // write the blue value
analogWrite(ledPin3, green); // write the green value
}
Circuit Images
Closeup of circuit
Zoomed out view of circuit and pots