【问题标题】:How to make a response in Bluetooth module (Arduino)如何在蓝牙模块(Arduino)中做出响应
【发布时间】:2021-03-05 04:26:29
【问题描述】:

我将我的蓝牙模块连接到手机并编写了一个代码,通过蓝牙在 Arduino 和手机之间进行通信(从蓝牙模块向设备发送消息,反之亦然)。

现在我想做出回应,这意味着如果我从手机发送“嗨”,arduino 会回复并说“你好”或其他什么。

我尝试了很多代码,但都没有奏效,请问有人能帮帮我吗?

#include <SoftwareSerial.h>

SoftwareSerial myserial (6,5); 

void setup() {
  myserial.begin(9600);
  Serial.begin (9600);
}

void loop() {
  if (myserial.available()) {
    Serial.write(+ myserial.read());
  }
  if (Serial.available()) {
    myserial.write(Serial.read());
  }
}

另一个代码,但在不发送任何内容的情况下进行循环

#include <SoftwareSerial.h>

SoftwareSerial myserial(6,5); //Arduino: R:5,T:6; bluetooth: T:5, R:6;

void setup() {
  myserial.begin(9600);
  Serial.begin (9600);
}

void loop() {
  if (myserial.available()) {
    Serial.write(myserial.read()); 
  }
  if (Serial.available()) {
    myserial.write(Serial.read());
  }
  for (int i = 0; i=2; i++) {
    myserial.write("hello");
  }
  if (myserial.read() =="n") {
    myserial.write("hello");
  }
}

【问题讨论】:

    标签: arduino bluetooth arduino-uno


    【解决方案1】:

    您需要使用SerialEvent() 中断逐个字符地构建串行字符串,然后使用.equals 方法进行字符串比较。尽管很多人会告诉您 String 类型是“邪恶的”(请参阅​​ thisthis),但它可能是使事情更清晰的好解决方案(如果您不想弄乱 C char,也许会更容易一些) strcmp() 函数和指针。最后,String 类型适合你,我认为没有理由不在一般项目中使用它。

    基于SerialEvent() 文档 [1] 和字符串参考 [2],您可以执行以下操作:

    String inputString = "";         // a String to hold incoming data
    bool stringComplete = false;  // whether the string is complete
    
    void setup() {
      // initialize serial:
      Serial.begin(9600);
      // reserve 200 bytes for the inputString:
      inputString.reserve(200);
    }
    
    void loop() {
      // print the string when a newline arrives:
      if (stringComplete) {
        
        //Then you compare the inputString with the word you want to detect using the .equals method from the String class
          if(inputString.equals("Hi"){
              Serial.println("Hello");
          }
        // clear the string for a new comparison:
        inputString = "";
        stringComplete = false;
      }
    }
    
    /*
      SerialEvent occurs whenever a new data comes in the hardware serial RX. This
      routine is run between each time loop() runs, so using delay inside loop can
      delay response. Multiple bytes of data may be available.
    */
    void serialEvent() {
      while (Serial.available()) {
        // get the new byte:
        char inChar = (char)Serial.read();
        // add it to the inputString:
        inputString += inChar;
        // if the incoming character is a newline, set a flag so the main loop can
        // do something about it:
        if (inChar == '\n') {
          stringComplete = true;
        }
      }
    }
    

    编辑 1:当然,您可以将此示例代码中的 Serial 对象替换为您自己的蓝牙通信中的 myserial 对象。

    【讨论】:

    • 嗯,谢谢你这个惊人的答案,但问题是我是一个超级初学者,我对你写的所有东西都不熟悉。但老实说,代码看起来很棒xd 所以,我如何在我自己的项目中使用这个代码,我的意思是我应该定义引脚等等还是我应该做什么?再次感谢您
    • 您可以逐行复制粘贴我与您共享的代码,并仅更改适用于您的SoftwareSerial() 的部分,仔细阅读,您会发现它非常简单。不要惊慌!
    猜你喜欢
    • 2013-04-21
    • 1970-01-01
    • 2016-04-29
    • 1970-01-01
    • 2013-04-06
    • 1970-01-01
    • 1970-01-01
    • 2018-02-19
    • 2018-03-10
    相关资源
    最近更新 更多