Komunikasi Serial 2 Arduino UNO - Half Duplex

 

Komunikasi Serial 2 Arduino UNO - Half Duplex

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

 

VR #1 - menggerakan Servo yang terdapat di Arduino #2

VR #2 - menyalakan LED yang terdapat di Arduino #1

 

Arduino #1

#include <SoftwareSerial.h>

SoftwareSerial SerialBaru(2,3);

const int ledPin =  13;  // Built-in LED
const int EnTxPin =  4;  // HIGH:Transmitter, LOW:Receiver
void setup()
{
  SerialBaru.begin(1200);
  SerialBaru.setTimeout(100);  
  pinMode(ledPin, OUTPUT);
  pinMode(EnTxPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  digitalWrite(EnTxPin, HIGH);
}
 
void loop()
{   
  int rdata = analogRead(0); //data from potentiometer
  int angle= map(rdata, 0, 1023, 0, 180);
 
  //transmitter data packet
  SerialBaru.print("I"); //initiate data packet
  SerialBaru.print("S"); //code for servo
  SerialBaru.print(angle); //servo angle data
  SerialBaru.print("F"); //finish data packet
  delay(50);

  //receiver data packet
  SerialBaru.print("I"); //initiate data packet
  SerialBaru.print("L"); //code for sensor
  SerialBaru.print("F"); //finish data packet
  SerialBaru.flush();    
 
  digitalWrite(EnTxPin, LOW); //RS485 as receiver

  if(SerialBaru.find("i"))
  {
      int data=SerialBaru.parseInt();
      if(SerialBaru.read()=='f') //finish reading
       {
         onLED(data);            
      }
      
  }
  digitalWrite(EnTxPin, HIGH); //RS485 as transmitter
 
}

void onLED(int data)
{
  if(data>500)
     digitalWrite(ledPin, HIGH);
  else
     digitalWrite(ledPin, LOW);
}

Arduino #2

#include <Servo.h>

#include <SoftwareSerial.h>

SoftwareSerial SerialBaru(2,3);

Servo myservo;
const int EnTxPin = 4;
void setup () {
  SerialBaru.begin (1200);
  myservo.attach (9);
  pinMode(EnTxPin, OUTPUT );
  digitalWrite (EnTxPin, LOW );
}

void loop (){
  if ( SerialBaru.available ()){
    if ( SerialBaru.read () == 'I' ){
      char function = SerialBaru.read ();
      if (function == 'S' ){
        int angle = SerialBaru.parseInt ();
        if ( SerialBaru.read () == 'F' ){
          if (angle <= 180) {
            myservo.write (angle);
          }
        }
      }
      else if (function == 'L' ){
        if ( SerialBaru.read () == 'F' ){
          int val = analogRead (0);
          digitalWrite (EnTxPin, HIGH ); //enable to transmit
          SerialBaru.print ( "i" );
          SerialBaru.print (val);
          SerialBaru.println ( "f" );
          SerialBaru.flush ();
          digitalWrite (EnTxPin, LOW ); //enable to receive
        }
      }
    }
  }
  delay (10);
}

 

Ref:

Modbus (RS-485) Using Arduino (create.arduino.cc)

 


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>

Pi Coin

Pi Coin adalah cryptocurrency yang dikembangkan oleh tim di Stanford University. Berikut adalah penjelasan spesifik tentang Pi Coin: ### 1. ...