SoftwareSerialRemoteExample

From ArduinoInfo
Jump to navigation Jump to search

SoftwareSerialRemoteExample


This shows use of the Software Serial library that is provided with the Arduino IDE.

One 'Master'Arduino uses the Serial Monitor for you to interact with. This uses the usual USB connected serial port. Another serial port is created with the Software Serial library, using pins 11 and 12. See the following code example for the 'Master'.

The two Arduinos are connected by 3 wires: Ground, and pins 11 and 12. Pin 11 on one is connected to pin 12 on the other and pin 12 on one is connected to pin 11 on the other: Transmit to Receive.

The second Arduino runs the code in the second example below. You can copy and paste these into the Arduino IDE. (NOTE: You can run two copies of the Arduino software at once if you wish. Keep track of which Arduino is on which Serial Port!).

You use this by bringing up both connected Arduinos and running the Serial Monitor on the "Master". Type in the top window of the Serial Monitor and it should be echoed back as it is sent to the second Arduino and looped back. You should see the Pin13 LED on the Remote Arduino blink as the data is received and sent back.

This will be the basis for controlling remotely located Arduinos. Here is an example showing RS485 for the physical connection between Arduinos, which will allow distances up to 1 Km and multiple Arduinos to be run on the same long cable.

/* YourDuino SoftwareSerialExample1
   - Connect to another Arduino running "YD_SoftwareSerialExample1Remote"
   - Connect this unit Pins 10, 11, Gnd
   - To other unit Pins 11,10, Gnd  (Cross over)
   - Open Serial Monitor, type in top window. 
   - Should see same characters echoed back from remote Arduino

   Questions: terry@yourduino.com 
*/

/*-----( Import needed libraries )-----*/
#include <SoftwareSerial.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define SSerialRX 10
#define SSerialTX 11
#define Pin13LED  13
/*-----( Declare objects )-----*/
SoftwareSerial mySerial(SSerialRX, SSerialTX); // RX, TX
/*-----( Declare Variables )-----*/
int byteReceived;
int byteSend;

void setup()   /****** SETUP: RUNS ONCE ******/
{
  // Start the built-in serial port, probably to Serial Monitor
  Serial.begin(9600);
  Serial.println("YourDuino.com SoftwareSerial remote loop example");
  Serial.println("Use Serial Monitor, type in upper window, ENTER");
  pinMode(Pin13LED, OUTPUT);

  // Start the software serial port, to another device
  mySerial.begin(4800);   // set the data rate 

}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  digitalWrite(Pin13LED, HIGH);  // Show activity
  if (Serial.available())
  {
    byteReceived = Serial.read();
    mySerial.write(byteReceived);  // Send byte to Remote Arduino
    digitalWrite(Pin13LED, LOW);  // Show activity    
    delay(25);
  }

  if (mySerial.available())  //Look for data from other Arduino
   {
    digitalWrite(Pin13LED, HIGH);  // Show activity
    byteSend = mySerial.read();    // Read received byte
    Serial.write(byteSend);        // Show on Serial Monitor
    delay(10);
    digitalWrite(Pin13LED, LOW);  // Show activity   
   }

}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/

//NONE
//*********( THE END )***********




/* YourDuino SoftwareSerialExample1Remote
   - Used with YD_SoftwareSerialExample1 on another Arduino
   - Remote: Receive data, loop it back...
   - Connect this unit Pins 10, 11, Gnd
   - To other unit Pins 11,10, Gnd  (Cross over)
   - Pin 13 LED blinks when data is received  
   
   Questions: terry@yourduino.com 
*/

/*-----( Import needed libraries )-----*/
#include <SoftwareSerial.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define SSerialRX 10
#define SSerialTX 11
#define Pin13LED  13
/*-----( Declare objects )-----*/
SoftwareSerial mySerial(SSerialRX, SSerialTX); // RX, TX
/*-----( Declare Variables )-----*/
int byteReceived;
int byteSend;

void setup()   /****** SETUP: RUNS ONCE ******/
{
  // Start the built-in serial port, probably to Serial Monitor
  Serial.begin(9600);
  Serial.println("SerialRemote");  // Can be ignored
  pinMode(Pin13LED, OUTPUT);

  // Start the software serial port, to another device
  mySerial.begin(4800);   // set the data rate 
}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  //Copy input data to output  
  if (mySerial.available())
  {
    byteSend = mySerial.read();   // Read the byte 

    digitalWrite(Pin13LED, HIGH);  // Show activity
    delay(25);
    digitalWrite(Pin13LED, LOW);

    mySerial.write(byteSend); // Send the byte back
//    delay(100);
  }// End If MySerialAvailable

}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/
//NONE

//*********( THE END )***********