Arduinos, Xbee & parseInt

Hey everyone. I've been having a problem for a few days now and can't seem to figure out what's going on. My goal is this: a wiichuck will send data to my Arduino Micro and send it over the serial1 port to an Xbee in the following format "x,y,button1,button2" example "123,223,0,0". I can read the data just fine from the serial port. I can read it fine when I plug my second Xbee into my computer and view the data over the serial port. The problem comes up when I plug the second Xbee into my Arduino Uno. I'm trying to use parseInt to read the values sent from the micro. The data is being received and I can assign them to an array, what happens is the data is not in the right place when I print the values to the serial port. Values will shift position when I monitor them from the Uno, like: "0,123,233,0" then "0,0,123,233".

I don't think the issue is that I'm printing to the serial while trying to read from it as well, since when I try to use the values to control motors and remove the serial printing, it don't work correctly. Is there a better way to pass the data over the serial ports than using parseInt? The Xbee's are Series1. Eventually I want to send more data over to the Uno and possibly even send data back and forth between the two.

Data transmiter
#include "Wire.h"
#include "WiiChuck.h"

WiiChuck chuck = WiiChuck();
int x, y, zButton, cButton;

void setup()
{
  Serial1.begin(19200);
  chuck.begin();
}

void loop() 
{
  chuck.update(); 
  x = map(chuck.readJoyX(),-100,109,0,255);
  y = map(chuck.readJoyY(),-111,102,0,255);
  if (x < 0)
  {
    x = 0;
  }
  else if(y < 0)
  {
    y = 0;
  }
  if (chuck.buttonZ) 
  {
     zButton = 1;
  } 
  else  
  {
     zButton = 0;
  } 
  if (chuck.buttonC) 
  {
     cButton = 1;
  }
  else  
  {
      cButton = 0;
  }
  Serial1.print(x);
  Serial1.print(",");
  Serial1.print(y);
  Serial1.print(",");
  Serial1.print(zButton);
  Serial1.print(",");
  Serial1.print(cButton);
  Serial1.print(",");
  Serial1.println();
}
Receiver 
void loop()
{
  if (Serial.available())
  {
    for(fieldIndex = 0; fieldIndex < 4; fieldIndex++)
    {
      values[fieldIndex] = Serial.parseInt();
    }  
    for(int i=0; i < fieldIndex; i++)
    {
      Serial.print(values[i]);
      Serial.print(", ");
    }
  }	
}

You need to send something like "<123,123,0,0>", and then read the data storing it when the < arrives, stopping (and using) the stored data when the > arrives.

.

#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
   Serial.begin(57600);
   // Other stuff...
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}

Yeah, so I tired that code and edited my transmitter to send the data like you said <x,x,x,x>. When I add a loop to print whats in the inData array, during the if statement to check started and ended , nothing is ever printed in the console, so I'm not sure that if statement is ever run. I don't fully understand what the code you are posted is doing, specifically checking if index is < 79 and inData[index] = '\0';.

I'd really like some input on the code I posted. It seems to mostly be working, I'm getting good values, but they are not being stored in the correct location of the array. There is not point sending the <> before the values as parseInt would just ignore them. I've tired changing the baud rate in case the speed was too much, but that hasn't made a difference. Could the problem be that I am reading from Serial and then printing data to the Serial to view them in the terminal console?

What I can't seem to find a straight answer on, is what happens when parseInt receives a non integer? It is returning a 0 or skipping to the next integer? I've read different people saying different things.

Could the problem be that I am reading from Serial and then printing data to the Serial to view them in the terminal console?

Partially, I suppose. It is not a good idea to use the same port to talk to two devices - the XBee and the PC.

What I can't seem to find a straight answer on, is what happens when parseInt receives a non integer? It is returning a 0 or skipping to the next integer?

The source code knows all. What parseInt() does is read the serial stream until a character that is not an integer digit arrives OR until there has been no data received on the port for a defined period of time. It then converts the data that it has stored to a long, and returns that value.

When I add a loop to print whats in the inData array, during the if statement to check started and ended , nothing is ever printed in the console

If you were using two different serial ports, either hardware or software, you could print on the port that the XBee is not connected to each character that is received from the XBee. Then, perhaps you'd see why the other Serial.print() statement(s) are not being executed (if indeed they aren't).