TUGAS PENDAHULUAN 2 M3 up&uc
1. Prosedur [Kembali]
- Rangkai komponen sesuai percobaan dan kondisi yang dipilih
- Buat program menggunakan Arduino IDE.
- Transfer program yang telah dibuat ke Arduino yang ada di Proteus.
- Uji coba program di simulasi sesuai dengan kondisi yang diinginkan.
- Proses selesai setelah uji coba berhasil dilakukan
2. Hardware dan Diagram Blok [Kembali]
Komunikasi antara dua Arduino dalam rangkaian ini menggunakan protokol I2C. Proses dimulai ketika Arduino master menginisiasi transmisi dengan mengirimkan sinyal start, kemudian mengirimkan alamat slave yang dituju dan operasi yang akan dilakukan. Setelah itu, Arduino slave memberikan respons sesuai permintaan. Data dikirimkan secara serial dan sinkron dengan sinyal clock, dan proses komunikasi diakhiri dengan Arduino master mengirimkan sinyal stop. Pada sisi slave, LED akan menyala berdasarkan nilai tombol yang ditekan, dengan nilai tombol bawah mempengaruhi LED jika genap dan nilai tombol atas mempengaruhi LED jika ganjil. Nilai tombol ditampilkan pada layar virtual terminal dengan angka 0-3, di mana angka kanan menunjukkan nilai tombol bawah dan angka kiri menunjukkan nilai tombol atas.
4. Flowchart dan Listing Program [Kembali]
Flowcharth
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);
}
listing program 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() {
}
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]
Saat Praktikum
6. Kondisi [Kembali]
Percobaan 5 tanpa kondisi
7. Video Simulasi [Kembali]
8. Link Download [Kembali]
Download HTML [Klik di sini]
Download file rangkaian [Klik di sini]
Download video rangkaian [Klik di sini]
Download program Arduino [Klik di sini]
Download datasheet Arduino [Klik di sini]
Komentar
Posting Komentar