Tugas Pendahuluan 1


Percobaan V

Komunikasi I2C

1. Prosedur [Kembali]

  • Rangkailah seperti rangkaian berikut
  • Buka Arduino IDE dan masukan listing program
  • Upload program ke arduino
  • Jalankan dan amati keluarannya

2. Hardware dan Diagram Blok [Kembali]

    1. Arduino Uno

    2. Button


    3. Virtual Terminal


    4. LED

    4. Ground


Diagram Blok:




Gambar 1. Rangkaian Simulasi.

Prinsip Kerja :

        Pada saat Button ditekan, yang kemudian arduino uno yang berperan sebagai Master akan mengirim perintah ke arduino uno yang berperan sebagai Slave melalui I2C. Slave menerima perintah dan bertindak menyalakan LED


4. Flowchart dan Listing Program [Kembali]

Flowchart:


Listing Program:


//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
#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
 }
 }
}


5. Kondisi [Kembali]

    Percobaan tanpa kondisi.

᭒ HTML↠ klik disini
Rangkaian Simulasi ↠ klik disini
᭒ Gambar Simulasi ↠ klik disini
᭒ Video Simulasi ↠ klik disini
᭒ Listing Program ↠ klik disini

















Tidak ada komentar:

Posting Komentar

Unggulan

Aplikasi Encoder Decoder

APLIKASI ROLLING TOKO OTOMATIS [KEMBALI KE MENU SEBELUMNYA] DAFTAR ISI 1. Tujuan 2. Alat dan Bahan 3. Dasar Teori 4. Percobaan ...