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:
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
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 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.