Sei sulla pagina 1di 8

P3 : ANTARMUKA POTENSIOMETER

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

Alat/Komp onen : No Perangkat/Komp onen 1 2 3 4 Board Arduino Uno Kabel USB A-B Protoboard Potensiometer 10k Jumlah 1 1 1 1 No 5 6 7 8 Perangkat/Komp onen LED RGB LED Resistor 220 Kabel jumper Jumlah 8 1 8 Secukupnya

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

P3.1 POT DIMMER 1

P3.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 potentiometer int ledPin = 10; // select the pin for the LED int 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 potentiometer int ledPin = 9; // select the pin for the LED int val = 0; // variable to store the value coming from the pot void setup()

Page 2 of 8 | P3 : Antarmuka Potensiometer

//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 output pinMode(ledPin, OUTPUT); //This is the default value, but we can set it anyways analogReference(DEFAULT); //5V Reference on UNO

{ //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 output pinMode(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 = 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 }

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 }

Page 3 of 8 | P3 : Antarmuka Potensiometer

P3.3 BLINKING RATE

P3.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 potentiometer const int ledPin = 13; // select the pin for the LED int 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 threshold int 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 output pinMode(ledPin, OUTPUT);

Page 4 of 8 | P3 : Antarmuka Potensiometer

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

} void loop() { // read the pot int val = analogRead(potPin); if(val < 800) { digitalWrite(ledPin, HIGH); } else { digitalWrite(ledPin, LOW); } }

Page 5 of 8 | P3 : Antarmuka Potensiometer

P3.5 LED BAR GRAPH

P3.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 GND int potPin = 0; // Potentiometer output

Page 6 of 8 | P3 : Antarmuka Potensiometer

*/ // these constants won't change: const int analogPin = A0; // the pin that the pot is attached to const 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 pins int redPin = 11; // Red LED, connected to digital pin 9 int grnPin = 10; // Green LED, connected to digital pin 10 int bluPin = 9; // Blue LED, connected to digital pin 11 // Program variables int redVal = 0; // Variables to store the values to send to the pins int 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 output pinMode(grnPin, OUTPUT); pinMode(bluPin, OUTPUT); } // Main program void loop() { potVal = analogRead(potPin); // read the pot value at the inp. pin if (potVal < 341) { // Lowest third of the pot's range (0-340) potVal = (potVal * 3) / 4; // Normalize to 0-255 redVal = 255 - potVal; // Red from full to off grnVal = potVal; // Green from off to full bluVal = 1; // Blue off } else if (potVal < 682) { // Middle third of pot's range (341-681) potVal = ( (potVal-341) * 3) / 4; // Normalize to 0-255 redVal = 1; // Red off

Page 7 of 8 | P3 : Antarmuka Potensiometer

} else { // Upper third of potentiometer"s range (682-1023) potVal = ( (potVal-683) * 3) / 4; // Normalize to 0-255 redVal = potVal; // Red from off to full grnVal = 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 pins analogWrite(grnPin, 255-grnVal); analogWrite(bluPin, 255-bluVal); }

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

DIY P3.7
...

Page 8 of 8 | P3 : Antarmuka Potensiometer

Potrebbero piacerti anche