Underwater ROV, slow rs 485 communication Part 1 of 2

Please read part 1 first.

Master (Surface)

#include <RS485_protocol.h>
#include <SoftwareSerial.h>
SoftwareSerial rs485 (10, 11);  // rx pin, tx pin

// Pins
int forePort = A0; //Top fore port thruster PIN for analogRead
int foreStar = A1;
int aftPort = A2;
int aftStar = A3;
int portThruster = A4; // Main port drive thruster pin for analogRead
int starThruster = A5;
const byte ENABLE_PIN = 24; // pin used to set hi and low, transmit or rec
const byte LED_PIN = 13;


//Thrust VAriables
int forePortVal = 1100; //testing levels
int foreStarVal = 1200; 
int aftPortVal = 1300; 
int aftStarVal = 1400; 
int portThrusterVal = 1500; 
int starThrusterVal = 1600; 

byte forePortValHi, foreStarValHi, aftPortValHi, aftStarValHi, portThrusterValHi, starThrusterValHi; // Hibytes for thrust
byte forePortValLo, foreStarValLo, aftPortValLo, aftStarValLo, portThrusterValLo, starThrusterValLo; // Lowbytes for thrust

//Unions for temp humidity and dewpoint
union temperature 
{
  float temp;
  byte b[4];
} t;

union humidity 
{
  float hum;
  byte b[4];
} h;

union dewpoint 
{
  float dew;
  byte b[4];
} d;

//Sensors
int pressureLvl, lightLvl, xAxis, yAxis, zAxis;
float temperature; //for temp and humidity
float humidity; 
float dewpoint;

// callback routines
  
void fWrite (const byte what)
  {
  rs485.write (what);  
  }
  
int fAvailable ()
  {
  return rs485.available ();  
  }

int fRead ()
  {
  return rs485.read ();  
  }

void setup()
{
  Serial.begin(115200);
  rs485.begin (57600);
  pinMode (ENABLE_PIN, OUTPUT);  // driver output enable
  pinMode (LED_PIN, OUTPUT);  // built-in LED
}  // end of setup
  


void loop()
{

  //Thurster pot readings and calibration
  forePortVal = analogRead(forePort); // Reads the value from the pot to forePortVal 
  forePortVal = map(forePortVal, 0, 1023, 4000, 8000); // Calibrates the min max from the pot to correspond with servo pulses 1000 to 2000
  
  //will add other 5 pots
  
  //-------------------------Hi Byte Low Byte creation of variables to be sent over to Slave------
  forePortValHi = highByte(forePortVal);
  forePortValLo = lowByte(forePortVal);
  foreStarValHi = highByte(foreStarVal);
  foreStarValLo = lowByte(foreStarVal);
  aftPortValHi = highByte(aftPortVal);
  aftPortValLo = lowByte(aftPortVal);
  aftStarValHi = highByte(aftStarVal);
  aftStarValLo = lowByte(aftStarVal);
  portThrusterValHi = highByte(portThrusterVal);
  portThrusterValLo = lowByte(portThrusterVal);
  starThrusterValHi = highByte(starThrusterVal);
  starThrusterValLo = lowByte(starThrusterVal);
  
      
  // assemble message
  byte msg [] = { 
     1,    // device 1
     2,    // turn light on
     forePortValHi, // first half of forePortValHi
     forePortValLo,  // second have of level
     foreStarValHi,
     foreStarValLo,
     aftPortValHi,
     aftPortValLo,
     aftStarValHi,
     aftStarValLo,
     portThrusterValHi,
     portThrusterValLo,
     starThrusterValHi, 
     starThrusterValLo    
  };

  // send to slave  
  digitalWrite (ENABLE_PIN, HIGH);  // enable sending
  sendMsg (fWrite, msg, sizeof msg);
  digitalWrite (ENABLE_PIN, LOW);  // disable sending

  // receive response  
  byte buf [24];
  byte received = recvMsg (fAvailable, fRead, buf, sizeof buf);
  
  digitalWrite (LED_PIN, received == 0);  // turn on LED if error    
  
//Create Sensor varaibles from Hi low
pressureLvl = word(buf [2], buf [3]);
lightLvl = word(buf [4], buf [5]);
xAxis = word(buf [6], buf [7]);
yAxis = word(buf [8], buf [9]);
zAxis = word(buf [10], buf [11]);
t.b[0] = buf [12]; // Send bytes from the buffer to the unions
t.b[1] = buf [13];
t.b[2] = buf [14];
t.b[3] = buf [15];
h.b[0] = buf [16];
h.b[1] = buf [17];
h.b[2] = buf [18];
h.b[3] = buf [19];
d.b[0] = buf [20];
d.b[1] = buf [21];
d.b[2] = buf [22];
d.b[3] = buf [23];

temperature = t.temp; // Pull floats from the union
humidity = h.hum;
dewpoint = d.dew;

  Serial.print("Pressure Level = ");
  Serial.print(pressureLvl);
  Serial.print("\t");
  Serial.print("Light Level = ");
  Serial.print(lightLvl);
  Serial.print("\t");
  Serial.print("X Axis = ");
  Serial.print(xAxis);
  Serial.print("\t");  
  Serial.print("y Axis = ");
  Serial.print(yAxis);
  Serial.print("\t");  
  Serial.print("Z Axis = ");
  Serial.print(zAxis);
  Serial.print("\t");  
  Serial.print("Temperature = ");
  Serial.print(temperature);
  Serial.print("\t");  
  Serial.print("Humidity = ");
  Serial.print(humidity);
  Serial.print("\t");  
  Serial.print("Dewpoint = ");
  Serial.println(dewpoint);
}  // end of loop

Slave (ROV)

#include <RS485_protocol.h>
#include <SoftwareSerial.h>
#include <Sensirion.h>
SoftwareSerial rs485 (10, 11);  // rx pin, txpin


// Pins
int pressurePin = A15; //  for pressure sensor
int lightPin = A14; // for light levels
const uint8_t dataPin  =  9; //data pint for temp and humidity
const uint8_t clockPin =  8; // clock pin for temp and humidity
const int xpin = A3;                  // x-axis
const int ypin = A2;                  // y-axis
const int zpin = A1;                  // z-axis
const byte ENABLE_PIN = 24;

//Thrust VAriables
int forePortVal, foreStarVal, aftPortVal, aftStarVal, portThrusterVal, starThrusterVal; //thruster variables

//Sensors VAriables
float temperature; //for temp and humidity
float humidity; 
float dewpoint; 
Sensirion tempSensor = Sensirion(dataPin, clockPin);
int pressureLvl = 0; // for pressure
int lightLvl = 200; // for light level
int xAxis, yAxis, zAxis;
byte hipressureLvl, lopressureLvl, lolightLvl, hilightLvl, loxAxis, hixAxis, loyAxis, hiyAxis, lozAxis, hizAxis, temperature0, temperature1, temperature2, temperature3, humidity0, humidity1, humidity2, humidity3, dewpoint0, dewpoint1, dewpoint2, dewpoint3;

//Unions for temp hum
union temperature 
{
  float temp;
  byte b[4];
} t;

union humidity 
{
  float hum;
  byte b[4];
} h;

union dewpoint 
{
  float dew;
  byte b[4];
} d;

void fWrite (const byte what)
  {
  rs485.write (what);  
  }
  
int fAvailable ()
  {
  return rs485.available ();  
  }

int fRead ()
  {
  return rs485.read ();  
  }

void setup()
{
  Serial.begin(115200); // for testing only
  rs485.begin (57600); // communication 4 RS485
  Serial2.begin(115200); //  Pololu Servo controller
  pinMode (ENABLE_PIN, OUTPUT);  // driver output enable
  digitalWrite (ENABLE_PIN, LOW);  // Sets pin to low 4 rs485

}

void loop()
{
//Sensor readings
//Pressure sensor
pressureLvl = analogRead(pressurePin);

//Light levels
lightLvl = analogRead(lightPin);

//temp and humidity
tempSensor.measure(&temperature, &humidity, &dewpoint);

// X Y and Z Axis readings
xAxis = analogRead(xpin);
yAxis = analogRead(ypin);
zAxis = analogRead(zpin);
t.temp = temperature;
h.hum = humidity;
d.dew = dewpoint;




//Hi Lo byte creation
  hipressureLvl = highByte(pressureLvl); // splits bytes off
  lopressureLvl = lowByte(pressureLvl);
  hilightLvl = highByte(lightLvl); 
  lolightLvl = lowByte(lightLvl);
  hixAxis = highByte(xAxis); 
  loxAxis = lowByte(xAxis); 
  hiyAxis = highByte(yAxis);
  loyAxis = lowByte(yAxis); 
  hizAxis = highByte(zAxis); 
  lozAxis = lowByte(zAxis); 



//Send sensor readings
  byte buf [24];
  
  byte received = recvMsg (fAvailable, fRead, buf, sizeof (buf) - 1);
  
  if (received)
    {
    if (buf [0] != 1)
      return;  // not my device
      
    if (buf [1] != 2)
      return;  // unknown command
    
    byte msg [] = {
       0,  // device 0 (master)
       3,  // turn light on command received
       hipressureLvl,
       lopressureLvl,
       hilightLvl,
       lolightLvl,
       hixAxis,
       loxAxis, 
       hiyAxis,
       loyAxis, 
       hizAxis,
       lozAxis,
       t.b[0],
       t.b[1],
       t.b[2],
       t.b[3],
       h.b[0],
       h.b[1],
       h.b[2],
       h.b[3],
       d.b[0],
       d.b[1],
       d.b[2],
       d.b[3]
      
    };
    
    delay (1); 
    digitalWrite (ENABLE_PIN, HIGH);  // enable sending
    sendMsg (fWrite, msg, sizeof msg);
    digitalWrite (ENABLE_PIN, LOW);  // disable sending
    

    forePortVal = word(buf [2], buf[3]);   // convert the bytes from master
    foreStarVal = word(buf [4], buf[5]);
    aftPortVal = word(buf [6], buf[7]);
    aftStarVal = word(buf [8], buf[9]);
    portThrusterVal = word(buf [10], buf[11]);
    starThrusterVal = word(buf [12], buf[13]);

    Serial.println(forePortVal); // for testing only
    
   }  // end if something received
   
 //Thurster values to Servo Controller  
   set_target(1, forePortVal);
   set_target(2, foreStarVal);
   set_target(3, aftPortVal);
   set_target(4, aftStarVal);
   set_target(5, portThrusterVal);
   set_target(6, starThrusterVal);
   
}  // end of main loop

void set_target(unsigned char servo, unsigned int target)
{
    Serial2.write(0xAA); //start byte
    Serial2.write(0x0C); //device id
    Serial2.write(0x04); //command number
    Serial2.write(servo); //servo number
    Serial2.write(target & 0x7F);
    Serial2.write((target >> 7) & 0x7F);    
}

Thank you
Andrew