Gambar 1 Resistor
Gambar 2. Potensiometer
Gambar 3. LED
Gambar 4. Arduino Uno
//MASTER
#include <Wire.h>
#define SLAVE_ADDR 9
int analogPin = 0;
int val = 0;
void setup() {
Wire.begin();
}
void loop() {
delay(50);
val = map(analogRead(analogPin), 0, 1023, 255, 1);
Wire.beginTransmission(SLAVE_ADDR);
Wire.write(val);
Wire.endTransmission();
}
#include <Wire.h>
#define SLAVE_ADDR 9
int analogPin = 0;
int val = 0;
void setup() {
Wire.begin();
}
void loop() {
delay(50);
val = map(analogRead(analogPin), 0, 1023, 255, 1);
Wire.beginTransmission(SLAVE_ADDR);
Wire.write(val);
Wire.endTransmission();
}
//SLAVE
#include <Wire.h>
#define SLAVE_ADDR 9
int LED = 13;
int rd;
int br;
void setup() {
pinMode(LED, OUTPUT);
Wire.begin(SLAVE_ADDR);
Wire.onReceive(receiveEvent);
Serial.begin(9600);
Serial.println("I2C Slave demo");
}
void receiveEvent(){
rd = Wire.read();
Serial.println(rd);
}
void loop() {
delay(50);
br = map(rd, 1, 255, 100, 2000);
digitalWrite(LED, HIGH);
delay(br);
digitalWrite(LED, LOW);
delay(br);
}
#include <Wire.h>
#define SLAVE_ADDR 9
int LED = 13;
int rd;
int br;
void setup() {
pinMode(LED, OUTPUT);
Wire.begin(SLAVE_ADDR);
Wire.onReceive(receiveEvent);
Serial.begin(9600);
Serial.println("I2C Slave demo");
}
void receiveEvent(){
rd = Wire.read();
Serial.println(rd);
}
void loop() {
delay(50);
br = map(rd, 1, 255, 100, 2000);
digitalWrite(LED, HIGH);
delay(br);
digitalWrite(LED, LOW);
delay(br);
}
#include <Wire.h>
#define SLAVE_ADDR 9
int LED = 13;
int rd;
int br;
void setup() {
pinMode(LED, OUTPUT);
Wire.begin(SLAVE_ADDR);
Wire.onReceive(receiveEvent);
Serial.begin(9600);
Serial.println("I2C Slave demo");
}
void receiveEvent(){
rd = Wire.read();
Serial.println(rd);
}
void loop() {
delay(50);
br = map(rd, 1, 255, 100, 2000);
digitalWrite(LED, HIGH);
delay(br);
digitalWrite(LED, LOW);
delay(br);
}
ANALISA PERCOBAAN 3 [I2C]:
1.
Analisa dengan mengubah rangkaian menjadi mengatur intensitas cahaya LED menggunakan potensiometer
Jawab :
Jika kita merubah fungsi rangkainnya menjadi mengatur intensitas cahaya pada LED dengan potensiometer maka kita juga harus mengganti listing programnya juga jadi pada prinsipnya nanti jika potensio semakin besar atau resistor semagin kecil maka lampu LED akan menjadi lebih terang dan sebaliknya jika potensio kecil atau resistansinya besar maka lampu LED akan menjadi lebih redup
//MASTER
#include <Wire.h>
#define SLAVE_ADDR 9
int analogPin = 0;
int val = 0;
void setup() {
Wire.begin();
}
void loop() {
val = analogRead(analogPin);
val = map(analogPin, 0, 1023 , 0 , 255);
Wire.beginTransmission(SLAVE_ADDR);
Wire.write(val);
Wire.endTransmission();
}
//SLAVE
#include <Wire.h>
#define SLAVE_ADDR 9
int LED = 13;
int rd;
int br;
void setup() {
pinMode(LED, OUTPUT);
Wire.begin(SLAVE_ADDR);
Wire.onReceive(receiveEvent);
Serial.begin(9600);
Serial.println("I2C Slave demo");
}
void receiveEvent(){
rd = Wire.read();
Serial.println(rd);
}
void loop() {
delay(50);
br = map(rd, 1, 255 , 100 , 2000);
digitalWrite(LED,br);
}
3. Rangkaian dan Listing Program download
4. Data Sheet potensiometer - download
5. Datasheet arduino - download
6. Library arduino - download
No comments:
Post a Comment