Jumat, 29 Maret 2024

M3 P5


Percobaan V

Arduino Uno I2C

1. Foto Hardware dan Diagram Blok [Kembali]

Hardware :




Diagram Blok:







2. Prosedur Percobaan  [Kembali]

+ Rangkai semua komponen 
+ buat program di aplikasi arduino IDE
+ setelah selesai masukkan program ke arduino 
+ jalankan program pada simulasi dan cobakan dengan modul






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. Video Demo [Kembali]

                                      


5. Kondisi [Kembali]

     Push button melakukan aktivasi dan variasi kondisi yang dilakukan pada Master untuk kemudian mengirimkan perintah menuju slave

6. Video Praktikum [Kembali]

                                             


᭒ HTML↠ klik disini
Listing Program↠ klik disini
Video Percobaan↠ klik disini

 Rangkaian Simulasi



Tidak ada komentar:

Posting Komentar

M3 P5

[KEMBALI KE MENU SEBELUMNYA] DAFTAR ISI 1. Foto Hardware dan Diagram Blok 2. Prosedur Percobaan 3. Rangkaian Simulasi ...