Arduino 2个XBee模块之间没有连接

Arduino 2个XBee模块之间没有连接,arduino,xbee,Arduino,Xbee,我有两个arduinos,每个都有一个arduino无线屏蔽,上面还有一个 通信未按预期工作。我可以接收和发送字节,但是模块之间的连接经常被中断,所以串行缓冲区增长了很多 此外,如果我将模块彼此移动超过1米,连接将被完全拒绝 我在想,我的xbee模块是不是有点坏了,或者我可能配置错了 有什么想法吗 发件人的源代码: void setup() { Serial.begin(9600); } void loop() { int sensorValue = analogRead(0); in

我有两个arduinos,每个都有一个arduino无线屏蔽,上面还有一个

通信未按预期工作。我可以接收和发送字节,但是模块之间的连接经常被中断,所以串行缓冲区增长了很多

此外,如果我将模块彼此移动超过1米,连接将被完全拒绝

我在想,我的xbee模块是不是有点坏了,或者我可能配置错了

有什么想法吗

发件人的源代码:

void setup()
{
 Serial.begin(9600); 
}

void loop()
{
 int sensorValue = analogRead(0);
 int val = map(sensorValue, 0, 1023, 35, 160);
 Serial.write(char(val));
 delay(250);
}
接收器的源代码:

#include <Servo.h>

Servo motor1;
Servo motor2;
Servo motor3;
Servo motor4;

void setup()
{
  motor1.attach(9);
  motor2.attach(10);
  motor3.attach(3);
  motor4.attach(11);

  Serial.begin(9600);
}

void loop()
{ 
  if(Serial.available() > 0)
  {
    byte incoming = Serial.read();
    int inValue = constrain(incoming, 35, 160);
    motor1.write(inValue);
    motor2.write(inValue);
    motor3.write(inValue);
    motor4.write(inValue);
  }
  delay(250);
}
#包括
伺服电机1;
伺服电机2;
伺服电机3;
伺服电机4;
无效设置()
{
电机1.连接(9);
电机2.连接(10);
电机3.连接(3);
电机4.连接(11);
Serial.begin(9600);
}
void循环()
{ 
如果(Serial.available()>0)
{
字节传入=串行.read();
无效=约束(传入,35,160);
电机1.写入(无效);
电机2.写入(无效);
3.写(无效);
4.写(无效);
}
延迟(250);
}

需要考虑的几件事:

  • 确保无线电没有在通道26(0x1A)上形成网络。XBee模块必须在该通道上以较低的功率运行,因此我通常将ATSC设置为0x7FFF以排除通道26

  • 该型号的XBee使用PCB天线,位于模块的锥形部分。确保上面或下面没有任何金属(接地层、组件、电线),并且不要将其放在会限制信号的大金属外壳中

  • 检查
    ATRO
    的值,即打包超时。如果您希望XBee在输入字符时发送字符,而不是等待更多数据在单个数据包中组合在一起,则需要将其设置为一个较低的值(3-5),甚至0或1

  • 如果存在范围问题,请检查
    ATPL
    (功率水平)和
    ATPM
    (功率模式)设置。启用增压模式(
    ATPM=1
    )和最高功率电平(
    ATPL=4
    )可能有助于解决范围问题

  • 您可能希望更改接收代码以更频繁地轮询字节,或者甚至忽略多个字节,只使用最后接收的值。这将防止接收端积压字节

处理任何未处理的字节:

while (Serial.available() > 0)
{
    byte incoming = Serial.read();
    int inValue = constrain(incoming, 35, 160);
    motor1.write(inValue);
    motor2.write(inValue);
    motor3.write(inValue);
    motor4.write(inValue);
}
忽略缓冲字节,仅写入最后一个值:

if (Serial.available() > 0)
{
    byte incoming;
    // read all bytes but only use the last value read
    while (Serial.available() > 0) incoming = Serial.read();
    int inValue = constrain(incoming, 35, 160);
    motor1.write(inValue);
    motor2.write(inValue);
    motor3.write(inValue);
    motor4.write(inValue);
}

谢谢你详细的回答。我想造一个四翼机。为此,我组装了一个铝制框架。铝没有问题,不是吗?你认为,问题可能是arduino的无线屏蔽?我检测到,我只能通过两个无线屏蔽之一配置xbee(我没有使用xbee浏览器usb组件)。