Senin, 29 April 2024

TP1M3


Percobaan 2 Kondisi 4
SPI-DIPSwitch dan Seven Segment

1. Prosedur [Kembali]

  • Rangkailah semua komponen 
  • Buat program di aplikasi arduino IDE
  • Setelah selesai, masukkan program ke arduino 
  • Jalankan program pada simulasi dan lakukan sesuai kondisi

  • Rangkaian


  • Prinsip Kerja

      Rangkaian diatas adalah salah satu contoh dari komunikasi antara arduino 1 atau arduino master dan arduino 2 yang mmerupakan arduino slave.

    Untuk percobaan kalli ini yaitu arduino 1(master) akan menerima input dari dipswitch 1, lalu input akan di proses oleh arduino master, Lalu komunikasi antar arduino master dan slave akan terjadi, arduino mengirimkan data pada arduino 2 atau slave yang akan memproses data dariarduino master lalu akan di tampilkan oleh seven segment.

  • Flowchart
    Master
    Slave


  • Listing Program
Master

//Master Arduino
#include<SPI.h> //Library for SPI 
int dip[] = {2,3,4,5,6,7,8,9};
int dipvalue[] = {};
void setup (){
 Serial.begin(9600); //Starts Serial Communication at Baud Rate 115200 
 for(int i = 0; i < 8; i++){
 pinMode(dip[i], INPUT_PULLUP);
 }
 SPI.begin(); //Begins the SPI commnuication
 SPI.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 master
 Mastersend = sw_on;
 Serial.println(Mastersend); 
 SPI.transfer(Mastersend); //Send the mastersend value to slave also receives value from slave
 delay(1000);
}

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 A2
volatile 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 Mode
 SPI.attachInterrupt(); //Interuupt ON is set for SPI commnucation
}
ISR (SPI_STC_vect){ //Inerrrput routine function
 Slavereceived = SPDR; // Value received from master if store in variable slavereceived
 received = 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 master 
  if (Slavereceived == 3){
    for(i=0; i<10; i++){
      setAngka(i);
      delay(100);
    }
    for(i=0; i<100; i+= 10){
      setAngka(i);
      delay(100);
    }
  }
   if (Slavereceived == 4){
    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);
}

4. Kondisi [Kembali]

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

HTML klik disini  
Rangkaian Simulasi  klik disini
Video Simulasi   klik disini
Listing Program klik disini














Tidak ada komentar:

Posting Komentar

MODUL 4

    [KEMBALI KE MENU SEBELUMNYA] DAFTAR ISI 1. Pendahuluan 2. Tujuan 3. Alat dan Bahan 4. Dasar Teori 5. Percobaan Per...