I am using a two Arduino setup with two XBee radios. One radio is sending data and the other is receiving data. I am sending three sensor readings following a protocol which uses three bytes for each sensor reading. Byte one is sensor information, byte two is data, and byte three is for error checking. When I run the set up for about 5 minutes I get approximately 47% errors in transmission. Once one error occurs, there seems to be an avalanche of errors. However, when I add delay in between sending bytes (100ms between bytes and 1s between sensor) it works perfectly. I've tried expanding the hardware buffer, but this didn't work.
delay(2000);
// Calcuate weather parameters for serial and web
calcWeather(); // Go calc all the various sensors
// send weather out to serial port
if (prevtemplm34_1 == templm34_1) // 2 in a row must be the same to send
{
Serial.write(0);
Serial.write(templm34_1);
Serial.write(templm34_1 ^ 0);
}
// send weather out to serial port
if(prevtemplm34_2 == templm34_2) // 2 in a row must be the same to send
{
Serial.write(1);
Serial.write(templm34_2);
Serial.write(templm34_2 ^ 1);
}
// send weather out to serial port
if(prevtemplm34_3 == templm34_3) // 2 in a row must be the same to send
{
Serial.write(2);
Serial.write(templm34_3);
Serial.write(templm34_3 ^ 2);
}
delay(2000);
The code for parsing received data is:
int readData ()
{
// There must be at least three available bytes to satisfy the protocol
// Otherwise, wait until three bytes have arrived to read anything
if (Serial.available() >= 3) {
// Read the next three byte in the serial buffer
incomingByteOne = Serial.read();
Serial.write("Byte One: ");
Serial.write(incomingByteOne);
Serial.println();
incomingByteTwo = Serial.read();
Serial.write("Byte Two: ");
Serial.write(incomingByteTwo);
Serial.println();
incomingByteThree = Serial.read();
Serial.write("Byte Three: ");
Serial.write(incomingByteThree);
Serial.println();
// XOR the first and second incoming bytes so that the result can be checked against the third error checking byte
byte xorValue = incomingByteOne ^ incomingByteTwo;
Serial.write("XOR VALUE: ");
Serial.write(xorValue);
Serial.println("Number of Errors: ");
Serial.println(numberOfErrors);
Serial.println();
Serial.println();
if(xorValue != incomingByteThree) {
numberOfErrors++; // Add one to the total count of number of errors in transmission
}
else {
// Mask for retrieving the five rightmost bits from incomingByteOne (00011111)
byte maskForSensorNumber = 0x1f;
// Shift incomingByteOne right 5 times in order to obtain the three bits representing the unit number
int unitNumber = incomingByteOne >> 5;
// AND incomingByteOne with the mask in order to retrieve only the five righmost bits representing the sensor number
int sensorNumber = incomingByteOne & maskForSensorNumber;
// Store the data for the correct sensor on the correct unit
data[unitNumber][sensorNumber] = incomingByteTwo*1;
}
}
}
What could be causing the transmissions to work fine with delay, but not otherwise?