PERCOBAAN 2 KONDISI 11
Ganti LED menjadi Buzzer, Buatlah kondisi awal Buzzer mati, lalu hidup dengan delay 200 ms
Rangkaian Simulasi [Kembali]
Video
[Kembali]
Prinsip Kerja [Kembali]
list code:
MASTER:
#include<SPI.h> //Library for SPI
#define buzzer 7
#define ipbutton 2
int buttonvalue;
int x;
void setup ()
{
Serial.begin(115200); //Starts Serial Communication at Baud Rate 115200
pinMode(ipbutton,INPUT); //Sets pin 2 as input
pinMode(buzzer,OUTPUT); //Sets pin 7 as Output
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()
{
byte Mastersend,Mastereceive;
buttonvalue = digitalRead(ipbutton); //Reads the status of the pin 2
if(buttonvalue == HIGH) //Logic for Setting x value (To be sent to slave) depending upon input from pin 2
{
x = 1;
}
else
{
x = 0;
}
digitalWrite(SS, LOW); //Starts communication with Slave connected to master
Mastersend = x;
Mastereceive=SPI.transfer(Mastersend); //Send the mastersend value to slave also receives value from slave
if(Mastereceive == 1) //Logic for setting the buzzer output depending upon value received from slave
{
digitalWrite(buzzer,HIGH);
delay(200);
digitalWrite(buzzer,LOW);
//Sets pin 7 HIGH
Serial.println("Master buzzer ON");
}
else
{
digitalWrite(buzzer,LOW); //Sets pin 7 LOW
Serial.println("Master buzzer OFF");
}
delay(500);
}
SLAVE:
#include<SPI.h>
#define buzzerPin 7
#define buttonpin 2
volatile boolean received;
volatile byte Slavereceived,Slavesend;
int buttonvalue;
int x;
void setup()
{
Serial.begin(115200);
pinMode(buttonpin,INPUT); // Setting pin 2 as INPUT
pinMode(buzzerPin,OUTPUT); // Setting pin 7 as OUTPUT
pinMode(MISO,OUTPUT); //Sets MISO as OUTPUT (Have to Send data to Master IN
SPCR |= _BV(SPE); //Turn on SPI in Slave Mode
received = false;
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()
{ if(received) //Logic to SET buzzer ON OR OFF depending upon the value recerived from master
{
if (Slavereceived==1)
{
digitalWrite(buzzerPin,LOW);
delay(500);
digitalWrite(buzzerPin, HIGH);//Sets pin 7 as HIGH buzzer ON
Serial.println("Slave buzzer ON");
}else
{
digitalWrite(buzzerPin,LOW); //Sets pin 7 as LOW buzzer OFF
Serial.println("Slave buzzer OFF");
}
buttonvalue = digitalRead(buttonpin); // Reads the status of the pin 2
if (buttonvalue == HIGH) //Logic to set the value of x to send to master
{
x=1;
}else
{
x=0;
}
Slavesend=x;
SPDR = Slavesend; //Sends the x value to master via SPDR
delay(200);
}
}
penjelasan:
Komunikasi yang digunakan yaitu SPI atau Serial Peripheral Interface yang fungsinya untuk mentransfer data secara serial bersifat sinkronus dimana pengiriman datanya di iringi sinyal clock dan terdiri dari MISO, MOSI, SCLK/SCL, dan SS.
MISO di pin 12 = Pengiriman data dari slave ke master
MOSI di pin 11 = Pengiriman data dari master ke slave
SCK di pin 13= pembangkitan sinyal clock
SS di pin 10 = Pin untuk penentu slave yang akan diajak komunikasi oleh master dan kondisinya aktif LOW.
Buzzer dihubungkan ke pin arduino 7 sebagai output dan button ke pin arduino 2 sebagai input.
Awalnya menggunakan library spi.h untuk menggunakan komunikasi serial SPI.
Pada master, jika master mendapatkan logika 1, maka buzzer akan hidup lalu delay 200 ms dan buzzer mati. Jika tidak terpenuhi, maka buzzer akan mati lalu delay sebesar 500 ms.
Pada slave, jikamendapatkan logika 1, maka buzzer akan mati lalu delay 500 ms dan buzzer hidup selama 200 ms.
Link Download
[Kembali]