Hi All,
Now for my question. 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.
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. My program reads the first message and get the 16bit address of every end devicen and sets its 16bit address and the 64bit address in the RemoteAtCommand. I also have tried in broadcast mode, but delays were even worst.
Library: Andrew Rapp.
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.
Another weird issue is that if I disable the response, the communication failed. So I keep it not only for debbuging but also for this problem.
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));
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;
if(enddevice==1)
{
remoteAtRequest1.setCommand(d0Cmd);
remoteAtRequest1.setCommandValue(d0Value);
xbee.send(remoteAtRequest1);
}
if(enddevice==2)
{
remoteAtRequest2.setCommand(d0Cmd);
remoteAtRequest2.setCommandValue(d0Value);
xbee.send(remoteAtRequest2);
}
if(enddevice==3)
{
remoteAtRequest3.setCommand(d0Cmd);
remoteAtRequest3.setCommandValue(d0Value);
xbee.send(remoteAtRequest3);
}
if(enddevice==4)
{
remoteAtRequest4.setCommand(d0Cmd);
remoteAtRequest4.setCommandValue(d0Value);
xbee.send(remoteAtRequest4);
}
}