首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何与HM-19 BLE模块通信并利用超声波传感器进行扫描

如何与HM-19 BLE模块通信并利用超声波传感器进行扫描
EN

Stack Overflow用户
提问于 2020-01-26 10:54:23
回答 1查看 2.7K关注 0票数 0

我正在为学校做我的高级项目,我需要做的一部分是使用HM-19蓝牙5.0模块连接到另一个蓝牙5.0模块,并建立主从连接。

我可以很好地做到这一点,但是当我添加超声波传感器进行扫描所需的代码时,我对HM-19的命令不会返回任何内容,并且我不能执行任何基本功能,例如查找连接。我在使用和不使用超声波传感器代码的情况下对其进行了测试,当我使用代码的传感器部分时出现问题。

要清楚的是,我所要做的只是让我的蓝牙5.0芯片连接到另一个芯片上,并执行正常的命令,同时当我把手放在前面时,也可以输入到我的串行监视器中一段距离。这只是一个测试,一旦我完成了测试,我就会去做我真正想做的事情。

这只是一个项目的起点。在void循环中,我有一个对传感器和蓝牙芯片的函数调用,这就是全部内容。

我只想知道如何解决这个问题。如何使用超声波传感器进行扫描并将命令发送到蓝牙模块?任何帮助都将不胜感激。

这是传感器被注释时的结果,这是导致无限循环的不成功结果,其中我无法访问返回芯片内容的代码部分。最后,尽管大多数链接都包含HM-10的内容,但HM-19的命令基本上是相同的。我添加了更多,因为堆栈溢出不会让我编辑这篇文章,直到有更多的字符或东西。我希望你有一个好的人阅读这篇文章。

下面是我的代码:

代码语言:javascript
运行
复制
    //  SerialIn_SerialOut_HM-10_01
//
//  Uses hardware serial to talk to the host computer and AltSoftSerial for communication with the bluetooth module
//
//  What ever is entered in the serial monitor is sent to the connected device
//  Anything received from the connected device is copied to the serial monitor
//  Does not send line endings to the HM-10
//
//  Pins
//  BT VCC to Arduino 5V out. 
//  BT GND to GND
//  Arduino D8 (SS RX) - BT TX no need voltage divider 
//  Arduino D9 (SS TX) - BT RX through a voltage divider (5v to 3.3v)
//

#include <AltSoftSerial.h>
AltSoftSerial BTserial; 
// https://www.pjrc.com/teensy/td_libs_AltSoftSerial.html


char c=' ';
boolean NL = true;
const int trigPin = 9;
const int echoPin = 10;
float duration, distance;
boolean wait_your_turn = false; //My attempt to make sure the sensor and the Bluetooth module don't interfere with each other
//if I'm sending data from the serial monitor to the bluetooth module and vice versa it switches to true and the bluetooth module 
//does its thing, so the sensor doesn't get in the way.

void setup() 
{
    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);
    Serial.begin(9600);
    Serial.print("Sketch:   ");   Serial.println(__FILE__);
    Serial.print("Uploaded: ");   Serial.println(__DATE__);
    Serial.println(" ");

    BTserial.begin(9600);  
    Serial.println("BTserial started at 9600");
}

void loop()
{
  Bluetooth();
  Sensor();
}

void Sensor(){
  if((wait_your_turn == true))
  {}

  else
  {
    Serial.println("Scanning for stuff.");
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);

    duration = pulseIn(echoPin, HIGH);
    distance = (duration*.0343)/2;

    if(distance <= 20)
    {
      Serial.println(distance);
      delay(500);
    }
  }
}

void Bluetooth()
{
    if (Serial.available())
    {
     if(wait_your_turn == false)
       Serial.println("Serial is available");

      wait_your_turn = true;

     while(Serial.available()>0)
       c = Serial.read();

     Serial.write(c);

     if(c!=10&c!=13)
       BTserial.print(c);       
    }

    if (BTserial.available())
    {
      // Serial.print("We are at the bluetooth portion.");
        while(BTserial.available())
          c = BTserial.read();

        Serial.print(c);
        wait_your_turn = false;
    }
}


  [1]: https://i.stack.imgur.com/Dn4i0.png
  [2]: https://i.stack.imgur.com/s9Ifv.png
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-28 06:21:54

对不起,我忘了这个问题。我想通了。我所做的是让1个Arduino控制超声波传感器,当有东西在传感器的范围内时,发送一个角色给另一个Arduino。然后,另一个Arduino将读取该字符,并根据字符send执行一个操作。感谢所有发表评论的人,祝你们过得愉快。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59915179

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档