Description
In this lab we learned about soldering and potentiometers. We used soldering irons to solder wires onto potentiometers and then added the pots to our 3 LED circuit from lab 2. Pots are variable resistors that allow us to control voltage by turning their knobs. The Arduino has analog to digital converter inputs to detect the voltage changes.
I modified the provided code which used one pot to change the blinking rate of the LEDs and one pot to change the brightness. The following code uses the pot connected to analog input A0 to turn on first just the red LED, then just the green LED, then just the blue LED, and finally all 3. Turning this potentiometer clockwise cycles the lights in this order and also gradually increases the brightness of each. The second pot connected to A1 adjusts the blinking rate of the LEDs. Turning this pot clockwise, decreases the blinking rate.
Components Used
1- Arduino UNO
1- Breadboard
3- 220 ohm resistors
3- LEDS (blue, red, green)
17- wires
2- potentiometers
Code
/*
* One pot changes the LED from red to green to blue to all colors at once, while gradually
* increasing the brightness of each with clockwise rotation. The second pot changes the LED blinking rate.
* modification of:
* http://www.arduino.cc/en/Tutorial/AnalogInput
* Sydney Mayes
* Lab 3, Fall 2013
*/
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 ledRed = 9; // select the pin for the red LED
int ledGreen = 10; // select the pin for the green LED
int ledBlue = 11; // select the pin for the blue LED
void setup() {
pinMode(ledRed, OUTPUT); // declare the red LED as an OUTPUT
pinMode(ledGreen, OUTPUT); // declare the green LED as an OUTPUT
pinMode(ledBlue, OUTPUT); // declare the blue LED as an OUTPUT
Serial.begin(9600);
}
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
Serial.print("Pot 1 is ");
Serial.print(pot1Val);
Serial.println();
Serial.print("Pot 2 is ");
Serial.print(pot2Val);
Serial.println();
if (pot1Val<=255) {
analogWrite(ledRed, pot1Val);
delay(pot2Val); // keep red LED on for the amount of time specified by pot2Val
analogWrite(ledRed, 0);
analogWrite(ledGreen, 0);
analogWrite(ledBlue, 0);
delay(pot2Val); // turn off red LED for the amount of time specifiec by pot2Val
}
else if (pot1Val<=510) {
analogWrite(ledGreen, pot1Val);
delay(pot2Val); // keep green LED on for the amount of time specified by pot2Val
analogWrite(ledRed, 0);
analogWrite(ledGreen, 0);
analogWrite(ledBlue, 0);
delay(pot2Val); // turn off green LED for the amount of time specifiec by pot2Val
}
else if (pot1Val<=765) {
analogWrite(ledBlue, pot1Val);
delay(pot2Val); // keep blue LED on for the amount of time specified by pot2Val
analogWrite(ledRed, 0);
analogWrite(ledGreen, 0);
analogWrite(ledBlue, 0);
delay(pot2Val); // turn off blue LED for the amount of time specifiec by pot2Val
}
else { // turns all LEDs on and off with increasing brightness when pot1Val is greater than 765
analogWrite(ledRed, pot1Val);
analogWrite(ledGreen, pot1Val);
analogWrite(ledBlue, pot1Val);
delay(pot2Val);
analogWrite(ledRed, 0);
analogWrite(ledGreen, 0);
analogWrite(ledBlue, 0);
delay(pot2Val);
}
}
- Login to post comments