简体   繁体   English

Arduino蓝牙

[英]Arduino Bluetooth

I posted something else about this a while ago. 不久前,我还发布了一些其他信息。 Basically, I was trying to establish a Bluetooth connection between an Android phone and an Arduino. 基本上,我试图在Android手机和Arduino之间建立蓝牙连接。 This was to get a remote control car working wirelessly. 这是为了让遥控车以无线方式工作。

I decided to get an HC-06 Bluetooth module instead of the HC-08 I had. 我决定购买HC-06蓝牙模块,而不是原来的HC-08。 This seemed to work for part of it. 这似乎部分起作用。 I can send a string from an Android phone to an Arduino but nothing happens. 我可以将字符串从Android手机发送到Arduino,但没有任何反应。 When testing this I have my Arduino hooked up to my computer and have the serial monitor displayed. 测试时,我将Arduino连接到计算机并显示了串行监视器。 When I send the string, the Arduino receives this string then activates a certain piece of code. 当我发送字符串时,Arduino会接收到该字符串,然后激活一段代码。 The forward code works and has a serial print line in it, and when the certain string is sent the word forward is printed on the serial monitor. 转发代码有效并且在其中具有串行打印行,并且当发送特定字符串时,将单词forward打印在串行监视器上。 So I know it's being received but it's just not activating the other part of the function for some reason. 所以我知道它已经被接收了,但是由于某种原因它并没有激活功能的其他部分。

Can anybody help? 有人可以帮忙吗?

I'm pretty sure it's the Arduino code so here it is: 我很确定这是Arduino代码,所以在这里是:

  if (Serial.available() > 0) {
    string = "";
  }
  while (Serial.available() > 0) {
    command = ((byte)Serial.read());
    if (command == ':') {
      break;
    } else {
      string += command;
    }
    delay(1);
  }
  if (string == "F") {
    forward();
  }
  if (string == "B") {
    back();
  }
  if (string == "R") {
    right();
  }
  if (string == "L") {
    left();
  }
  if (string == "S") {
    stop();
  }
}

void forward() {
  digitalWrite(ENA, HIGH);
  digitalWrite(ENB, HIGH);
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  Serial.println("Forward");
}

Let's try this, just remove 让我们尝试一下,删除

if (Serial.available() > 0) {
    string = "";
  }

And add string = ""; 并添加字符串=“”; after each cases. 在每种情况下。

Result : Hope that will help you 结果:希望对您有帮助

while (Serial.available() > 0) {
    command = ((byte)Serial.read());
    if (command == ':') {
      break;
    } else {
      string += command;
    }
    delay(1);
    }
    if (string == "F") {
    forward();
    string = "";
    }
    if (string == "B") {
    back();
    string = "";
    }
    if (string == "R") {
    right();
    string = "";
    }
    if (string == "L") {
    left();
    string = "";
    }
    if (string == "S") {
    stop();
    string = "";
    }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM