通过红外(IR)在2 mBots之间发送和接收数据(Arduino)

时间:2017-06-06 21:42:54

标签: arduino infrared

我有两个mBots(Arduino机器人),我想知道如何使用电路板的红外传感器进行通信。我达到的最大值是在mBot中检测遥控器上的按钮脉冲。我真正想做的是从第一个mBot向第二个mBot发送ints,但看起来mBot红外代码只能用于检测遥控器按钮脉冲。如果我可以从第一个mBot发送当你按下按钮到第二个mBot时发送遥控器的相同脉冲,那么我可以做一个开关盒并将接收到的按钮脉冲(例如按钮0)转换为数字( int received = 0)。

而不是这样做,直接发送和接收整数甚至字符串会更好。但是在这一点上任何用红外线对两个mBot进行通信的方法对我来说都没问题。

这是用于检测mBot中遥控器按钮脉冲的代码:

include Wire.h 
include SoftwareSerial.h 
include MeMCore.h

MeIR ir;
MeBuzzer buzzer;

void setup()
{

ir.begin();

}

void loop(){

if(ir.keyPressed(22)) // receive button 0 pulse
buzzer.tone(460,200); // make a beep 
}

¿有人知道在两个mBot之间发送和接收数据的代码是怎样的吗? (即使它只有按钮编号的脉冲从0到9)

请记住,因为这些机器人并不完全是arduino uno board,所以使用IR传感器的正常方式是不行的,因为mBot有自己的构建和自己的库。

任何帮助都将是apreciated

1 个答案:

答案 0 :(得分:0)

解决!

如何通过红外线通信两个mBot:

1:下载Arduino-IRremote库:https://github.com/z3t0/Arduino-IRremote

2:转到C / programs / Arduino / libraries并删除默认的IR库,因为它与此不一样,并且会导致不兼容。然后复制Arduino-IRremote库文件夹,将其添加到Arduino库中。

3:打开makeblock库,在文件MeMCore.h中注释第68行(//#include“MeIR.h”)。这也是为了避免不兼容。

最后,您可以上传到您的mBot草图,通过infrarred发送和接收数据。这是发送和接收数据的代码:

通过IR发送数据:

#include <IRremote.h>

IRsend irsend;

int robot_ori=91;
int n_coordenadas=4;
int x[10], y[10];

void setup()
{
  x[1]=200;   y[1]=217;
  x[2]=199;   y[2]=213;
  x[3]=210;   y[3]=179;
  x[4]=212;   y[4]=140;
}

void loop() 
{

  irsend.sendSony(robot_ori, 16);   // data to send, nº of bits to send 
  delay(40);                        // for int type is 16 bits
  irsend.sendSony(n_coordenadas, 16); 
  delay(40);

  for (int i = 1; i <= n_coordenadas; i++) 
  {
    irsend.sendSony(x[i], 16);
    delay(40);  
    irsend.sendSony(y[i], 16);        
    delay(40);
  }

  delay(5000); //5 second delay between each signal burst

}

通过IR接收数据:

#include <IRremote.h>

int RECV_PIN = 2;

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
}

void loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, DEC); // DEC decimal, HEX hexadecimal, etc
    irrecv.resume(); // Receive the next value
  }
}