Master - Slave Arduino Menggunakan Komunikasi Serial

Master - Slave Arduino menggunakan komunikasi serial.

Simulasi menggunakan: https://www.tinkercad.com/

 

Master

#include <SoftwareSerial.h>

SoftwareSerial ArduinoSlave(2,3);
char cmd;
char old_cmd;
char answer;
char old_answer;

void setup(){
  Serial.begin(9600);
  Serial.println("ENTER Commands:");
  ArduinoSlave.begin(1200);            
}

void loop(){
  old_cmd=cmd;
  old_answer=answer;
  //Read command from monitor
  if (Serial.available()){
    cmd=Serial.read();
  }
  //Read answer from slave
  if (ArduinoSlave.available()){
    answer=ArduinoSlave.read();
    delay(100);
  }
  //Send data to slave
  if(cmd!=old_cmd){
    Serial.println();
    Serial.print("Master sent : ");
    Serial.println(cmd);
    ArduinoSlave.write(cmd);
    delay(100);
  }
  //Send answer to monitor
  if(answer!=old_answer){
    Serial.print("Slave received : ");
    Serial.println(answer);
    Serial.println();
    Serial.println("ENTER Commands:");
  }
 
}

 

Slave

#include <SoftwareSerial.h>

SoftwareSerial ArduinoMaster(2,3);
char cmd;
char old_cmd;

void setup(){
  ArduinoMaster.begin(1200);    
}

void loop(){
  old_cmd=cmd;
  // Read data from master
  if (ArduinoMaster.available()){
    cmd=ArduinoMaster.read();
    delay(100);
  }
  // Send answer to master
  if(cmd!=old_cmd){
    ArduinoMaster.write(cmd);
    delay(100);
  }
}

 

Hasil Serial Monitor:

 


 

Bonus: Script Box / Script Area di dalam tulisan blogspot


<div style="border: 3px #eeeeee solid; padding: 10px;background-color:#ffffff;
overflow: auto; height: 100px; width: 520px;
text-align: left;">
.kode script disini.</div>



<div style="-moz-border-radius: 10px; -webkit-border-radius: 10px; background-color: #6fa8dc; border-radius: 10px; border: 4px double rgb(255, 255, 255); height: 100px; overflow: auto; padding: 10px; t-align: left; text-align: left; width: 90%;">
.kode script disini.</div>