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.
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.
Library from Andrew Rapp
Thanks in advance!
#include
byte inbyte, note, velocity, var=0x04, sal=0x30, DL1, DL2, DL3, D16H=0xFF, D16L=0xFE, D16H1=0xFF, D16H2=0xFF, D16H3=0xFF, D16L1=0xFE, D16L2=0xFE, D16L3=0xFE, mode=0x44;
int action=2, i=2, enddevice;
long value=0, sum;
XBee xbee = XBee();
RemoteAtCommandResponse remoteAtResponse = RemoteAtCommandResponse();
void setup()
{
Serial3.begin(38400); //Serial for xbee communication
Serial1.begin(31250); //MIDI In
Serial.begin(38400); //For debbug
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)
{
if (note >= 24 && note <36)
{
enddevice=1;
note=note-23;
}
else if (note>=36 && note<48)
{
enddevice=2;
note=note-35;
}
else if (note >=48 && note<60)
{
enddevice=3;
note=note-47;
}
else if (note>=60 && note<72)
{
enddevice=4;
note=note-59;
}
else
{
note = 0;
}
if (velocity >10) //HIGH xbee output
var=0x05;
else
var=0x04; //LOW xbee output
if (note > 0)
{
while(i<5) //Just for getting 16bit address for the first time
{
enddevice=i;
SendXbee(note, var, enddevice);
i++;
}
i=10;
SendXbee(note, var, enddevice);
}
}
void SendXbee(byte note, byte var, int enddevice)
{
if(enddevice==1)
{
DL1=0x56; //3 bytes of 64bit xbee address
DL2=0x4D;
DL3=0x74;
D16H=D16H1; //2 bytes of 16bit xbee address
D16L=D16L1;
}
else if(enddevice==2)
{
DL1=0x91;
DL2=0xA1;
DL3=0xBB;
D16H=D16H2;
D16L=D16L2;
}
else if(enddevice==3)
{
DL1=0x91;
DL2=0xA2;
DL3=0x8B;
D16H=D16H3;
D16L=D16L3;
}
mode=0x44; //Letter 'D'
sal=note+47; //Get which output to set low or high
sending(var, sal, DL1, DL2, DL3, mode, D16H, D16L);
}
void sending(byte var, byte sal, byte DL1, byte DL2, byte DL3,byte mode, byte D16H, byte D16L)
{
Serial3.write(0x7E); //Start byte
Serial3.write(0x00); //Length MSB
Serial3.write(0x10); //Length LSB
Serial3.write(0x17); //RemoteAtCommand
Serial3.write(0x01);
Serial3.write(0x00); //Destination 64bit addres MSB
Serial3.write(0x7D); //Escape
Serial3.write(0x33); //0x13 XOR 0x20 because of escape chracters
Serial3.write(0xA2);
Serial3.write(0x00);
Serial3.write(0x40);
Serial3.write(DL1); //3 bytes 64bit dest addres LSB
Serial3.write(DL2);
Serial3.write(DL3);
Serial3.write(D16H); //2 bytes 16bit dest address
Serial3.write(D16L);
Serial3.write(0x02); //Apply changes
Serial3.write(mode); //Letter 'D'
Serial3.write(sal); //Number of digital output
Serial3.write(var); //0x04 or 0x05 if LOW or HIGH
long sum = 0x17 + 0x13 + 0x01 + 0xA2 + 0x40 + DL1 + DL2 + DL3 + D16H + D16L + 0x02 + mode + sal + var;
Serial3.write(0xFF - (sum & 0xFF)); //checksum
if (xbee.readPacket(200))
{ //Wait for a response
// got a response
// should be an AT command response
if (xbee.getResponse().getApiId() == REMOTE_AT_COMMAND_RESPONSE)
{
xbee.getResponse().getRemoteAtCommandResponse(remoteAtResponse);
if (remoteAtResponse.isOk())
{
Serial.print("Command was successful!");
uint16_t senderShortAddress = remoteAtResponse.getRemoteAddress16(); //Get 16bit address of end device
byte D16h = (senderShortAddress >> 8);
byte D16l = (senderShortAddress & 0xFF);
if(DL3==0x8B) //Check which end device that 16bit address belongs to
{
D16H3=D16h;
D16L3=D16l;
}
else if(DL3==0xBB)
{
D16H2=D16h;
D16L2=D16l;
}
if(DL3==0x72)
{
D16H1=D16h;
D16L1=D16l;
}
}
else
{
Serial.print("Command returned error code: ");
Serial.println(remoteAtResponse.getStatus(), HEX);
}
}
else
{
Serial.print("Expected Remote AT response but got ");
Serial.print(xbee.getResponse().getApiId(), HEX);
}
}
else if (xbee.getResponse().isError())
{
Serial.print("Error reading packet. Error code: ");
Serial.println(xbee.getResponse().getErrorCode());
}
else
{
Serial.print("No response from radio");
}
}