Lab 3: Sensing with Potentiometers

Posted by raghavchandra

raghavchandra's picture

Description:

The objective of this lab is to use a potentiometers (pots) as sensing devices. For this lab, we use potentiometers as inputs to the Arduino micro-controller to control LEDs, specifically their blinking and brightness. In this process, we explore the input features of our Arduino boards. We also learn how to solder wires.

Components Used:
  • 3 LEDs(Red, Blue, Green)
  • Arduino Micro-controller
  • 3 Resistors (220 Ohms)
  • Wires/Bread Board
  • Solder Iron and Wire
  • 3 Potentiometers ('pots')
  • 'Helping Hands' (Clamps to hold the wires and pot in place while soldering)

Arduino Code: (Part of HW-Extra Credit)

Depending on the amount we rotate the nob of the potentiometer, two colors at a time are mixed. This creates a whole spectrum of colors by mixing green/blue, blue/red, and red/green. It also creates white light by mixing all three together.

(attached images give a few of the colors. Colors not very visible as the camera could not capture the glow)

Can use three pots; one for mixing color, one for increasing/decreasing the brightness and one for blinking. Using all three and playing around is a visual treat and creates a good light show!

/*
 * -- (EXTRA CREDIT) --
 * ### Using the ROTATION of a pot to create effect ###
 * Depending on how much we rotate a pot, we can control which
 * LED lights up. i.e. the rotation will rotate the brightness 
 * among the pots.
 * Example: 
 *       0%: LED 1 HIGH only
 *       0% to 25%: LED 1 dims while LED 2 brightens.
 *       25%: LED 2 HIGH only.
 *       25% to 50%: LED 2 dims while LED 3 brightens.
 *       50%: LED 3 HIGH only
 *       50% to 75%: LED 3 dims, while LED 1 brightens.
 *       after 75%: all LEDs have same brightness. (75% is HIGH, 100% is LOW)
 *
 * If we add 2nd and 3rd pot, one pot dims, the other pot changes the blinking rate
 *
 * modification of the following
 * http://www.arduino.cc/en/Tutorial/AnalogInput
 */
 
/* UNCOMMENT if using additional pots(1,2) for blinking and dimming.
int pot1Pin = 0;   // select the input pin for the potentiometer 1
int pot2Pin = 1;   // select the input pin for the potentiometer 2
*/
int potRotPin = 2; // select the input pin for the potentiometer 3
 
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 potRotVal = 0; // variable to store the value coming from pot 3
 
int led1Pin = 9;   // select the pin for the LED 1
int led2Pin = 10;  // select the pin for the LED 2
int led3Pin = 11;  // select the pin for the LED 3
 
// values for each led to store their brightness depending on the amount
// the 'rotational effect' pot has been turned.
int led1Val = 0;
int led2Val = 0;
int led3Val = 0;
 
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() {
  /* UNCOMMENT if using additional pots (1,2) for blinking and dimming
  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
  */
  
   //0(only 1), 255(only 2), 510(only 3), 765(all)
   potRotVal = analogRead(potRotPin);  // read the value from pot 2, between 0 - 1024, for 'Rotational effect'
   
      // 0% to 25%
   if (potRotVal >= 0 && potRotVal < 255) {
led1Val = 255 - potRotVal;
led2Val = 255 - led1Val;
led3Val = 0;
   }  // 25% to 50%
   else if (potRotVal >= 255 && potRotVal < 510) {
led2Val = 255 - (potRotVal-255);
led3Val = 255 - led2Val;
led1Val = 0;
   }  // 50% to 75%
   else if (potRotVal >= 510 && potRotVal < 765) {
led3Val = 255 - (potRotVal-510);
led1Val = 255 - led3Val;
led2Val = 0;
   }  // 75% to 100%
   else if (potRotVal >= 765) {
    led1Val = -255 + (potRotVal-765);
    led2Val = -255 + (potRotVal-765);
    led3Val = -255 + (potRotVal-765);
   }
   
   // -pot1Val/4 is for dimming using another pot.
   analogWrite(led1Pin, led1Val - pot1Val/4); // dim LED 1 to value from pot1 and 3
   analogWrite(led2Pin, led2Val - pot1Val/4); // dim LED 2 to value from pot1 and 3
   analogWrite(led3Pin, led3Val - pot1Val/4); // dim LED 3 to value from pot1 and 3
 
   // Below code is for Blinking using another pot.
   /* UNCOMMENT if using a pot(pot 2) for Blinking.
   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
   */
}
White
Red
Pink/Purple
Green
Blue
5
Your rating: None Average: 5 (1 vote)
Tags: