I’m successfully sent single int values from Processing to Arduino, but now I need to sent a pair of ints (both having values between 0 and 90) together and I’m not sure how to do that.
Reading the docs, I see I can send byte/byte[]/int/char/String types.
One idea is to send string containing the two ints concatenated by a comma character,
but am not 100% sure how to convert the data to a String and split it in Arduino.
serial.write(intValue1+","+intValue2);
Another thing I was thinking was packing two ints into a byte[] somehow, but I haven’t worked with bytes much, so any tips on getting started would be helpful.
If you’re sending pairs of ints, or any number of bytes, it seems you may want to consider a simple serial protocol. There are a few questions here specifically about serial command protocols for embedded systems, for example:
One suggestion is starting off your message with a header byte, usually something outside of your data range if possible (>90 in your case?). The following code has not been tested; it’s just an idea, but it should run.
Another option would be to acknowledge the receipt of each byte (i.e. receive header, respond with a special “ack header” byte, receive next byte, respond with “ack byte one”, etc.). This is slower but it may fine for you.
Finally, a couple key points from the questions I linked to above:
Consider sending a checksum (i.e. sum of bytes) at the end of the message. The Arduino would add up the header, byte 1 and byte 2. If it doesn’t match the check sum, then it could respond with a failure, or send again message.
Rex’s answer to question [2] sums up what a message protocol might look like: