使用Arduino UNO配置和配对2个HC-06蓝牙模块为主控和从属

问题描述 投票:1回答:1

我一直在尝试在两个HC-06蓝牙模块之间建立连接。配对已完成。这两个模块正在通信。我的目的是从一个模块发送一封信,并从另一个模块接收确认。主模块的代码如下。

#include <SoftwareSerial.h>
SoftwareSerial BTserial(2,3); // RX, TX

char c;
char s[]="Matched";
int t[]="NotMatched";

void setup() 
{
    // start the serial communication with the computer
    Serial.begin(9600);
    Serial.println("Arduino with HC-06 is ready");
     // start communication with the HC-06 using 38400
    BTserial.begin(38400);  
    Serial.println("Bluetooth serial started at 38400");
}

void loop()
{  
   // Read from HC-06 and send to Arduino Serial Monitor
   if (BTserial.available())
    { 
        c=(BTserial.read()); 
    if (c=='a')
      {
      Serial.write(s);
      }
    else 
      {
      Serial.write(t);  
      }
    }

    // Read from Arduino Serial Monitor and send to HC-06
    if (Serial.available())
    {
        c =  Serial.read();
        Serial.write(c);   
        BTserial.write(c); 
    }
}

类似代码用于从站模块。除了代码中的“其他”部分之外,其他所有内容都可以正常运行。当代码的if和else部分(即“不匹配不匹配不匹配”)在接收到字符“ a”时被打印,并且同时打印“不匹配不匹配不匹配”时,我收到确认以及else部分被打印两次它接收除'a'以外的任何东西。您能给我一些可能出问题的建议吗]

bluetooth arduino-uno master-slave
1个回答
0
投票

尝试在两个写入功能之间和/或在写入和读取功能之间放置一些延迟。

© www.soinside.com 2019 - 2024. All rights reserved.