Lab 3 : Sensing Part 1

Posted by apoorvas

apoorvas's picture

 

Objective
The objective of this lab is to explore analog input using potentiometers (pots).  We use potentiometers as analog inputs to the Arduino micro-controller and use them to control the brightness and blinking of the LEDs. We also learnt 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)
 
Traffic light implementation of the potentiometer. (HW Extra credit)
Depending on the amount we rotate the nob of the potentiometer, either the signal shows the color red, or green,yellow or white. So it is a mapping of the potentiometer on leds to be used as traffic light.
Can use three pots; one for mixing color, one for increasing/decreasing the brightness and one for blinking. 
/*
 * -- (EXTRA CREDIT) --
 * ### Using the ROTATION of a pot to create traffic light 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% - 25% :  Red light
 *       25% to 50%:  Yellow
 *       50% to 75% :  White ( represent pedestrian crossing)
*        75% to 100% : Green 
 * 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
 */
 
int potRotPin = 0;   // select the input pin for the potentiometer 1
/*UNCOMMENT if using additional pots(1,2) for blinking and dimming.
 
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 potRotVal = 0; // variable to store the value coming from pot Rotate
 
 
int led1Pin = 9;   // select the pin for the LED 1 - red
int led2Pin = 10;  // select the pin for the LED 2 - green 
int led3Pin = 11;  // select the pin for the LED 3 - blue
 
// 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(Red), 255(Yellow), 510(white), 765(green)
   potRotVal = analogRead(potRotPin);  // read the value from pot 0, between 0 - 1024, for 'Rotational effect'
   
      // 0% to 25%
   if (potRotVal >= 0 && potRotVal < 255) {
led1Val = 255;
led2Val = 0;
led3Val = 0;
   }  // 25% to 50%
   else if (potRotVal >= 255 && potRotVal < 510) {
led1Val = 255;
led2Val = 240;
led3Val = 0;
   }  // 50% to 75%
   else if (potRotVal >= 510 && potRotVal < 765) {
led3Val = 255;
led1Val = 255;
led2Val = 255;
   }  // 75% to 100%
   else if (potRotVal >= 765) {
    led1Val = 0;
    led2Val = 255;
    led3Val = 0;
   }
    analogWrite(led1Pin, led1Val); // 
   analogWrite(led2Pin, led2Val); // 
   analogWrite(led3Pin, led3Val); //
 
 /* UNCOMMENT if using a pot(pot 2) for Blinking.
   // -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
   */
}

 

Yellow -> slow down
White -> Crossing
Red Traffic light
Green Traffic light
0
Your rating: None
Tags: