so I am trying to transmit analog data from a ps2 button to an xbee. my sending xbee is the router that is connected to an arduino uno. my receiving xbee is the coordinator that is connected to an arduino mega 2560. when i send data i can receive all of it perfectly in xctu but when i connect the receiving xbee to the arduino mega with the code below it won't go in the second loop (XBeeResponse().getApiId()==ZB_RX_RESPONSE).
sender code:
#include <XBee.h>
XBee xbee = XBee();
uint8_t ps2Button[2]; //= "Hallo War";
XBeeAddress64 addr64 = XBeeAddress64 (0x0013A200, 0x4125704D);
ZBTxRequest zbTx = ZBTxRequest (addr64, ps2Button, sizeof(ps2Button));
ZBTxStatusResponse txStatus = ZBTxStatusResponse();
int YValue = 0;
const byte errorLed = 4;
const byte statusLed = 6;
const byte succesLed = 8;
void setup()
{
pinMode (errorLed, OUTPUT);
pinMode (statusLed, OUTPUT);
pinMode (succesLed, OUTPUT);
Serial.begin(9600);
xbee.setSerial(Serial);
}
void loop()
{
YValue = analogRead (A0);
//YValue = map(YValue, 0, 1023, 0, 510);
ps2Button[0] = YValue >> 8 & 0xFF;
//Serial.println (ps2Button[0]);
//Serial.print (" ");
Serial.println (YValue);
ps2Button[1] = YValue & 0xFF;
xbee.send(zbTx);
flashLed (statusLed, 2, 100);
if (xbee.readPacket(5000))
{
flashLed (succesLed, 5, 100);
if (xbee.getResponse().getApiId() == ZB_TX_STATUS_RESPONSE)
{
xbee.getResponse().getZBTxStatusResponse (txStatus);
if (txStatus.getDeliveryStatus() == SUCCESS)
{
flashLed (statusLed, 5, 50);
}
else
{
flashLed (errorLed, 3, 500);
}
}
}
else if (XBeeResponse().isError())
{
Serial.print ("error reading packet. error code: ");
Serial.println (xbee.getResponse().getErrorCode());
}
else
{
flashLed (errorLed, 2, 50);
}
delay (1000);
}
void flashLed (byte pin, byte times, int wait)
{
for (int i = 0; i < times; i++)
{
digitalWrite (pin, HIGH);
delay (wait);
digitalWrite (pin, LOW);
if (i + 1 < times)
{
delay (wait);
}
}
}
receiver code:
XBee xbee = XBee();
ZBRxResponse rx = ZBRxResponse();
ModemStatusResponse msr = ModemStatusResponse();
Servo FrontLeftMotor;
Servo BackLeftMotor;
Servo FrontRightMotor;
Servo BackRightMotor;
const byte statusLed = 8;
const byte errorLed = 4;
const byte dataLed = 6;
int currentYValue = 0;
int multiMotorSpeed = 0;
void setup()
{
pinMode (statusLed, OUTPUT);
pinMode (errorLed, OUTPUT);
pinMode (dataLed, OUTPUT);
FrontLeftMotor.attach(3);
BackLeftMotor.attach(5);
FrontRightMotor.attach(7);
BackRightMotor.attach(9);
Serial.begin (9600);
xbee.begin(Serial);
flashLed (statusLed, 3, 50);
}
void loop()
{
xbee.readPacket();
flashLed (statusLed, 1, 100);
if (XBeeResponse().isAvailable())
{
XBeeResponse().getZBRxResponse(rx);
flashLed (dataLed, 3, 25);
uint8_t analogMSB = rx.getData (0);
uint8_t analogLSB = rx.getData (1);
currentYValue = analogLSB + (analogMSB * 256);
Serial.println (currentYValue);
if (rx.getOption() == ZB_PACKET_ACKNOWLEDGED)
{
flashLed (statusLed, 10, 10);
}
else
{
flashLed (errorLed, 2, 20);
}
servoControl();
if (XBeeResponse().getApiId() == MODEM_STATUS_RESPONSE)
{
XBeeResponse().getModemStatusResponse(msr);
if (msr.getStatus() == ASSOCIATED)
{
flashLed (statusLed, 10, 10);
}
else if (msr.getStatus() == DISASSOCIATED)
{
flashLed (errorLed, 10, 10);
}
else
{
flashLed (statusLed, 5, 10);
}
}
else
{
Serial.println ("error");
flashLed (errorLed, 1, 100);
}
}
else if (XBeeResponse().isError())
{
Serial.print ("error reading packet. error code: ");
Serial.println (XBeeResponse().getErrorCode());
}
delay (100);
}
void servoControl()
{
static int lastYValue;
if (currentYValue == 0)
{
multiMotorSpeed = 0;
FrontLeftMotor.write (multiMotorSpeed);
BackLeftMotor.write (multiMotorSpeed);
FrontRightMotor.write (multiMotorSpeed);
BackRightMotor.write (multiMotorSpeed);
}
if (lastYValue > (currentYValue + 10) || lastYValue > (currentYValue - 10))
{
multiMotorSpeed = multiMotorSpeed - 10;
FrontLeftMotor.write (multiMotorSpeed);
BackLeftMotor.write (multiMotorSpeed);
FrontRightMotor.write (multiMotorSpeed);
BackRightMotor.write (multiMotorSpeed);
}
if (lastYValue < (currentYValue + 10) || lastYValue < (currentYValue - 10))
{
multiMotorSpeed = multiMotorSpeed + 10;
FrontLeftMotor.write (multiMotorSpeed);
BackLeftMotor.write (multiMotorSpeed);
FrontRightMotor.write (multiMotorSpeed);
BackRightMotor.write (multiMotorSpeed);
}
lastYValue = currentYValue;
}
void flashLed (byte pin, byte times, int wait)
{
for (int i = 0; i < times; i++)
{
digitalWrite (pin, HIGH);
delay (wait);
digitalWrite (pin, LOW);
if (i + 1 < times)
{
delay (wait);
}
}
}