Sending and Receiving 10 Bit integers to/from Arduino and Processing

I am trying to send and receive 10 bit integers (the range of analogRead()) first from processing to Arduino and then arduino will reply back the same number to processing. I am aware that Serial.write and both send and receive 8 bit (1 Byte) values and I have that working. Now want to add 2 more bits to my Byte. I am sure I am not the first person to ask this.

The idea of bit-shifting is very interesting and I think it will solve my problem, however I need help on doing this; preferably an example code would be nice. Or maybe help me build on top of the code I already have.

Thanks a bunch,


import processing.serial.*;
Serial myPort; 

int inByte = 0; 
boolean proceed = false; 
boolean sending = true; 

void setup(){
  String portName = Serial.list()[0]; 
  // change [0] for the Arduino serial port 
  myPort = new Serial(this, "COM5", 57600); 

void draw() {
  // first contact 
  while (!proceed) {
    if ( myPort.available() > 0) {
      inByte =; 
      if (inByte == 'A') {
        println("Received " + inByte); 
        proceed = true; 
  // send and receive test 
  while (proceed) {
    if (!sending) { 
      if ( myPort.available() > 0) {
        inByte =; 
        println("Received " + inByte); 
        sending = true; 
    if (sending){
      sending = false; 


int inByte = 0; 

void setup() {
  // setup Serial comm with computer 
  Serial.print('A'); // send letter "A" 

void loop() {
  if (Serial.available() > 0) {
      inByte =; 

FardinB: Now want to add 2 more bits to my Byte

A byte has 8 bits, you can't add two more bits, you have to send another byte.

int x = 1023;
byte low = x;
byte high = x >> 8;

Or highByte and lowByte.

This is great!

The problem here is how to assemble it in Processing and Arduino? Its a 2 way comm link


Reverse the technique in reply #1

How? Example?

Try this, it lets you treat an integer as two bytes:

union ByteInt {
  int val;
  struct {
    // Access to the bytes of 'val'
    byte lo, hi;
  } bytes;
  ByteInt& operator=(int n) { val = n;  }
  operator int() const { return val;      }

// examples
void sendInt(int n)
  ByteInt b;
  b = n;

int readInt()
  ByteInt b;
  b.bytes.lo =;
  b.bytes.hi =;
  return b;

FardinB: How? Example?

Think about it for more than 1 minute. You took the low byte first. You then took the high byte by shifting to the right 8. So, to reconstruct you do it in reverse. The only thing extra needed is a logical OR or addition.

Thanks a lot fungus...

If you are sending data packets of more than one byte then you should provide some way for the receiver to work out where each message starts and ends. If you naively send out alternate low and high bytes then any serial error which caused a byte to get lost would lead to the receiver getting out of step - in this case, swapping the high byte from one sample with the low byte from the adjacent sample.

One way to make this transfer more robust is to encode the numbers as decimal strings and use a textual separator between the values (comma, linefeed etc).

Another way is to encode the value so that you can tell whether a given byte is the high byte or the low byte. For example if you reserve one bit in each byte to tell you which is the high byte, then you still have seven bits for your data so a two byte message would support up to fourteen bits of data. With this approach you would use simple bit shifting and bit masking to encode and decode the number to/from each two byte message.