trouble with "If'' and "Else if" along with Boolean

Hello again,

First I want to say thanks for the help previously.

now, I'm having an issue with getting two motors to fire together in both forward-forward, reverse-reverse, forward- reverse and, reverse-forward style control scheme.

Similar to a Tank style controls.

I think the problem is my if statements. I've been considering using Case functions, just my depth of knowledge for that is shallow as a shower.

Processing Code:

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("Y Rotation Z 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, -100, 100) ;
  y_right = (byte) map(stick2.getX(), 1, -1, -100, 100) ;
  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() < 4);
    
    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 (rightSpeed > 0 && leftSpeed >0);
//    {
//      rightSpeed = map(rightSpeed, 1, 100, 1, 255);
//      constrain (rightSpeed, 1, 250);
//      
//      leftSpeed = map(leftSpeed, 1, 100, 1, 255);
//      constrain (leftSpeed, 1, 250);
//      
//      digitalWrite(stbdBow_Dir, HIGH);
//      digitalWrite(stbdStern_Dir, HIGH);
//      analogWrite(stbdBow_PWM, rightSpeed);
//      analogWrite(stbdStern_PWM, rightSpeed);
//      
//      digitalWrite(portBow_Dir, HIGH);
//      digitalWrite(portStern_Dir, HIGH);
//      analogWrite(portBow_PWM, leftSpeed);
//      analogWrite(portStern_PWM, leftSpeed);
//    }
//     else if (rightSpeed < 0 && leftSpeed < 0)
//    {
//      rightSpeed = map(rightSpeed, -1, -100, 1, 255);
//      constrain (rightSpeed, 1, 250);
//      
//      leftSpeed = map(leftSpeed, -1, -100, 1, 255);
//      constrain (leftSpeed, 1, 250);
//      
//      digitalWrite(stbdBow_Dir, LOW);
//      digitalWrite(stbdStern_Dir, LOW);
//      analogWrite(stbdBow_PWM, rightSpeed);
//      analogWrite(stbdStern_PWM, rightSpeed);
//      
//      digitalWrite(portBow_Dir, LOW);
//      digitalWrite(portStern_Dir, LOW);
//      analogWrite(portBow_PWM, leftSpeed);
//      analogWrite(portStern_PWM, leftSpeed);
//    }
      
   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);
//    }
   }
}

If there is a way to streamline my code please feel free to criticize, I need it. haha

To start, make all these pin assignments byte instead of int:

//const int loadTime = 100;
const char PacketStart = '~'; << not this one, but the rest
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;

If you decide to put these kind back in:
// if (vert_up = 8 )
make sure to use == for a comparison. Single = is an assignment.

CrossRoads:
To start, make all these pin assignments byte instead of int:

//const int loadTime = 100;

const char PacketStart = '~'; << not this one, but the rest
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;

If I set my pin Callouts from "int" to "byte" how does that change the other part of my coding? Pure curiosity, and want of understanding.

also thanks for the reply.

You need to be careful with ELSE IF. It is easy to make a logical nonsense.

I would only use that for a few mutually exclusive options.

You are using leftSpeed in one place and rightSpeed in another and I suspect that isn't logically correct.

I suggest you take a pencil and paper and write out a table showing all possible permutations before you decide how to write the code for it.

If you are trying to establish the states for a pair of motors it might be better to do that directly by having a couple of variables (for example motorLeft = 'F'; motorRight = 'R'; etc). Then, and separately, you can have a neat little function that reads those variables and makes the motors move appropriately. I don't reca;; what sort of motors you are using, but it may even be possible to make it simpler if a single number can define the direction and speed.

...R

byte vs int saves on SRAM. Might be a little faster as you're only moving 1 byte across the bus vs 2 bytes.

rightSpeed & left Speed I think would be needed to support different rates of turn.

Send 3 bytes

  //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);

}

How many bytes are you reading ?

{
  while(Serial.available() < 1);
  if (Serial.read() == PacketStart)
  {
    while (Serial.available() < 4);
    
    leftSpeed = Serial.read();
    rightSpeed = Serial.read();
    crab_left = Serial.read();
    crab_right = Serial.read();

What UKHeliBob said, but also with that bit of code on the Arduino...

while(Serial.available() < 1);

I think you have a type-o there. If Serial.available() is less than 1 it must be 0 (AFIK the return is unsigned). If Serial.available() returns zero then there isn't any available bytes in the serial buffer, thus an immediate call to Serial.read() would most likely return garbage. Me thinks you want greater than instead of less than, and make sure your Arduino code is trying to read exactly what your Processing code is sending. No more, no less.

Me thinks you want greater than instead of less than

?
Did you miss the semicolon?

AWOL:

Me thinks you want greater than instead of less than

?
Did you miss the semicolon?

D'oh, yeah I did. Silly me for not expecting a busy loop to halt all operation until a byte is registered coming in through the serial port.

Sorry about that.