XBee serial data - odd behaviour, why?

Hi! I've just set up 3 XBee series 2.5 modules using X-CTU. Everything went pretty fine following the instructions in this document (http://www.embedded.arch.ethz.ch/xbee-setup.pdf - very good & clear!).

I am using sparkfun's xbee explorer for my coordinator module, and 2 duemilanoves with xbee shields for the routers. One router has a potentiometer (knob) attached, the other an ADXL322 accelerometer. When in wireless mode, I am getting odd readings from the accelerometer but not from the pot. If I have the arduino with the accelerometer connected via usb, whether or not I also have the xbee shield installed, I get correct readings.

So - wireless communication from 2 routers to the coordinator works, but for some reason the data flow from the accelerometer is affected by the xbee communication. Does anyone know why, and how to solve this?! I am hoping it is something that can be solved either in the firmware setup of the xbee, or in my arduino code....

Any suggestions most welcome, thanks!

Here is a sample of the readings:

.A 113 126 125
.A 113 126 124
.A 113 127 124
.A 113 126 124
.A 113 126 124
.A 113 126 124
.A 114 127 125
.A 113 126 125
.A 113 126 124
// up to hear they are correct, then it seems to reset to 0 and work its way up again...??
.A 113A 0 0 2
.A 5 6 7
.A 12 15 14
.A 21 25 23
.A 31 36 33
.A 42 48 43
.A 54 61 55
.A 65 73 67
.A 75 85 79
.A 87 98 91
.A 98 110 101
.A 104 117 109
.A 108 121 A 0 0 3
.A 5 6 8
.A 12 14 14
.A 21 24 23
.A 31 36 33
.A 42 49 44
.A 53 61 55
.A 64 73 67
.A 76 85 80
.A 87 99 92

Here's the arduino code:

// Define the number of samples to keep track of. The higher the number,
// the more the readings will be smoothed, but the slower the output will
// respond to the input. Using a #define rather than a normal variable lets
// use this value to determine the size of the readings array.
#define NUMREADINGS 10

int readingsX[NUMREADINGS]; // the readings from the X-axis analog input
int totalX = 0; // the running total of X
int averageX = 0; // the average for X
int readingsY[NUMREADINGS]; // the readings from the Y-axis analog input
int totalY = 0; // the running total of Y
int averageY = 0; // the average for Y
int readingsZ[NUMREADINGS]; // the readings from the Z-axis analog input
int totalZ = 0; // the running total of Z
int averageZ = 0; // the average for Z

int index = 0; // the index of the current reading

int inputXPin = 3; //X-axis input
int inputYPin = 2; //y-axis input
int inputZPin = 1; //Z-axis input
int groundPin = 18; //analog input pin 4
int powerPin = 19; //analog input pin 5

void setup()
{
Serial.begin(57600); // initialize serial communication with computer
pinMode(groundPin, OUTPUT);
pinMode(powerPin, OUTPUT);
digitalWrite(groundPin, LOW);
digitalWrite(powerPin, HIGH);
for (int i = 0; i < NUMREADINGS; i++)
readingsX = 0; // initialize all the readings to 0

  • for (int e = 0; e < NUMREADINGS; e++)*

  • readingsY[e] = 0;*

  • for (int f = 0; f < NUMREADINGS; f++)*

  • readingsZ[f] = 0;*
    }
    void loop()
    {

  • totalX -= readingsX[index]; // subtract the last reading*

  • totalY -= readingsY[index];*

  • totalZ -= readingsZ[index];*

  • readingsX[index] = analogRead(inputXPin); // read from the sensor*

  • readingsY[index] = analogRead(inputYPin);*

  • readingsZ[index] = analogRead(inputZPin);*

  • totalX += readingsX[index]; // add the reading to the total*

  • totalY += readingsY[index];*

  • totalZ += readingsZ[index];*

  • index = (index + 1); // advance to the next index*

  • if (index >= NUMREADINGS) // if we're at the end of the array...*

  • index = 0; // ...wrap around to the beginning*

  • averageX = totalX / NUMREADINGS; // calculate the average*

  • averageY = totalY / NUMREADINGS;*

  • averageZ = totalZ / NUMREADINGS;*

  • averageX = map(averageX, 0, 1023, 0, 255);*

  • averageY = map(averageY, 0, 1023, 0, 255);*

  • averageZ = map(averageZ, 0, 1023, 0, 255);*

  • Serial.print("A"); // to identify as coming from accelerometer*

  • Serial.print(" ");*

  • Serial.print(averageX); // send it to the computer*

  • Serial.print(" ");*

  • Serial.print(averageY);*

  • Serial.print(" ");*

  • Serial.print(averageZ);*

  • Serial.println();*
    }

I would add a Serial.println() call in setup(), to see if somehow the Arduino is resetting. I don't see anything that should make it reset, but it might be happening.

Since you are keeping a running total of the values read, there is really no reason to save the individual values in an array. After populating the array, you never reference it again, except for the last element. You could simply replace the arrays with scalar variables to keep track of the last reading for x, y, and z.

Also, since you are not doing anything in loop when you have taken less than 10 readings, you could all a for loop to read each value 10 times. Then, there is no reason to keep track of the previous reading. After the for loop, send the data.

Thanks for the reply! I put in the Serial.println() call in startup, and guess what, it IS restarting all the time! I've put a 110ohm resistor across reset & 5v pins (as suggested as an alternative to cutting the reset solder trace on the board), but this doesn't stop the restart. I'll try rewriting the code as you say, but although that maybe better code I don't think that has anything to do with this problem, which must be to do with the XBee wireless transmission - or does anyone have other suggestions?

Here is the received data again, with "start" added in setup:

.A 113 126 124
.A 113 126 124
.A 113 126 124
.A 113 126 124
.A 113 126 124
.A 113 126 124
.A 113 126 124
.A 113 126 124
.A 113 126 124
.A 113 126 124
.A start
.A 0 0 2
.A 5 6 7
.A 12 14 14
.A 21 25 23
.A 31 36 33
.A 42 49 44
.A 53 61 56
.A 64 73 68
.A 76 86 80
.A 87 98 92
.A 98 110 102
.A 104 117 109
.A 1start
.A 0 0 2
.A 5 6 7
.A 12 14 13

Unless there is a hardware problem, the Arduino isn't even aware that there is a XBee attached. So, it's unlikely that the XBee is the problem.

Have you tried swapping which Arduino has the accelerometer attached? Maybe the problem is with the Arduino board itself.

On the other hand, since the reset seems to occur about every 10 readings, and your arrays are supposed to be able to hold 10 values, perhaps it's a code issue - not that anything is obvious.

Another thing you might notice is that the output from the map function is 1/4 of the input. Rather then a call to map, 3 times, simply divide the value by 4.

Thanks for your ideas - but problem is solved (at least for now) - I trawled again through the results of a forum search and found this page http://www.faludi.com/projects/common-xbee-mistakes/ which is excellent reference. He mentioned this in arduino mistakes:

"Sending continuously without any delay (try 10ms delay)"

I HAD had a delay earlier, but more like 1s which didn't seem to work so I removed it completely. Now with delay(10); added it works continuously.

Serial.print("A");
Serial.print(" ");
Serial.print(averageX); // send it to the computer
Serial.print(" ");
Serial.print(averageY);
Serial.print(" ");
Serial.print(averageZ);
Serial.println();
delay(10);
}

But why is this needed?