So I’m using two Arduinos sending sensor data (3axis accel, 3axis gyro) over two XBees to another XBee + USB shield which is then piping the data into Max/MSP.
At first I was using something similar to the built-in ‘VirtualColorMixer’ patch, by sending the data, then a “,”, then more data.
I added to that tags for each arduino (1 or 2) and then for each sensor type (Ax, Ay, Az),
So I had something like this:
Serial.print("1 ");
Serial.print("ax ");
Serial.print(analogRead(ax));
Serial.println();
I then shortened that to this:
Serial.print("1 ax ");
Serial.print(analogRead(ax));
Serial.println();
But then I realized that I have two Arduinos piping data in at the same time, and it’s conceivable that I could ‘cross the streams’ and break all the tagging. (if one device sends its data value before a line break from the next device it would break the parsing side of things).
Then after a suggestion from Grumpy Mike I used a hex byte to label the device (rather than the 4 bytes I’m using there). So 0x11 would be device one, sensor1, 0x24 would be deice two, sensor4 etc…
So I changed the code to look like this:
Serial.print(char(0x11) + AccX);
Serial.println();
I also moved all of the read functions to the start of the void loop to avoid printing in the middle of reading and delaying when the analog data is sampled from (using some software filtering that compares previous positions, so I would imagine this would improve accuracy).
The problem I’m having is that I don’t seem to be getting the hex value in serial monitor (or Max/MSP). In Max after grouping and parsing things a bit I’m getting the 6 sensor values in a row, but nothing in front of each… In serial monitor I’m getting something similar.
Is there something wrong with my serial.print code there? Is this the most thorough way to avoid ‘crossing the streams’ from two devices sending serial data? (my println is on a separate line, and I guess that’s my footer value at the moment, should I remove that and presume that everything before a hex character is the full data?)
Here is all of the code:
#define aX A0
#define aY A1
#define aZ A2
#define gX A6
#define gY A7
#define gZ A3
int AccX;
int AccY;
int AccZ;
int GyroX;
int GyroY;
int GyroZ;
void setup()
{
Serial.begin(115200);
}
void loop()
{
AccX = analogRead(aX);
AccY = analogRead(aY);
AccZ = analogRead(aZ);
GyroX = analogRead(gX);
GyroY = analogRead(gY);
GyroZ = analogRead(gZ);
Serial.print(char(0x11) + AccX);
Serial.println();
Serial.print(char(0x12) + AccY);
Serial.println();
Serial.print(char(0x13) + AccZ);
Serial.println();
Serial.print(char(0x14) + GyroX);
Serial.println();
Serial.print(char(0x15) + GyroY);
Serial.println();
Serial.print(char(0x16) + GyroZ);
Serial.println();
}