Assignment: Sensing: Potentiometers
Collaborators:
Description
I started with the 2b_OnePotControlsBrightnessOnePotControlsBlinking.txt code where one potentiometer adjusts blinking and one adjusts brightness. I adapted this code so that it worked with all three LEDs. I also created a separate code that uses one potentiometer to light up each color LED based on the rotational position of the potentiometer.
Components Used
- Light Emitting Diodes (LED)--red, green, and blue
- 220 ohm resistors (3)
- Arduino board
- Bread board
- Wires
- 2 potentiometers
Arduino Code
2 Potentiometers, one controls brightness, one controls blinking
/*
* one pot dims, the other pot changes the blinking rate
* modification of the following
* http://www.arduino.cc/en/Tutorial/AnalogInput
*/
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 (red)
int led2Pin = 10; // select the pin for the LED 2 (green)
int led3Pin = 11; // select the pin for the LED 3 (blue)
void setup() {
pinMode(led1Pin, OUTPUT); // declare the led1Pin as an OUTPUT
pinMode(led2Pin, 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 blinking
analogWrite(led1Pin, pot1Val/4); // dim LED 1 to value from pot1
analogWrite(led2Pin, pot1Val/4); // dim LED 2 to value from pot1
analogWrite(led3Pin, pot1Val/4); // dim LED 3 to value from pot1
delay(pot2Val); // stop the program for some time, meaning, LED is on for this time
analogWrite(led1Pin, 0); // dim LED 1 to completely dark (zero)
analogWrite(led2Pin, 0); // dim LED 2 to completely dark (zero)
analogWrite(led3Pin, 0); // dim LED 3 to completely dark (zero)
delay(pot2Val); // stop the program for some time, meaning, LED is OFF for this time
}
Rotational position of one potentiometer controls which LED is lit
/*
* Red, Green, Blue Potentiometer
*
* Control 3 LEDs with 1 potentiometer. Red LED lights up when potentiometer is at its low range;
* green at its middle range; and blue at its high range.
*
*
* 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; // Potentiometer connected to analog pin 0 (Connect power to 5V and ground to analog ground)
// Digital pin settings
int aOut = 9; // Red LED
int bOut = 10; // Blue LED
int cOut = 11; // Green LED
// Values
int aVal = 0; // Variables to store the input from the potentiometer
// Variables for comparing values between loops
int i = 0; // Loop counter
int wait = (1000); // Delay between most recent pot adjustment and output
// FLAGS
int PRINT = 1; // 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);
}
void loop()
{
i += 1; // Count loop
aVal = analogRead(0);
if (aVal < 341) { //if potentionmeter is in its low range, light the Red LED
aVal = analogRead(aIn); // read input pins
analogWrite(aOut, 255); // Send new values to LEDs
analogWrite(bOut, 0);
analogWrite(cOut, 0);
}
if (aVal > 342 && aVal < 682) { //if potentionmeter is in its middle, light the Green LED
aVal = analogRead(aIn);
analogWrite(bOut, 255); // Send new values to LEDs
analogWrite(aOut, 0);
analogWrite(cOut, 0);
}
if (aVal > 681) { //if potentionmeter is in its high range, light the Blue LED
aVal = analogRead(aIn);
analogWrite(cOut, 255); // Send new values to LEDs
analogWrite(aOut, 0);
analogWrite(bOut, 0);
}
}
Photos
2 pots, LEDs off
2 pots, LEDs on
Rotational potentiometer control of LEDs