Week 3 Lab

One pot controls brightness, another pot controls blinking

 

Description: Use one pot to control a LED's brightness and the other to control its blinking speed.

Components Used: LED, Resistors, Potentiometers

Photo: Attached

Source Code

/*
 * one pot dims, the other pot changes the blinking rate
 * modification of the following
 * http://www.arduino.cc/en/Tutorial/AnalogInput
 */
int pot1Pin = 1;   // select the input pin for the potentiometer 1
int pot2Pin = 2;   // 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 = 10;  // select the pin for the LED 2
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(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
}

 

Extra Point: Combination Lock

Description: This is a combination lock that lights up when a user gets two numbers right.

Components Used: LED, Resistors, Potentiometers

Photo: Attached

Source Code:

/*
 * It's a lock that gives you light only when it gets two numbers right.
 */
int pot1Pin = 1;   // select the input pin for the potentiometer 1
int pot2Pin = 2;   // 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 lock1Val = 300; // secret number for lock 1
int lock2Val = 800; // secret number for lock 2
int led1Pin = 9;   // select the pin for the LED 1
int led2Pin = 10;  // select the pin for the LED 2
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
   
    if(abs(pot1Val-lock1Val)<100 && abs(pot2Val-lock2Val)<100)
    {
       analogWrite(led2Pin, 100); // dim LED to value from pot1
       analogWrite(led1Pin, 100);
     }else{
      analogWrite(led2Pin, 0);         // dim LED to completely dark (zero)    
      analogWrite(led1Pin, 0);
     } 
}
Homework1
Extra Point
0
Your rating: None