jobsheet p3 - antarmuka potensiometer

Upload: ivan-arrianto

Post on 03-Jun-2018

234 views

Category:

Documents


1 download

TRANSCRIPT

  • 8/12/2019 Jobsheet P3 - Antarmuka Potensiometer

    1/8

    P3 : ANTARMUKA POTENSIOMETER3 : ANTARMUKA POTENSIOMET ER

    1 Tujuan Prakte k : a. Mengetahui cara menghubungkan (antarmuka) potensiometer ke board mikrokontroler Arduino Uno b. Membuat aplikasi pengontrolan yang menggunakan komponen I/O : potensiometer dan LED

    2 Alat/Komp onen :

    No Perangkat/Komponen Jumlah No Perangkat/Komponen Jumlah

    1 Board Arduino Uno 1 5 LED 82 Kabel USB A-B 1 6 RGB LED 1

    3 Protoboard 1 7 Resistor 220 8

    4 Potensiometer 10k 1 8 Kabel jumper Secukupnya

    3

    Kegiatan Praktek : a. Rangkai dan ujilah sampai berhasil, aplikasi antarmuka potensiometer P3.1 sampai P3.6. b. Rancang, rangkai, dan ujilah satu aplikasi antarmuka yang menggunakan potensio. dan LED (DIY P3.7). c. Buat dan kumpulkan Laporannya.

    Page 1 of 8 | P3 : Antarmuka Potensiometer

  • 8/12/2019 Jobsheet P3 - Antarmuka Potensiometer

    2/8

    P3 1 POT DIMMER 13 1 POT DIMMER 1 P3 2 POT DIMMER 23 2 POT DIMMER 2

    /** PotDimmer v1

    * -------------------- ** Use a potentiometer to adjust the brightness of an PWM'd LED* v1 (changing analog input 0 1023 to analog output 0 255 )

    **/

    int potPin = 0; // select the input pin for the potentiometerint ledPin = 10; // select the pin for the LEDint val = 0; // variable to store the value coming from the pot

    void setup(){

    /** PotDimmer v2

    * -------------------- ** Use a potentiometer to adjust the brightness of an PWM'd LED

    * v2 (changing the range of values w/ constrain & map instruction) **/

    int potPin = 1; // select the input pin for the potentiometerint ledPin = 9; // select the pin for the LEDint val = 0; // variable to store the value coming from the pot

    void setup()

    Page 2 of 8 | P3 : Antarmuka Potensiometer

  • 8/12/2019 Jobsheet P3 - Antarmuka Potensiometer

    3/8

    //Note: We don't need to specifiy potPin as an input, //since it defaults to that when we read it

    //The LED pin needs to be set as an outputpinMode(ledPin, OUTPUT);

    //This is the default value, but we can set it anyways

    analogReference(DEFAULT); //5V Reference on UNO}

    void loop(){

    val = analogRead(potPin); // read the value from the pot val = val/4; // convert from 0-1023 to 0-255

    analogWrite(ledPin, val); // turn the ledPin on}

    { //Note: We don't need to specifiy potPin as an input, //since it defaults to that when we read it

    //The LED pin needs to be set as an outputpinMode(ledPin, OUTPUT);

    //This is the default value, but we can set it anywaysanalogReference(DEFAULT); //5V Reference on UNO}

    void loop(){

    val = analogRead(potPin); // read the value from the pot val = constrain(val, 750, 900); // constrain val from 750 to 900 int ledLevel = map(val, 750, 900, 255, 0); // map it to 255 to 0 analogWrite(ledPin, ledLevel); // turn the ledPin on}

    Page 3 of 8 | P3 : Antarmuka Potensiometer

  • 8/12/2019 Jobsheet P3 - Antarmuka Potensiometer

    4/8

    P3 3 BLINKING RATE3 3 BLINKING RATE P3 4 THRESHOLD3 4 THRESHOLD

    /** Pot sketch* blink an LED at a rate set by the position of a potentiometer*/

    const int potPin =2; // select the input pin for the potentiometerconst int ledPin = 13; // select the pin for the LEDint val = 0; // variable to store the value coming from the sensor

    void setup(){

    pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT}

    // Threshold

    //This will turn on an LED after a thresholdint potPin = 3;int ledPin = 4;

    void setup(){

    //Note: We don't need to specifiy sensePin as an//input, since it defaults to that when we read it

    //The LED pin needs to be set as an outputpinMode(ledPin, OUTPUT);

    Page 4 of 8 | P3 : Antarmuka Potensiometer

  • 8/12/2019 Jobsheet P3 - Antarmuka Potensiometer

    5/8

    void loop() {val = analogRead(potPin); // read the voltage on the potdigitalWrite(ledPin, HIGH); // turn the ledPin ondelay(val); // blink rate set by pot value (in milliseconds)digitalWrite(ledPin, LOW); // turn the ledPin offdelay(val); // turn led off for same period as it was turned on

    }

    }

    void loop(){

    // read the potint val = analogRead(potPin);

    if(val < 800){digitalWrite(ledPin, HIGH);

    }else{

    digitalWrite(ledPin, LOW);}

    }

    Page 5 of 8 | P3 : Antarmuka Potensiometer

  • 8/12/2019 Jobsheet P3 - Antarmuka Potensiometer

    6/8

    P3 5 LED BAR GRAPHP3 5 LED BAR GRAPH P3 6 RGB POT MIXERP3 6 RGB POT MIXER

    /* LED bar graph

    Turns on a series of LEDs based on the value of an analog sensor. This is a simple way to make a bar graph display. Though this graph uses 8 LEDs, you can use any number by changing the LED count and the pins in the array.

    This method can be used to control any series of digital outputs that depends on an analog input.

    The circuit: * LEDs from pins 2 through 9 to ground

    /** RGB Pot Mixer** Code for making one potentiometer control 3 LEDs, RGB,* or one tri-color LED.* The program cross-fades from R to G, G to B, and B to R.** Code assumes you have the LEDs connected in a common-anode

    * with the LED's anode connected to +5V via a resistor and the* cathode connected to Arduino pins 9,10,11.*/

    // INPUT: Potentiometer should be connected to 5V and GNDint potPin = 0; // Potentiometer output

    Page 6 of 8 | P3 : Antarmuka Potensiometer

  • 8/12/2019 Jobsheet P3 - Antarmuka Potensiometer

    7/8

    */

    // these constants won't change:const int analogPin = A0; // the pin that the pot is attached toconst int ledCount = 8; // the number of LEDs in the bar graph

    int ledPins[] = { 2, 3, 4, 5, 6, 7,8,9 }; // an array of LED pin numbers

    void setup() { // loop over the pin array and set them all to output: for (int thisLed = 0; thisLed < ledCount; thisLed++) { pinMode(ledPins[thisLed], OUTPUT);

    }}

    void loop() { // read the potentiometer: int potReading = analogRead(analogPin);

    // map the result to a range from 0 to the number of LEDs: int ledLevel = map(potReading, 0, 1023, 0, ledCount);

    // loop over the LED array: for (int thisLed = 0; thisLed < ledCount; thisLed++) { // if the array element's index is less than ledLevel, // turn the pin for this element on: if (thisLed < ledLevel) { digitalWrite(ledPins[thisLed], HIGH); }

    // turn off all pins higher than the ledLevel: else { digitalWrite(ledPins[thisLed], LOW);

    } }

    int potVal = 0; // Variable to store the input from the potentiometer

    // OUTPUT: Use digital pins 9-11, the PWM pinsint redPin = 11; // Red LED, connected to digital pin 9int grnPin = 10; // Green LED, connected to digital pin 10int bluPin = 9; // Blue LED, connected to digital pin 11

    // Program variablesint redVal = 0; // Variables to store the values to send to the pinsint grnVal = 0;int bluVal = 0;

    int DEBUG = 0; // Set to 1 to turn on debugging output

    void setup(){

    pinMode(redPin, OUTPUT); // sets the pins as outputpinMode(grnPin, OUTPUT);pinMode(bluPin, OUTPUT);

    }

    // Main programvoid loop(){

    potVal = analogRead(potPin); // read the pot value at the inp. pinif (potVal < 341) { // Lowest third of the pot's range (0-340)

    potVal = (potVal * 3) / 4; // Normalize to 0-255redVal = 255 - potVal; // Red from full to offgrnVal = potVal; // Green from off to fullbluVal = 1; // Blue off

    }else if (potVal < 682) { // Middle third of pot's range (341-681)

    potVal = ( (potVal-341) * 3) / 4; // Normalize to 0-255redVal = 1; // Red off

    Page 7 of 8 | P3 : Antarmuka Potensiometer

  • 8/12/2019 Jobsheet P3 - Antarmuka Potensiometer

    8/8

    } grnVal = 255 - potVal; // Green from full to offbluVal = potVal; // Blue from off to full

    }else { // Upper third of potentiometer"s range (682-1023)

    potVal = ( (potVal-683) * 3) / 4; // Normalize to 0-255redVal = potVal; // Red from off to fullgrnVal = 1; // Green off

    bluVal = 255 - potVal; // Blue from full to off}// "255-" is because we have common-anode LEDs

    analogWrite(redPin, 255-redVal); // Write values to LED pinsanalogWrite(grnPin, 255-grnVal);analogWrite(bluPin, 255-bluVal);

    }

    DIY P3 7

    Page 8 of 8 | P3 : Antarmuka Potensiometer