Description
This lab expanded on the three LED circuit we built last week. Instead of using serial communication through the computer to alter the output of the LEDs, I used analog inputs from potentiometers (pots) to change their states. Pots are circuit components that have variable resistance depending on the physical state those components are in (i.e. degree of rotation). I used an Arduino Uno Microcontroller to read pot values through analog inputs and control the output of the LEDs as a function of the pots.
To begin, I soldered wire leads to a pot so that I could use it in my circuit. Once the pot had been properly soldered, I constructed the three LED circuit used in the previous lab, but also impletmented the pot. Specifically, I connected the pot signal wire (middle yellow wire on pot) to the analog input pin A2. I then grounded the pot with its black wire, and connected its red wire to the 5V output of the microcontroller. The remainder of the LED circuit was contructed as previously decribed with 3 LEDs in parallel and with respective 220Ω resistors. I next, ran two sketches separately that were given to us for this lab, which used the potentiometer to control the brightness or blink rate of one of the LEDs. Following this, I expanded the circuit to utilize two pot inputs instead of just one. After soldering another pot, I changed the analog input pins to A0 and A1 and uploaded the supplied 2 pot sketch. This sketch allowed me to control the LED brightness and blink rate.
For the homework, I chose to complete Option 1, which is to control the brightness and blink rate with two pots. This was very easy because we were supplied with a sketch that had this functionality already for just one LED. I edited this sketch so that the pots would control all three LEDs and have attached it with this lab for reference. I also completed the assignment for extra points. This code can be seen below. I wrote a sketch that lets the user manipulate one pot to individually select either of the three LEDs. The second pot controls the brightness of the selected LED. This was accomplished with an If statement that incremented the pot analog input into three levels, each corresponding to a different LED. The second pot was the serial brightness value for the "analogWrite" commands as seen below.
Components
1- Arduino Uno Microcontroller
1- Breadboard
3- 220Ω Resistors
3- Light Emitting Diodes (LEDs) (Red, Green, Blue)
1- USB Cable
1- Apple Laptop Computer (Mac OSX)
1- Soldering Iron
Solder
1- Wire Cutter/Stripper
Spooled Wire in Red, Black, and Yellow
2- Potentiometers
Code
/*Stuart Altman
*Homework Extra Points
* one pot selects the LED, the other pot changes the brightness of the selected LED
*/
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 blue LED
int led2Pin = 11; // select the pin for the red LED
int led3Pin = 10; // select the pin for the green LED
void setup() {
pinMode(led1Pin, OUTPUT); // declare the led1Pin as an OUTPUT
pinMode(led2Pin, OUTPUT); // declare the led2Pin as an OUTPUT
pinMode(led3Pin, OUTPUT); // decalre the led3Pin as an OUTPUT
Serial.begin(9600);
}
void loop () {
int val1 = analogRead(pot1Pin); // read the value from pot 1, between 0 - 1024, for LED selection
int pot11Pin = val1/4; // change range from 1 to 255
int val2 = analogRead(pot2Pin); // read the value from pot 2, between 0 - 1024, for brightness control
int pot22Pin = val2/4; // change range from 1 to 255
if (pot11Pin < 85) {
analogWrite(led1Pin,pot22Pin);
analogWrite(led2Pin,0);
analogWrite(led3Pin,0);
}
else if (pot11Pin<170 && pot11Pin>=85) {
analogWrite(led3Pin,pot22Pin);
analogWrite(led1Pin,0);
analogWrite(led2Pin,0);
}
else {
analogWrite(led2Pin,pot22Pin);
analogWrite(led3Pin,0);
analogWrite(led1Pin,0);
}
}
Images
- Login to post comments