Issue with sending a packet containing "too much information"?

I’ve run into a bit of a curious problem with my program.

To begin I will give you a little insight into what I’m trying to do.

I am building a submersible remotely operated vehicle (ROV). This is a school project and I have only a week of any formal instruction on the subject of programming.

All of the important electronics will be bottom side (on the ROV under the water). This includes My Arduino Mega 2560, 2 H-Bridge boards with 4 H-bridges embedded on the board (these will operate the Thrusters, Manipulator and other suck systems). I will be using my laptop with a PS3 Controller and a Processing Program to interpret the information from my controller and send it serially via USB to the Mega. Which the Mega will then interpret said information and Fire Outputs based on read inputs.

Now for my problem.

My program on the Arduino isn’t able to pick apart all of the pieces of my packet to fire a plurality of outputs at once.

I have come up with a few possible options, break my large packet up into a bunch of little packets or possibly convert my inputs to a binary array and each bit represents an input state. I have no idea on how to do the latter, and I have no idea if the prior would even work.

Before I get to my Question time for some code:

Code for the Processing Side:

import procontroll.*;
import java.io.*;
import processing.serial.*;

//Create instances of specific hardware components;
ControllIO controll;
ControllDevice device;

ControllStick stick1;
ControllStick stick2;
ControllButton L2;
ControllButton R2;
ControllButton Up;
ControllButton Dwn;

//Create Serial port Instance
Serial port;

//Global Variable Callout:
byte y_left;
byte y_right;
byte crab_l;
byte crab_r;
byte vert_up;
byte vert_dwn;

char PacketStart = '~'; //sgnifies the start of data packet

void setup()
{
  size(100,100);
  
  //Open a Serial port with device 0 at the defined speed;
  port = new Serial(this, Serial.list()[0], 9600);
   
  
  // Instantiate input/output capabilities;
  controll = ControllIO.getInstance(this);
  
  //lock onto the Controller
  device = controll.getDevice("MotioninJoy Virtual Game Controller");
  
  //recover the name of the controller from the printDevice() command
  device.printSticks();
  device.printButtons();
  
  //Assigning variable names to both PS3 sticks:
  stick1 = device.getStick("X axis Y axis");
  stick2 = device.getStick("Z Axis X Rotation");
  L2 = device.getButton("L1");
  R2 = device.getButton("R1");
  Up = device.getButton("Button 13");
  Dwn = device.getButton("Button 15");

  
  //Tolerances to tilt on joysticks before it registers
  stick1.setTolerance(0.10f);
  stick2.setTolerance(0.10f);


  
  //Drawing Settings
  fill(0);
  rectMode(CENTER);
}

void draw()
{
  background(255);
  
  //Grab both stick Y-Values
  y_left = (byte) map(stick1.getY(), 1, -1, 255, 1) ;
  y_right = (byte) map(stick2.getX(), 1, -1, 255, 1) ;
  crab_l = (byte) map(L2.getValue(), 0, 8, 0, 1);
  crab_r = (byte) map(R2.getValue(), 0, 8, 0, 1);
  vert_up = (byte) Up.getValue();
  vert_dwn = (byte) Dwn.getValue();
  
 //for debugging:
  print(y_left);
  print("  ");
  print(y_right);
  print("  ");
  print(crab_l);
  print("  ");
  print(crab_r);
  print("  ");
  print(vert_up);
  print("  ");
  println(vert_dwn);
  
  
  
  //Send over the Serial Data;
  port.write( PacketStart ); 
  port.write(y_left);
  port.write(y_right);
  port.write(crab_l);
  port.write(crab_r);
  port.write(vert_up);
  port.write(vert_dwn);

}

Arduino Code:

const int loadTime = 100;
const char PacketStart = '~'; 
const int portBow_Dir = 2;
const int portBow_PWM = 3;
const int portStern_Dir = 4;
const int portStern_PWM = 5;
const int stbdBow_Dir = 7;
const int stbdBow_PWM = 6;
const int stbdStern_Dir = 8;
const int stbdStern_PWM = 9;
//const int vert_Dir = 12;
//const int vert_PWM = 10;
//const int manip_Dir = 13;
//const int manip_PWM = 11;



int leftSpeed = 0;
int rightSpeed = 0;
int crab_left;
int crab_right;
//int vert_up;
//int vert_dwn;
//int manip_open;
//int manip_close;

void setup() 
{
  pinMode(portBow_Dir, OUTPUT);
  pinMode(portBow_PWM, OUTPUT);
  pinMode(portStern_Dir, OUTPUT);
  pinMode(portStern_PWM, OUTPUT);
  pinMode(stbdBow_Dir, OUTPUT);
  pinMode(stbdBow_PWM, OUTPUT);
  pinMode(stbdStern_Dir, OUTPUT);
  pinMode(stbdStern_PWM, OUTPUT);
//  pinMode(vert_Dir, OUTPUT);
//  pinMode(vert_PWM, OUTPUT);
//  pinMode(manip_Dir, OUTPUT);
//  pinMode(manip_PWM, OUTPUT);
  
  Serial.begin(9600);
  
  delay(loadTime);
  
 
}

void loop()
{
  while(Serial.available() < 1);
  if (Serial.read() == PacketStart)
  {
    while (Serial.available() < 2);
    
    leftSpeed = Serial.read();
    rightSpeed = Serial.read();
    crab_left = Serial.read();
    crab_right = Serial.read();
//    vert_up = Serial.read();
//    vert_dwn = Serial.read();
    

    
  //Port side Motor forward and backward
     if (leftSpeed > 0)
    {
      leftSpeed = map(leftSpeed, 1, 100, 1, 255);
      constrain (leftSpeed, 1, 250);
      
      digitalWrite(portBow_Dir, HIGH);
      digitalWrite(portStern_Dir, HIGH);
      analogWrite(portBow_PWM, leftSpeed);
      analogWrite(portStern_PWM, leftSpeed);
    }
   else if(leftSpeed < 0)
    {
      leftSpeed = map(leftSpeed, -1, -100, 1, 255);
      constrain (leftSpeed, 1, 250);
      
      digitalWrite(portBow_Dir, LOW);
      digitalWrite(portStern_Dir, LOW);
      analogWrite(portBow_PWM, leftSpeed);
      analogWrite(portStern_PWM, leftSpeed);
    }
    
    //stbd forward and reverse   
    else if (rightSpeed > 0)
    {
      rightSpeed = map(rightSpeed, 1, 100, 1, 255);
      constrain (rightSpeed, 1, 250);
      
      digitalWrite(stbdBow_Dir, HIGH);
      digitalWrite(stbdStern_Dir, HIGH);
      analogWrite(stbdBow_PWM, rightSpeed);
      analogWrite(stbdStern_PWM, rightSpeed);
    }
   else if (rightSpeed < 0)
    {
      rightSpeed = map(rightSpeed, -1, -100, 1, 255);
      constrain (rightSpeed, 1, 250);
      
      digitalWrite(stbdBow_Dir, LOW);
      digitalWrite(stbdStern_Dir, LOW);
      analogWrite(stbdBow_PWM, rightSpeed);
      analogWrite(stbdStern_PWM, rightSpeed);
    }
      
   else if (crab_left > 0)
   {
     digitalWrite(portBow_Dir, LOW);
     digitalWrite(portStern_Dir, HIGH);
     digitalWrite(stbdBow_Dir, HIGH);
     digitalWrite(stbdStern_Dir, LOW);
  
     analogWrite(portBow_PWM, 157);
     analogWrite(portStern_PWM, 157);
     analogWrite(stbdBow_PWM, 157);
     analogWrite(stbdStern_PWM, 157);
   }
  
  else if (crab_right > 0)
  {
           
   digitalWrite(portBow_Dir, HIGH);
   digitalWrite(portStern_Dir, LOW);
   digitalWrite(stbdBow_Dir, LOW);
   digitalWrite(stbdStern_Dir, HIGH);
  
   analogWrite(portBow_PWM, 157);
   analogWrite(portStern_PWM, 157);
   analogWrite(stbdBow_PWM, 157);
   analogWrite(stbdStern_PWM, 157);
  }
  else 
  {
   analogWrite(portBow_PWM, 0);
   analogWrite(portStern_PWM, 0);
   analogWrite(stbdBow_PWM, 0);
   analogWrite(stbdStern_PWM, 0);
  }
  
//  if (vert_up = 8)
//  {
//    digitalWrite(vert_Dir, HIGH);
//     
//     for(speed = 0; speed <= 255; speed + 10)
//     {
//       analogWrite(vert_PWM, speed);
//     }
//  }
//  else if (vert_up = 0)
//  {
//    digitalWrite(vert_Dir, HIGH);
//    analogWrite(vert_PWM, speed);    
//  }
  
//  if (vert_dwn = 8)
//  {
//    digitalWrite(vert_Dir, LOW);
//    
//    for(speed = 255; speed <=0; speed - 10)
//    {
//      analogWrite(vert_PWM, speed);
//    }
//  }
//    else if (vert_dwn = 0)
//    {
//      digitalWrite(vert_Dir, LOW);
//      analogWrite(vert_PWM, speed);
//    }
   }
}

Now for my questions:

  1. is my problem what I think it is?
  2. will any of my possible fixes, fix the problem?
  3. is there a more efficient way of doing what I’m trying to do?

Thank you in Advance,
B

Common issue - you’ve ensured that you have two characters to read and then you read four:

    while (Serial.available() < 2);
    
    leftSpeed = Serial.read();
    rightSpeed = Serial.read();
    crab_left = Serial.read();
    crab_right = Serial.read();

Here is one problem…

    while (Serial.available() < 2);
    
    leftSpeed = Serial.read();
    rightSpeed = Serial.read();
    crab_left = Serial.read();
    crab_right = Serial.read();

You wait for two bytes to arrive and then read 4 bytes.

wildbill:
Common issue - you’ve ensured that you have two characters to read and then you read four:

    while (Serial.available() < 2);

leftSpeed = Serial.read();
    rightSpeed = Serial.read();
    crab_left = Serial.read();
    crab_right = Serial.read();

Hah, one number being the bane of my existence.

Thank you.

DavidOConnor:
Here is one problem…

    while (Serial.available() < 2);

leftSpeed = Serial.read();
    rightSpeed = Serial.read();
    crab_left = Serial.read();
    crab_right = Serial.read();




You wait for two bytes to arrive and then read 4 bytes.

So I have more than one? Which does not surprise me, I am fairly new at this.

So I have more than one?

No. Just two people saw it at around the same time.

B_Mariner:
So I have more than one? Which does not surprise me, I am fairly new at this.

It's hard to know how you expect us to take things forward based on this reply.

In my opinion you have shown us far too much code and given us very little information. I'm lazy. I'm not going to take the trouble to study your code for 10 or 15 minutes to try to figure out what it does. But if you tell me I will certainly try to help.

What is a typical transmission from the PC to the Arduino?
How often is this data transmitted?

How is the Arduino supposed to respond to the data it receives?

...R

B_Mariner:

  1. is my problem what I think it is?

Apparently you have more than one problem.

  1. will any of my possible fixes, fix the problem?

See 1.

  1. is there a more efficient way of doing what I'm trying to do?

Depends what you mean by more efficient. A couple days ago I posted a sketch on the forum for receiving 'key=value' pairs sent to a Serial port.

While it is not a particularly efficient comms protocol, it addresses the edge cases and mistakes which comms programming beginners often struggle with. Borrowing my code and focussing your own effort on your application's control logic, might be more efficient use of your time.