1. Kondisi [kembali]
Rangkaialah 5 buah led, kemudian buatlah kelimanya menyala bergantian dengan interval berbanding terbalik dengan nilai potensiometer.
2. Rangkaian Simulasi dan Prinsip Kerja [kembali]
3. Flowchart [kembali]
4. Listing Program [kembali]
Program Master
#include <Wire.h>
#define SLAVE_ADDR 9
int analogPin = A0;
int val = 0;
void setup() {
Wire.begin();
}
void loop() {
delay(50);
val = map(analogRead(analogPin), 0, 1000, 255, 1);
Wire.beginTransmission(SLAVE_ADDR);
Wire.write(val);
Wire.endTransmission();
}
Program Slave
#include <Wire.h>
#define SLAVE_ADDR 9
int LED1 = 13;
int LED2 = 12;
int LED3 = 11;
int LED4 = 10;
int LED5 = 9;
int rd;
int br1, br2, br3, br4, br5;
void setup() {
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, 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);
br1 = map(rd, 1,255, 1000, 900);
br2 = map(rd, 1,255, 800, 700);
br3 = map(rd, 1,255, 600, 500);
br4 = map(rd, 1,255, 400, 300);
br5 = map(rd, 1,255, 200, 100);;
digitalWrite(LED1, HIGH);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
delay(br1);
digitalWrite(LED1, LOW);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
delay(br2);
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
delay(br3);
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, HIGH);
digitalWrite(LED5, LOW);
delay(br4);
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(LED5, HIGH);
delay(br5);
}
5. Video [kembali]
6. Download File [kembali]
Tidak ada komentar:
Posting Komentar