So I have a project where an xbee receives a number from an OpenCV program and then sends it to another Xbee which is connected to an Arduino Mini. I know the OpenCV part works but I am having problems with the Arduino understanding what the XBee is receiving. The Xbee sends the number as a most significant and least significant bit. The Arduino has a hard time getting all the data (even with a header). Sometimes I get the full resolution, sometimes I don't, and sometimes I get phantom data.
I have gotten the Arduino to send data through an Xbee to a computer but not a computer to the Arduino via the XBee.
Sure, This is the code that woks most of the time. The CV program send the data as LSB and MSB with the header of !. Can I send this as a number larger than 255? It would make it easier if I could. I was also thinking I might want to set up the programs to send and receive only when they needed. Right now it just broadcasts and receives the data constantly.
Now I am no longer getting phantom numbers!! But still having problems. Data should be !(33) 88 2. It keeps reading the ! for the LSB and sometimes the MSB. Sometimes the 88 is repeated as well.
I'm not sure I understand that string. Your code is written to accept exactly three characters, a !, a LSB, and an MSB. But I count 10 characters there.
Sorry that is the output of Serial.println.
the input is ! LSB MSB. The values for those should be !=33, LSB =88 MSB = 2. I am sending the same data over and over again to see if it works first before I start sending the actual data from the CV program.
Ah, so your serial data is being sent by a program and not being typed in a terminal program? Ok, that makes sense. It's just sending 33, 88, 2, 33, 88, 2, over and over (for now), right?
Now my guess is that the sending software is sending so fast that you are losing data. Printing all that HUZZAH! stuff is slow, and by the time loop is called again, you probably have lost some incoming bytes.
Use my second recommendation (above) to make sure the stream stays in sync.
Okay I fixed the program I have the Arduino send a Z when the CV program receives the Z it sends the data. When the Arduino gets data it that does not fit the things I want it sends a Z.
Messy but here it is:
val = Serial.read();
if (val != -1) {
if (Serial.available() > 2) {
getByte = Serial.read();
if (getByte == '!'){
while (Serial.available() == 0);
LSB = Serial.read();
while (Serial.available() == 0);
MSB = Serial.read();
Serial.println(LSB,DEC); // just to check the data, will remove later
Serial.println(MSB,DEC);// see above
}
}
}
if (val = -1){
Serial.print('Z');
}
delay(50); //lag time for buffer
On the OpenFrameWorks/CV end
if (header == 'Z'){
printf("Z is recieved");
serial.writeByte('!');
serial.writeByte(numLSB);
serial.writeByte(numMSB);
}