2017-06-06 53 views
1

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

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

这是代码我有用于检测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 
} 

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

请记住,因为这些机器人不完全是arduino uno板,所以使用红外传感器的正常arduino方式将不起作用,因为mBot有自己的建立和自己的图书馆。

任何帮助将被折扣

回答

0

解决!

如何comunicate通过红外2个mBots:

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

2:进入C /程序/ Arduino的/库和清除预设的IR库,因为它的这不一样,这导致不兼容。然后复制Arduino-IRremote库文件夹以将其添加到Arduino库。 3:打开makeblock库并在文件MeMCore.h中注释第68行(//#include“MeIR.h”)。这也是为了避免不兼容。

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

#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 
    } 
} 

#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接收数据:

通过红外发送数据