Approach
For this assignment we are given multiple pots which we can use to adjust the brightness or the LED and the their blinking speed. Starting from the given code "2b_OnePotControlsBrightnessOnePotControlsBlinking.txt", I used it as a template to mix different settings and give different color, for example, in order to light up the red LED, the value of the first pot has to be larger than 301, while the second pot has to be smaller or equal than 300. I have set 3 different conditions for the 3 colors. This kind of setting is commonly seen in control systems, for example, a functional machine usually has a green light on, but when there is an error it will give a yellow light, and if it has to be sent for maintainence it will give a red light.
Materials
1. Arduino UNO
2. Solderless breadboard,Digikey #23273-ND
3. Blue LED,Jameco #183222
4. Green LED, Jameco #334473
5. Red LED, Jameco #33481
6. 220 ohm resistor, Jameco #107941 x 3
7. USB cable, Jameco #222607
8. Potentiometers x 2
Code
int pot1Pin = 0; // select the input pin for the potentiometer 1
int pot2Pin = 5; // 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 = 11; // select the pin for the LED 2
int led3Pin = 10; // select the pin for the LED 3
void setup() {
pinMode(led1Pin, OUTPUT); // declare the led1Pin as an OUTPUT
pinMode(led2Pin, OUTPUT); // declare the led2Pin as an OUTPUT
pinMode(led3Pin, OUTPUT); // declare the led3Pin 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
if (pot1Val <=300 && pot2Val <=300) { // condition 1 to turn on led2pin
analogWrite(led2Pin, pot1Val/4); // dim LED to value from pot1
delay(pot2Val); // stop the program for some time, meaning, LED is on for this time
analogWrite(led2Pin, 0); // dim LED to completely dark (zero)
delay(pot2Val); } // stop the program for some time, meaning, LED is OFF for this time}
else if (pot1Val > 301 && pot2Val <=300) { // condition 2 to turn on led1pin
analogWrite(led1Pin, pot1Val/4);
delay(pot2Val);
analogWrite(led1Pin, 0);
delay(pot2Val);
}
else if (pot1Val > 301 && pot2Val > 301) { // condition 3 to turn on led3pin
analogWrite(led3Pin, pot1Val/4);
delay(pot2Val);
analogWrite(led3Pin, 0);
delay(pot2Val);
}
}
- Login to post comments