1. Rangkai semua komponen sesuai percobaan.
4. Flowchart dan listing program
[Kembali]
5. Kondisi
[Kembali]
6. Video demo [Kembali]
2. Buat program di aplikasi arduino IDE.
3. Setelah selesai masukkan program ke arduino.
4. Jalankan program pada rangkaian dan cobakan sesuai dengan modul.
5. Selesai.
2. Hardware dan diagram blok
[Kembali]
3. Rangkaian simulasi dan prinsip kerja
[Kembali]
Rangkaian Simulasi
Prinsip Kerja
Pada rangkaian percobaan 5 modul 3 terdapat 2 arduino uno, 2 Push button kuning dan hijau dan LED merah. Arduino yang dihubungkan ke push button di deklarasikan programnya sebagai master (pengontrol) dan Arduino yang dihubungkan ke LED di deklarasikan programnya sebagai slave (dikontrol). Prinsip kerjanya , ketika rangkaian di dijalnkan push button yang di program sebagai master akan mengontrol output dari LED yang diprogram sebagai slave, ketika push button hijau di tekan sebanyak 3 kali maka push button kuning dalam keadaan aktif dimana outptnya berupa LED hidup saat push button kuning ditekan sekali, LED hidup mati saat push button kuning ditekan ke dua kalinya, LED mati saat push button kuning ditekan ke tiga kalinya. Ketika Push button hijau ditekan sebanyak 3 kali lagi maka push button kuning akan non aktif.
Flowchart
Listing Program
Master:
//Master
#include <Wire.h>
#define SLAVE_ADDRESS 9 // Slave Arduino address
#define BUTTON1 2 // Pin for the push button
#define BUTTON2 3
int buttonState1 = 0;
int buttonState2 = 0;
int buttonPrevState1 = 0;
int buttonPrevState2 = 0;
unsigned int counter = 0;
unsigned int count = 0;
void setup() {
Wire.begin(); // Initialize I2C communication
pinMode(BUTTON1, INPUT_PULLUP); // Set button pin as input with internal pull-up resistor
pinMode(BUTTON2, INPUT_PULLUP);
Serial.begin(9600); // Initialize serial communication for debugging
}
void loop() {
buttonState1 = digitalRead(BUTTON1);
buttonState2 = digitalRead(BUTTON2);
if (buttonState2 != buttonPrevState2){
if(buttonState2 == LOW){
count++;
}
delay(50);
}
buttonPrevState2 = buttonState2;
if(count > 3){
count = 0;
}
if(count % 2 == 0){
if (buttonState1 != buttonPrevState1) {
if (buttonState1 == LOW) {
// Button is pressed
counter++;
Wire.beginTransmission(SLAVE_ADDRESS);
Wire.write(counter); // Send command to the slave
Wire.endTransmission();
}
delay(50); // Debouncing delay
}
buttonPrevState1 = buttonState1;
if(counter > 3){
counter = 0;
}
}else if(count % 3 == 0){
Wire.endTransmission();
}
Serial.print(count); Serial.println(counter);
}
Slave:
//Slave
#include <Wire.h>
#define LED_COUNT 8
#define LED_PIN_START 2 // Start pin for the LEDs
void setup() {
Serial.begin(9600);
Wire.begin(9); // Initialize I2C communication as Slave with address 9
Wire.onReceive(receiveEvent); // Register event for receiving data
for (int i = LED_PIN_START; i < LED_PIN_START + LED_COUNT; i++) {
pinMode(i, OUTPUT); // Set LED pins as output
}
}
void loop() {
// Nothing to do here, all actions are performed in the receiveEvent function
}
void receiveEvent(int numBytes) {
unsigned int command = Wire.read(); // Read incoming command from master
Serial.println(command);
delay(500);
if (command == 1) {
// Turn all LEDs ON
for (int i = LED_PIN_START; i < LED_PIN_START + LED_COUNT; i++) {
digitalWrite(i, HIGH);
}
} else if (command == 2) {
// Turn all LEDs OFF
for (int i = LED_PIN_START; i < LED_PIN_START + LED_COUNT; i++) {
digitalWrite(i, LOW);
}
} else if (command == 3) {
// Blink all LEDs
for (int j = 0; j < 5; j++) { // Repeat the blinking 5 times
for (int i = LED_PIN_START; i < LED_PIN_START + LED_COUNT; i++) {
digitalWrite(i, HIGH);
delay(1000);
}
delay(500); // Delay for ON state
for (int i = LED_PIN_START; i < LED_PIN_START + LED_COUNT; i++) {
digitalWrite(i, LOW);
delay(1000);
}
delay(500); // Delay for OFF state
}
}
}
Modul 3 Percobaan 5 Komunikasi I2C
7. Link Download
[Kembali]
Download HTML klik disini
Download Simulasi Rangkaian klik disini
Download Library Arduino Uno klik disini
Donwload Datasheet Arduino Uno klik disini
Donwload Datasheet Push Button klik disini
Donwload Datasheet LED klik disini
0 komentar:
Posting Komentar