Sunday, March 5, 2017

Arduino to Arduino Communication via Tx/Rx

Arduino to Arduino Communication via Tx/Rx

3/5/2017

Scope: 
  Experience using Tx/Rx to communicate a slave Arduino from the Master Arduino

Setup: 

  • Connect Vcc to Vcc
  • Connect GND to GND
  • Connect Tx to Rx
  • Connect Rx to Tx





Demo:



Master Arduino Code:

/*Master Arduino Code
 * 3/5/17
 Scope: Experience COM communication between Arduino
 Approach: Send a command via Tx/Rx from one Arduino to another Arduino.
 */

void setup() {
  // initialize serial:
  Serial.begin(9600);
  pinMode(9, OUTPUT);
}

void loop() {
  //Turn on LED for 1 sec
  digitalWrite(9, HIGH);
  delay(500);
  digitalWrite(9, LOW);

  //Send instruction via COM
  Serial.print("9 5 200");

  //Wait until above instruction is completed
  delay(3000);
}


Slave Arduino Code:

/* Slave Arduino Code
By Samson Yang 3/5/2017
This is a simple demo of how to use Arduino control another Arduino 'via' Tx/Rx.
*/

void setup() {
  // initialize serial:
  Serial.begin(9600);
  // make the pins outputs:
  pinMode(9, OUTPUT);
}

void loop() {
  // if there's any serial available, read it:
  while (Serial.available() > 0) {
    int Pin = Serial.parseInt();
    int Count = Serial.parseInt();
    int delay_time = Serial.parseInt();      
    if((Pin==9)){LED_Blinking(Pin,Count,delay_time);}
  }    
}

//Control LED blinking on pin 9
void LED_Blinking(int Pin,int Count,int delay_time){  
  //Blank LED
  for (int i = 0;i<Count;i++){
    digitalWrite(Pin, HIGH);delay(delay_time/2);digitalWrite(Pin, LOW);delay(delay_time/2);
  }
}





No comments:

Post a Comment