2017-06-06 187 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接收數據:

通過紅外發送數據