Two Way Serial Communication

Le Mon 13 October 2014

The most basic arrangement for the ROV is one way communication between the handset Arduino and the ROV Arduino. It would be more useful to have some ability to receive data from the ROV as well. This information would primarily be about battery voltage, but with extra instrumentation may include things like depth, temperature, camera status, and motor current.

The sketches below send messages to one another. The Master is an Arduino Mega so that it can communicate with the computer without the need to add in software serial ports. The slave is a Seeeduino Stalker operating a servo and an array of LEDs to indicate the messages received and do something that is vaguely related to what it would be doing in the ROV. For this arrangement to work properly the 5V supply to the servo and to supply the Seeeduino Stalker MUST be from another source. The 5V supply from the Arduino Mega's 5V line is not enough and so the servo creates a lot of problems with noise and possibly voltage droop that prevent the happy operation of the Seeeduino Stalker. A 5V regulated supply and large capacitor was used to provide tidy power to the 5V rail that feeds the servo, the LEDs, and the Seeeduino Stalker.

Circuit Diagram

Two Way Serial Communication Circuit Diagram
For a higher resolution image click here.

The two sketches presented below can be downloaded from here: TwoWaySerialTest_Masterv3_and_Slavev3.zip

Sketch for the Master – Arduino Mega

/*
TwoWaySerialTest_Masterv3.ino
This makes use of Two Way Serial Communication with a Master Arduino
using a potentiometer to provide data that will be used to actuate
a servo on a Slave Arduino.  The Slave Arduino will send back data
on the angle it is turning to.  This figure will be sent to the PC
for display on the Terminal.  Serial_Joystick_Masterv1.ino has
also been used to develop the sensor actuation part.

The two way communication is swift and accurate between the
two arduinos.  It is essential that a regulated 5V supply independent
of the Arduinos is supplied to the 5V rail.  This is to avoid 
the servo putting noise or too much draw on the 5V supply that the
Arduinos are trying to run off.

Both Arduinos will be running a 9600 baud rate to match what will
be run on the ROV. Except for the Serial Comms to the PC which can run
flat out eh.

The Master will be an Arduino Mega to make use of it's extra serial
connections to still allow the serial monitor to be used.  Communication
with the PC will be through the standard Hardware Serial connection
and communication with the Slave Arduino through Serial1.  The
Slave Arduino will be a Seeeduino Stalker.

On both Arduinos a 10 second delay is added before the Serial communications
are initiated so that there is a window of opportunity to
load other programs before the Serial ports are otherwise engaged.
This is generally not a problem with the Mega and the Stalker anyway.

Pin Assignments on the Master
Potentiometer connected to A00
Serial Connection: Stalker D1 (TX) to Mega D19 (RX1)
Serial Connection: Stalker D0 (RX) to Mega D18 (TX1)
Connect the GND on both
Mega 5V to the 5V rail.

*/

const int potpin = 0; // analog pin used to connect the potentiometer
int flex = 0;  // variable to read the value from the analog pin
int inmsg = 0;  //messages back from the slave.

void setup()
{
  delay(10000);    //The 10 second delay to upload new programs.
  Serial.begin(9600);  //Begin Serial to talk to the Serial Monitor   
  Serial1.begin(9600); //Begin Serial to talk to the Slave Arduino
  Serial.println("Serial Monitor Connected"); 
  Serial.parseInt();     //clear any garbage in the buffer.  
}

void loop()
{
  //Read the flex sensor.  We'll keep this value raw.
  flex = analogRead(potpin);// reads the value of the potentiometer(value between 0 and 1023)
  Serial.print("Sent Value ");
  Serial.println(flex); // Send the potentiometer value to the Monitor
  Serial1.println(flex);  //Send the potentiometer value to the Slave Arduino
  // keep an ear out for messages back from the Slave.  You know
  // complaints and the like.
  delay(10);  //This delay is added to give the Slave a chance to
  //return data

  if(Serial1.available()) //while there is data available
  {
    inmsg = Serial1.parseInt();
    Serial.print("Received Angle ");
    Serial.println(inmsg);        //Print message to the Serial Monitor
    delay(10);  //This delay is added to give the Slave a chance to
                //return data
  }

}

Sketch for the Slave – Seeeduino Stalker

/*
TwoWaySerialTest_Slavev3.ino
This makes use of Two Way Serial Communication with a Master Arduino
using a potentiometer to provide data that will be used to actuate
a servo on a Slave Arduino.  The Slave Arduino will send back data
on the angle it is turning to.  This figure will be sent to the PC
for display on the Terminal.  Serial_Slavev2.ino has also been
used to develop the servo actuation part.
The two way communication is swift and accurate between the
two arduinos.  It is essential that a regulated 5V supply independent
of the Arduinos is supplied to the 5V rail.  This is to avoid 
the servo putting noise or too much draw on the 5V supply that the
Arduinos are trying to run off.

Both Arduinos will be running a 9600 baud rate to match what will
be run on the ROV

The Master will be an Arduino Mega to make use of it's extra serial
connections to still allow the serial monitor to be used.  Communication
with the PC will be through the standard Hardware Serial connection
and communication with the Slave Arduino through Serial1.  The
Slave Arduino will be a Seeeduino Stalker.

On both Arduinos a 10 second delay is added before the Serial communications
are initiated so that there is a window of opportunity to
load other programs before the Serial ports are otherwise engaged.
This is generally not a problem with the Mega and the Stalker anyway.

Pin Assignments on the Slave
Servo Control connected to D09
Serial Connection: Stalker D1 (TX) to Mega D19 (RX1)
Serial Connection: Stalker D0 (RX) to Mega D18 (TX1)
Connect the GND on both
Connect the Servo 5V to a common 5V rail
Stalker 5V from the 5V rail.
Received Message indicator LEDs are on pins D10, D11, and D12



*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int recdMSG = 0; //variable for received Messages
int val=90;  // Scaled setting to the servo
              //- default is neutral 90degrees

int inmsg = 0;  //This is the message flag from the Serial
const int redLEDPin = 12; //Red LED on pin D12
const int yelLEDPin = 11; //Yellow LED on pin D11
const int grnLEDPin = 10; //Green LED on pin D10

void setup()
{
  delay(10000);  //Give us a window to upload new sketches.
  Serial.begin(9600);  //Serial port for communications with Master
  pinMode(redLEDPin,OUTPUT); //Set up the LED pins
  pinMode(yelLEDPin, OUTPUT);
  pinMode(grnLEDPin,OUTPUT);
  myservo.attach(9,600,2250);// attaches the servo servo on pin 9 to the servo object
}

void loop()
{
  if(Serial.available()) //while there is data available
  {
    recdMSG = Serial.parseInt();             //Reads integers as integer rather than ASCII
    val = map(recdMSG, 0, 1023, 0, 179);  //scale the value to an angle for the servo
    myservo.write(val);    //send this angle to the servo
    Serial.println(val);   //Send the new val back to the Tx
    if(recdMSG>=651)    //The following lights an LED based on the message content.
    {
      digitalWrite(redLEDPin,HIGH);
      digitalWrite(yelLEDPin,LOW);
      digitalWrite(grnLEDPin,LOW);
    }
    if(recdMSG>350,recdMSG <= 650)
    {
      digitalWrite(redLEDPin,LOW);
      digitalWrite(yelLEDPin,HIGH);
      digitalWrite(grnLEDPin,LOW);
    }
    if(recdMSG <= 350)
    {
      digitalWrite(redLEDPin,LOW);
      digitalWrite(yelLEDPin,LOW);
      digitalWrite(grnLEDPin,HIGH);
    }
  }
  delay(15); //15 millisecond delay

}

Par Hamish Trolove, Catégorie : Tech Projects

Tags : ROV / Arduino / Communication / Serial /