We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Edit: Code now works (See Second Comment). Also this code is just the serial communication parts, no robot code.
Evening all,
I am building a haptic robot arm system (input arm controls an output arm). I am taking arm integer data from a 2560 and sending it to a second arm (arduino UNO).
I am getting good stable data from the first arm in my processing sketch, breaking it into bytes in processing, sending those bytes to the second arm from processing, reassembling them to integer values on the arduino UNO, and echoing the integer values back to a processing sketch.
The problem is the data from the second arm is unstable. sometimes it sends the right data, sometimes not. I was hoping you could look at my code and maybe tell me where the instability is coming from.
Thank you!
Data:

PROCESSING CODE
// serialEvent method is run automatically
// whenever the buffer receives a linefeed byte
import processing.serial.*;
//MSB =Most Significant Byte
//LSB =Least Significant Byte
int memorySize = 20; //size of the robot onboard memory array
Serial leftInputArm; //port for input data
Serial leftOutputArm; //port for output of data, also receives state information
String [] comPortList; //list of comports
int[] centralArray = new int[20]; //main storage array for data
int [] leftInputArray = new int[20]; //integer values for the input arm
byte [] leftOutputArrayMSB = new byte[20];  //place for the most significant bytes
byte [] leftOutputArrayLSB = new byte[20];  //place for the least significant bytes
int baud = 9600;
int haveData = 0; //we dont have data to pass yet...
void setup() {
  SetupComs();   //get the serial ports
  background(0); //draw a black box.
}
void draw()
{
  if (haveData > 0) //make sure we have recieved some data from the input arm
    LeftOutArmTx(); //now that we have data call the transmit function
  delay(100);
}
void LeftOutArmTx()
{
  println();
  print("TO Left Arm: ");
  int n;
  leftOutputArm.write(byte(9)); //send header byte
  for (n=0; n<memorySize; n++)
  {
    leftOutputArm.write(leftOutputArrayMSB[n]);       //send MSB
    print(" MSB" + n + ": " + leftOutputArrayMSB[n]); //check what we sent
    leftOutputArm.write(leftOutputArrayLSB[n]);       //send LSB
    print(" LSB" + n + ": " + leftOutputArrayLSB[n]); //check what we sent
  }
  leftOutputArm.write(byte(6)); //send tail byte
  println();
  delay(10);
}
void serialEvent(Serial myPort)
{
  if (myPort == leftInputArm) {
    byte MSB;
    byte LSB;
    String myString = myPort.readStringUntil('\n'); // read the serial buffer
    if (myString != null) // if we get any bytes other than the linefeed
    {
      println();
      myString = trim(myString); // remove the linefeed
      int inputArray[] = int(split(myString, '\t')); // split the string at the tabs
      int count = inputArray.length;                 //and convert the sections into integers:
      haveData = 1; //we now have data to send
      print("FROM Left Input Arm: ");
      for (int i=0; i<count; i++)
      {
        leftInputArray[i] = inputArray[i]; //trasnfer the Int from the temporary array
        LSB = byte(inputArray[i]); //grab the LSB
        MSB = byte(inputArray[i]>>8); //shift the Int and grab the MSB
        if (MSB < 0) MSB*=-1; //dont be negative
        if (LSB < 0) LSB*=-1; 
        leftOutputArrayMSB[i] = MSB; //load the byte into the proper array
        leftOutputArrayLSB[i] = LSB;
        print(i + ": " + leftInputArray[i] + " "); //show the input data
        //print(i + " MSB: " + leftOutputArrayMSB[i] + " "); //check what we have
        //print(i + " LSB: " + leftOutputArrayLSB[i] + " ");
      }
      println();
    }
  }
  if (myPort == leftOutputArm)
  {
    String myString = myPort.readStringUntil('\n'); // read the serial buffer:
    if (myString != null) // if we get any bytes other than the linefeed:
    {
      println();
      myString = trim(myString); // remove the linefeed
      int inputArray[] = int(split(myString, '\t'));// split the string at the tabs and convert the sections into integers:
      int count = inputArray.length;
      print("--->FROM Left Arm: ");
      for (int i=0; i<count; i++)
      {
        centralArray[i] = inputArray[i]; //load the data into the central array
        print(i + ": " + inputArray[i] + " "); //what did we get?
      }
      println();
    }
  }
  myPort.clear(); //clear the buffer when we are done getting data
}
//Open COM Port for Communication
void SetupComs()
{
  comPortList = Serial.list();
  if (comPortList.length>0) {
    leftOutputArm = new Serial(this, Serial.list()[0], baud);
    leftOutputArm.bufferUntil('\n');
  }
  if (comPortList.length>1) {
    leftInputArm = new Serial(this, Serial.list()[1], baud);
    leftInputArm.bufferUntil('\n');
  }
}
ARDUINO UNO CODE
int memorySize = 20; // Set this equal to coreInfo length!
int coreInfo[20]; //this is set the number of things you want to report.
//int dummyData[] = {100,200,300,400}; //in case you want check the data stream, use this.
void setup()
{
  Serial.begin(9600);
}
void loop()
{
  ReadData();
  SendIntArray(coreInfo);
}
void SendIntArray(int Values[])
{
  for (int i = 0;i<memorySize;i++)
  {
    if(Values[i] < 0) Values[i]=0; //dont send negatives, it hurts my feelings
    Serial.print(Values[i]); //send the Int as string data
    Serial.print("\t"); //delimit for great justice
  }
  Serial.print('\n'); //tag the ass end
  delay(10); //give it time to breathe
}
void ReadData()
{
  byte tmp; //temporary byte storage
  byte MSB; //Most Significant Byte
  byte LSB; //Least Significant Byte
  int value; //assembly area for our bytes
  int n = 0; //reset the counter
  while(Serial.available() > 0) //check if there is data
  {
    tmp = Serial.read(); //read a byte from serial buffer
    if(tmp = 9) //check for header value: coordinate with processing sketch value
    {
      //now that we now we are at the head of the data stream:
      for(n = 0; n < memorySize; n++)
      {
        MSB = byte(Serial.read()); //data is sent in pairs, MSB first
        LSB = byte(Serial.read());
        value = MSB; //set the MSB data
        value = value<<8; //shift it left to make room for LSB
        value |= LSB; //set the LSB in place
        if(value >= 0) //positive values only please
          coreInfo[n] = value; //load the data into the core memory array
      }
    }
    else if(tmp == 6) break; //tail byte, this is our exit!
  }
}
Answers
I finally got it to work, code below:
PROCESSING CODE:
ARDUINO UNO CODE: