How to Send large numbers from Processing to Arduino?

Hey guys. I'm trying to figure out a way to send some numbers like mouseX and mouseY from processing to Arduino through Serial. Problem is: whenever a send a number larger than 127 it gets messed up and I need to send numbers in a range between 0 and 900. I tried to "mask" the number and send 1 byte and then another. But I don't know how to get this data correctly with arduino. When I use Serial.read(); it reads 1 byte. I use it again, it reads the other incoming byte. How do I put them together to recover the original number?

What I've got so far with processing is this. Don't know if it's right:

Serial myPort;
byte posX;

void setup() { 
 size(800,600);
 String portName = Serial.list()[0];
 myPort = new Serial(this, portName, 9600);
}

void draw() {
myPort.clear();
myPort.write(mouseX & 0xff);
myPort.write(mouseX >> 8);
}

Anyone could tell me if everything's alright with my code for sending the numbers and help me to find a way to read this correctly with Arduino?

If you read the write() documentation, you'll see that myPort.write(mouseX); will send your integer.

If I remember correctly mouseX is an int and as int are stored as 32 bits of information - although you do masking, you are sending twice an int, so 8 bytes not just two bytes.

An easy way to see what's coming is to send something you know like

int v = 0xDEADBEEF;
myPort.write(v);

may be only once or when you press a key if you don't want to flood the Serial line and write the matching arduino code reading incoming bytes and printing them as HEX

That will show you what happens

Show your Arduino code.

You need a temporary buffer where you can store the first byte that you receive; once you receive the second one, you can combine them into the final number.

Note that your Arduino code needs to be able to identify if it's the first byte or the second byte. If you srick with binary, there are two ways that I can think of:
1)
A timeout mechanism; if the second byte is not received within a certain time, assume that the byte that was received first was actually the second byte and throw the current value away and start over
2)
Identification in the data bytes; use e.g. the most significant bit to identify if the received byte is the first byte (msb==0) or the second byte (msb==1). In that case you can only send 7 bits (and a total of 14 actual bits for the value, but that is sufficient).

If you're prepared to send the number as text, you can read the Serial Input Basics - updated thread to get ideas.

If you have more requirements (like sending keystrokes, mouse clicks etc as well), you will have to design a protocol.

PS
1)
After reading J-M-L's reply, the above is based on the fact that you're sending bytes (which your code implied)
2)
No experience with Processing software.

processing is most likely not sending one byte after doing the masking (an int masked or shifted still remains an int, does not becomes a byte) - I think this is why OP is getting confused

Hi, guys. Thanks for your answers. I found out a way of doing my task by sending my number as text as sterretje said. I converted all my numbers to String and sent them using myPort.write(“1234567,”); using the comma as a separator to warn the arduino that the first number ends there.
My Arduino code was the one for receiving several characters that was explained here

Here’s my processing code

num = 1234567;
str = str(num)+",";
  myPort.clear();
  myPort.write(str);
  myPort.write('\n');

And my Arduino code, as said, is quite like the one I mentioned, only difference is that I search for the comma on my showNewData() function:

void showNewData() {
 if (newData == true) {
  str = String(receivedChars);

for(int i = 0; i < str.indexOf(',') ;i++){
  str += receivedChars[i];
}

num = str.toInt();

str = "";

 newData = false;
 }
}

May not be the best solution, but it works fine on my application. Thanks for your help

asallan3:
And my Arduino code, as said, is quite like the one I mentioned, only difference is that I search for the comma on my showNewData() function:

I understand the need to search for the comma - but why do it in a function whose purpose is just to show new data. Put the parsing code in its own function.

...R

Why the hell (excuse my French) would you want to build a String (capital S) when you already have the message in a c-string apparently?