Xbee Series 2 Delay issue

Hi All,

For my system, I am using xbees S2 to transmit a condition in a timing sensitive application. I have an Arduino Mega receiving from Serial MIDI information at 31250 baud rate. Then that information is processed by the arduino and it sends AtRemoteCommand to 4 xbees as end devices (Arduinos UNO with Xbee Shield from SparkFun), which turns their digital outputs LOW or HIGH depending on the MIDI information sent by the Mega. The xbee in the Mega is configured as API 2 Coordinator, and the end devices in AT mode. I am using Andrew Rapp API Library.

My problem is about velocity when changing from one end device address to another. It took almost a second. And I have tried a lot of things but I haven't succedd in getting thay delay off yet.

In X-CTU I have tried some firmwares (XB24-B Zigbee and XB24-ZB Zigbee) but it seems to be the same thing. The end devices are also set not to sleep (SM=0), and CTS and RTS are enabled. BaudRate=38400. JN=1. ZS=0. RO=1 (also tried 0). And using as outputs DIO0 to DIO5.

I also tried dissabling response from Xbees.
In broadcast mode, delays were even worst.

I also tried not ussing the library, instead 'Serial.write'ing each of the HEX chracters to see if that improves the velocity, but I realised that the library isn't the problem because it was the same delay.

My question is, are this delays problem of the code, library, or is it the xbee itself?
I can't get my application working with this delay, as it has to turn on and off according to the music.

I attached my program if needed to help me.
In the loop I get the MIDI command and call to the playNote function where I decide to which end device the information will be sent, which digital output, and which state (HIGH or LOW) of it, in every remote end device will be set.

Thanks in advance!

#include <XBee.h>

byte inbyte, note, velocity;
int action=2,enddevice;
long value=0;
XBee xbee = XBee();

uint8_t d0Cmd[] = { 0x44,0x30};
uint8_t d0Value[] = { 0x05 };

uint32_t end1=0x40564D71;  //Xbee 1 DL
uint32_t end2=0x40564D72;  //Xbee 2 DL
uint32_t end3=0x40564D73;  //Xbee 3 DL
uint32_t end4=0x40564D74;  //Xbee 4 DL

uint32_t addressH=0x0013A200;  //Set DH of Xbees

XBeeAddress64 remoteAddress1 = XBeeAddress64(addressH, end1);
XBeeAddress64 remoteAddress2 = XBeeAddress64(addressH, end2);
XBeeAddress64 remoteAddress3 = XBeeAddress64(addressH, end3);
XBeeAddress64 remoteAddress4 = XBeeAddress64(addressH, end4);

RemoteAtCommandRequest remoteAtRequest1 = RemoteAtCommandRequest(remoteAddress1, d0Cmd, d0Value, sizeof(d0Value));
RemoteAtCommandRequest remoteAtRequest2 = RemoteAtCommandRequest(remoteAddress2, d0Cmd, d0Value, sizeof(d0Value));
RemoteAtCommandRequest remoteAtRequest3 = RemoteAtCommandRequest(remoteAddress3, d0Cmd, d0Value, sizeof(d0Value));
RemoteAtCommandRequest remoteAtRequest4 = RemoteAtCommandRequest(remoteAddress4, d0Cmd, d0Value, sizeof(d0Value));

RemoteAtCommandRequest remotes[4]={remoteAtRequest1,remoteAtRequest2,remoteAtRequest3,remoteAtRequest4};

void setup() 
{
  Serial3.begin(38400);
  Serial1.begin(31250);
  Serial.begin(38400);
  xbee.setSerial(Serial3);
}

void loop () 
{
  if (Serial1.available() > 0)
  {
    inbyte = Serial1.read();
    delayMicroseconds(300);
  }
  if ( (action==0)&&(note==0) )
  {
    note=inbyte;
    playNote(note, 0);
    note=0;
    velocity=0;
    action=2;
  }
  else if ( (action==1)&&(note==0) )
  {
    note=inbyte;
  }
  else if ( (action==1)&&(note!=0) )
  {
    velocity=inbyte;
    playNote(note, velocity);
    note=0;
    velocity=0;
    action=2;
  }
  else if (inbyte== 144)
  {
    action=1;
  }
  else if (inbyte== 128)
    action=0;
}

void playNote(byte note, byte velocity)
{
       static byte endDevices[] = { 0, 0, 1, 2, 3, 4 };
       static int notes[] = { 0, 0, -23, -35, -47, -59 };
       int idx = note / 12;
       if ((idx > 0) && (idx < sizeof(endDevices)))
       {
          enddevice = endDevices[idx];
          note += notes[idx];
       } 
       else 
       { 
           enddevice = 0; note = 0; 
       }
      if (velocity >10)
          d0Value[0]=0x05;
      else
          d0Value[0]=0x04;
      if (note > 0)
          SendXbee(note, d0Value, enddevice);
}

void SendXbee(byte note, uint8_t* d0Value, int enddevice)
{
  d0Cmd[1]=note+47;
  remotes[enddevice].setCommand(d0Cmd);
  remotes[enddevice].setCommandValue(d0Value);
  xbee.send(remotes[enddevice]); 
}

You should write some code to get the 16bit network addresses for all of your end devices and address them with that instead of the 64bit. Albeit the response time probably won't increase to the response level you want. Robert Faludi has a great book that explains what happens when the MAC address is used instead of the network address pg. 126. Basically the network will send a few broadcasts to figure out the desired devices network address and then once it does that then the original message will be sent to the device. If you start to get a big network this will create an unmanageable amount of overhead and there will be packet crashes and timeouts and some other fun stuff. Also XBees IMO are designed to build a rock solid network that will sacrifice speed in order to create and maintain network robustness. Look up posts from Jack Christensen and draythomp they have a lot of knowledge regarding xbees. Faludi's book is called Building wireless sensor networks get it if you don't have it.
Cheers!

The xbee in the Mega is configured as API 2 Coordinator, and the end devices in AT mode. I am using Andrew Rapp API Library.

Why would you put the receivers in AT mode, and then send them API packets?

How much delay is tolerable? How is the delay being measured?

I'm sure the problem isn't with the library, I use it a lot and have not observed this issue.

If I didn't intend to sleep the XBees, I'd use the Router firmware rather than End Device.