Hello guys,
This is my setup. I have two arduinos(Mega2560,UnoR3). I am using these to make a RC plane with the help of XBee S2 (XB-24) modules. Lets say that I am trying to control 3 Servos with the help of 2 joysticks(X-Y, 2 pots with a push button).
/* TX
*/
#define aileron 2
#define elevon 3
#define esc 4
#define elevontrim 0
#define ailerontrim 1
#define esctrim 5
int ailerontrimVal,elevontrimVal,aileronVal,elevonVal,esctrimVal,escVal;
void setup()
{
Serial.begin(9600);//1152200 only 9600 works, check XBEE forum
}
void loop()
{
aileronVal = analogRead(aileron);
aileronVal = map(aileronVal, 0, 1023, 0, 179);
ailerontrimVal = analogRead(ailerontrim);
ailerontrimVal = map(ailerontrimVal, 0, 1023, -25, 25);
aileronVal = aileronVal+ ailerontrimVal;
constrain(aileronVal, 0, 179);
elevonVal = analogRead(elevon);
elevonVal = map(elevonVal, 0, 1023, 0, 179);
elevontrimVal = analogRead(elevontrim);
elevontrimVal = map(elevontrimVal, 0, 1023, -25, 25);
elevonVal = elevonVal+ elevontrimVal;
constrain(elevonVal, 0, 179);
escVal = analogRead(esc);
escVal = map(escVal, 0, 1023, 0, 179);
esctrimVal = analogRead(esctrim);
esctrimVal = map(esctrimVal, 0, 1023, -25, 25);
escVal = escVal+ esctrimVal;
constrain(escVal, 0, 179);
Serial.print("L");
Serial.print(aileronVal);
Serial.print("R");
Serial.print(elevonVal);
Serial.print("S");
Serial.print(escVal);
//delay(5);
}
/* RX
* State machine
*/
#include <Servo.h>
#define laileron 11
#define raileron 10
#define throttle 9
typedef enum{NONE,GOT_L,GOT_R,GOT_S} states;
Servo left,right,esc;
states state = NONE;
unsigned int currentValue;
void setup()
{
Serial.begin(9600); //1152200 only 9600 works, check whats wrong
state=NONE;
left.attach(laileron);
right.attach(raileron);
esc.attach(throttle);
}
void processLeft(const unsigned int value)
{
left.write(value);
}
void processRight(const unsigned int value)
{
right.write(value);
}
void processEsc(const unsigned int value)
{
esc.write(value);
}
void hanldlePreviousState()
{
switch(state)
{
case GOT_L:
processLeft(currentValue);
break;
case GOT_R:
processRight(currentValue);
break;
case GOT_S:
processEsc(currentValue);
break;
}
currentValue=0;
}
void processIncomingByte(const byte c)
{
if(isdigit(c))
{
currentValue *= 10;
currentValue += c-'0';
}
else
{
hanldlePreviousState();
switch(c)
{
case 'L': state=GOT_L;
break;
case 'R': state=GOT_R;
break;
case 'S': state=GOT_S;
break;
default: state=NONE;
}
}
}
void loop()
{
if(Serial.available())
processIncomingByte(Serial.read());
}
I know the code works fine because when I connect the two Arduinos using the TX,RX pins it works without any trouble. But when I try to use the Serial connection of the 2 XBEEs it gets slow with the time. It works like the wire connection at first but after some time it doesn't even work.
My Xbees are setup like this.
One I am using at the transmitter is the Router which is set to AT mode (Zigbee router AT: command set in X-CTU) has following set.
PanID (ATID) =0488
Channel verification (ATJV) = 1
ATDH = 0
The other one is the Coordinator which is the receiver for my plane is also in AT mode (Zigbee coordinator AT) has following set
PanID = 0488
others are all set to the factory defaults.
Why is my serial connection laggy with XBEEs?
Anyone could point me to an answer would receive my gratitude! ![]()
Bhashithe