【问题标题】:Send and receive data through infrared (IR) between 2 mBots (Arduino)在 2 个 mBot (Arduino) 之间通过红外线 (IR) 发送和接收数据
【发布时间】:2017-06-06 21:42:54
【问题描述】:

我有两个 mBot(Arduino 机器人),我想知道如何与董事会的红外传感器进行通信。我已经达到的最大值是在 mBot 中检测到遥控器上的按钮脉冲。我真正想做的是将整数从第一个 mBot 发送到第二个,但看起来 mBot 红外代码仅用于检测遥控器按钮脉冲。如果我可以从第一个 mBot 发送与您按下按钮到第二个 mBot 时发送遥控器相同的脉冲,那么我可以做一个开关盒并将接收到的按钮脉冲(例如按钮 0)转换为数字(收到的 int = 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 板,因此使用 IR 传感器的正常 arduino 方式将无法正常工作,因为 mBot 有自己的构建和自己的库。

任何帮助将不胜感激

【问题讨论】:

    标签: arduino infrared


    【解决方案1】:

    解决了!

    如何通过红外线与两个 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,以通过红外线发送和接收数据。这是发送和接收数据的代码:

    通过 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
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-19
      相关资源
      最近更新 更多