TUGAS PENDAHULUAN MODUL 3 PERCOBAAN 2 KONDISI 4
- Modul 3 Komunikasi
- Buatlah rangkaian sesuai dengan kondisi yang telah dipilih
- Buat Program pada software Arduino IDE
- Masukkan program yang telah dibuat pada software Arduino IDE sebagai library Arduino
- Jalankan rangkaian, sesuai dengan kondisi yang diinginkan
a. Rangkaian SimulasiPada modul 3 percobaan 2 kondisi 4 ini kita menggunakan dip switch sebagai inputan dan dual seven segment sebagai output. Selanjutnya juga menggunakan 2 buah modul, pertama arduino master sebagai pemberi perintah dan arduino slave sebagai penerima/pengeksekusi perintah. Arduino master terhubung dengan inputan dip switch dan arduino slave terhubung dengan dual seven segment sebagai output. Pada percobaan ini menggunakan komunikasi SPI yang ditandai dengan pin MOSI, MISO, SCLK, SS/CS pada kedua modul saling terhubung.Sesuai dengan kondisi yang diminta, ketika Jika 3 switch aktif maka counting 1-9 pada digit 1 lalu pindah pada digit 2, dimana hal ini sesuai dengan program yang telah kita inputkan pada arduinoif (Slavereceived == 3){for(i=0; i<100; i+=10){setAngka(i);delay(100);}for(i=0; i<10; i++){setAngka(i);delay(100);}Selanjutnya sesuai dengan kondisi kedua yang diminta, 4 switch aktif maka counting 1-9 pada digit 2 lalu pindah pada digit 1, dimana hal ini sesuai dengan program yang telah kita inputkan pada arduinoif (Slavereceived == 4){for(i=0; i<10; i++){setAngka(i);delay(100);}for(i=0; i<100; i+= 10){setAngka(i);delay(100);}Arduino Mastera. Listing Program//Master Arduino#include<SPI.h> //Library for SPIint dip[] = {2,3,4,5,6,7,8,9};int dipvalue[] = {};void setup (){Serial.begin(9600); //Starts Serial Communication at Baud Rate 115200for(int i = 0; i < 8; i++){pinMode(dip[i], INPUT_PULLUP);}SPI.begin(); //Begins the SPI commnuicationSPI.setClockDivider(SPI_CLOCK_DIV8); //Sets clock for SPI communication at 8 (16/8=2Mhz)digitalWrite(SS,HIGH); // Setting SlaveSelect as HIGH (So master doesnt connnect with slave)}void loop(void){byte Mastersend;int sw_on = 0;for(int i = 0; i < 8; i++){dipvalue[i] = digitalRead(dip[i]);sw_on = dipvalue[0] + dipvalue[1] + dipvalue[2] + dipvalue[3] + dipvalue[4] + dipvalue[5] + dipvalue[6] + dipvalue[7];}digitalWrite(SS, LOW); //Starts communication with Slave connected to masterMastersend = sw_on;Serial.println(Mastersend);SPI.transfer(Mastersend); //Send the mastersend value to slave also receives value from slavedelay(1000);}- Arduino Slave
//Slave Arduino:#include<SPI.h>#define a 2#define b 3#define c 4#define d 5#define e 6#define f 7#define g 8#define digit1 A1#define digit2 A2volatile boolean received = false;volatile byte Slavereceived;int satuan, puluhan, i;void setup(){Serial.begin(9600);pinMode(a, OUTPUT);pinMode(b, OUTPUT);pinMode(c, OUTPUT);pinMode(d, OUTPUT);pinMode(e, OUTPUT);pinMode(f, OUTPUT);pinMode(g, OUTPUT);pinMode(digit1, OUTPUT);pinMode(digit2, OUTPUT);SPCR |= _BV(SPE); //Turn on SPI in Slave ModeSPI.attachInterrupt(); //Interuupt ON is set for SPI commnucation}ISR (SPI_STC_vect){ //Inerrrput routine functionSlavereceived = SPDR; // Value received from master if store in variable slavereceivedreceived = true; //Sets received as True}void loop(){Serial.println(Slavereceived);if(received){//Logic to SET LED ON OR OFF depending upon the value recerived from masterif (Slavereceived == 4){for(i=0; i<10; i++){setAngka(i);delay(100);}for(i=0; i<100; i+= 10){setAngka(i);delay(100);}}if (Slavereceived == 3){for(i=0; i<100; i+=10){setAngka(i);delay(100);}for(i=0; i<10; i++){setAngka(i);delay(100);}}delay(1000);}}void angka(int xx){if(xx==0){digitalWrite(a,HIGH);digitalWrite(b,HIGH);digitalWrite(c,HIGH);digitalWrite(d,HIGH);digitalWrite(e,HIGH);digitalWrite(f,HIGH);digitalWrite(g,LOW);}if(xx==1){digitalWrite(a,LOW);digitalWrite(b,HIGH);digitalWrite(c,HIGH);digitalWrite(d,LOW);digitalWrite(e,LOW);digitalWrite(f,LOW);digitalWrite(g,LOW);}if(xx==2){digitalWrite(a,HIGH);digitalWrite(b,HIGH);digitalWrite(c,LOW);digitalWrite(d,HIGH);digitalWrite(e,HIGH);digitalWrite(f,LOW);digitalWrite(g,HIGH);}if(xx==3){digitalWrite(a,HIGH);digitalWrite(b,HIGH);digitalWrite(c,HIGH);digitalWrite(d,HIGH);digitalWrite(e,LOW);digitalWrite(f,LOW);digitalWrite(g,HIGH);}if(xx==4){digitalWrite(a,LOW);digitalWrite(b,HIGH);digitalWrite(c,HIGH);digitalWrite(d,LOW);digitalWrite(e,LOW);digitalWrite(f,HIGH);digitalWrite(g,HIGH);}if(xx==5){digitalWrite(a,HIGH);digitalWrite(b,LOW);digitalWrite(c,HIGH);digitalWrite(d,HIGH);digitalWrite(e,LOW);digitalWrite(f,HIGH);digitalWrite(g,HIGH);}if(xx==6){digitalWrite(a,HIGH);digitalWrite(b,LOW);digitalWrite(c,HIGH);digitalWrite(d,HIGH);digitalWrite(e,HIGH);digitalWrite(f,HIGH);digitalWrite(g,HIGH);}if(xx==7){digitalWrite(a,HIGH);digitalWrite(b,HIGH);digitalWrite(c,HIGH);digitalWrite(d,LOW);digitalWrite(e,LOW);digitalWrite(f,LOW);digitalWrite(g,LOW);}if(xx==8){digitalWrite(a,HIGH);digitalWrite(b,HIGH);digitalWrite(c,HIGH);digitalWrite(d,HIGH);digitalWrite(e,HIGH);digitalWrite(f,HIGH);digitalWrite(g,HIGH);}if(xx==9){digitalWrite(a,HIGH);digitalWrite(b,HIGH);digitalWrite(c,HIGH);digitalWrite(d,HIGH);digitalWrite(e,LOW);digitalWrite(f,HIGH);digitalWrite(g,HIGH);}}void setAngka(int xxx) {if (xxx < 10) {puluhan = 0;satuan = xxx;} else if (xxx < 100) {puluhan = xxx / 10;satuan = xxx % 10;}digitalWrite(digit1, LOW);digitalWrite(digit2, HIGH);angka(puluhan);delay(50);digitalWrite(digit1, HIGH);digitalWrite(digit2, LOW);angka(satuan);delay(50);}- b. Flow Chart
Arduino Master- Arduino Slave
Jika 3 switch aktif maka counting 1-9 pada digit 1 lalu pada digit 2, jika 4 switch aktif maka counting 1-9 pada digit 2 lalu pada digit 1.Download HTML klik disiniDownload Rangkaian Proteus klik disiniDownload Program Arduino Master klik disiniDownload Program Arduino Slave klik disini- Download Video klik disini
- Download Datasheet 7 segment klik disini
- Download Datasheet Arduino klik disini
Komentar
Posting Komentar