Hi Terrance, sorry this took so long for me to respond! I literally just saw this thread, and I’ll do my best… hit me up at email@example.com too if email’s easier for you too…
Communicating from the Arduino to the TouchShield is kind of manual right now. It’s easy to pass unsigned chars one at a time, because the serial.read() function grabs one char at a time, which is one byte. The catch is that integers (ints) are 2 bytes long.
So in order to send a full int from the arduino, you have to send one part first, then the second part. Chris figured out how to get this working in the Pin Visualizer project (http://www.liquidware.com/projects/8/Pin+Visualizer), and I’ve adapted it for the Processing graphics library code too (Antipasto Hardware Blog: Processing ported to an Arduino + TouchShield ported from Processing).
On the TouchShield, I use this code:
int getValue = (Serial.read() << 8) + Serial.read();
This first calls Serial.read() to get the high bit. The “<< 8” means bit shift the byte up (to the left) 8 bits. Then, it calls Serial.read() again to get the low byte. And finally it adds both together. It kind of looks like this:
First get byte 1, the high byte:
Shift it left 8 bits
Then get byte 2, the low byte:
Add that to the left-shifted byte:
And on the Arduino, I’ll use this code:
unsigned char lowByte, highByte;
unsigned int val;
//set val to something
lowByte = (unsigned char)val;
highByte = (unsigned char)(val >> 8);
That kind of does the inverse behavior. First it take val, and sets lowByte to only the lower or rightmost 8 bits. Then, it sets highByte to the upper or leftmost 8 bits (by bitshifting it down 8 bits first). Then, it prints the high byte, followed by the low byte. I throw a delay(1) in between just to give the Arduino and TouchShield time to separate the two sends from each other.
So if val looks like:
It sets lowByte to:
It then shifts val to the right by 8 bits to get:
Which it then grabs only the lower 8 bits of:
Which get stored as highByte
Because I send highByte first, and the send lowByte, that means the TouchShield has to know that the first thing it gets will be the highByte. So that’s why the first byte the TouchShield gets is shifted left by 8 bits!
Phew. That was really long… I’m sorry
If you send me some code to look at, I can also help debug it too…