Bluetooth 我想将hc sr04数据传递给arduino上附带的hc05

Bluetooth 我想将hc sr04数据传递给arduino上附带的hc05,bluetooth,arduino,sensors,Bluetooth,Arduino,Sensors,我想将我的传感器数据,即两个hc sr04读数传递到我的蓝牙模块hc 05,然后传递到我的android设备。下面写的代码是正确的吗?我也想要一些减少它的建议 我只需要从arduino向蓝牙模块发送0,1,2个值, 也将只有两个led 帮我描述两个传感器的读数?或者我可能需要为每个传感器使用两个led #include <SoftwareSerial.h> int bTx = 2; int bRx = 3; SoftwareSerial bluetooth

我想将我的传感器数据,即两个hc sr04读数传递到我的蓝牙模块hc 05,然后传递到我的android设备。下面写的代码是正确的吗?我也想要一些减少它的建议

我只需要从arduino向蓝牙模块发送0,1,2个值, 也将只有两个led

帮我描述两个传感器的读数?或者我可能需要为每个传感器使用两个led

#include <SoftwareSerial.h>

    int bTx = 2;
    int bRx = 3;

    SoftwareSerial bluetooth(bTx,bRx);

    #define trigPin 12
    #define echoPin 13
    #define trigPin2 10
    #define echoPin2 11
    #define rLed 6
    #define gLed 7  

    void setup() {
      Serial.begin (9600);//usb serial connection to computer

      //setup bluetoothserial connection to android
      bluetooth.begin(9600);
      bluetooth.print("$$$");//Slave mode
      delay(100);
      pinMode(trigPin, OUTPUT);
      pinMode(echoPin, INPUT);
      pinMode(trigPin2, OUTPUT);
      pinMode(echoPin2, INPUT);
      pinMode(rLed, OUTPUT);
      pinMode(gLed, OUTPUT);

    }

    void loop() 
    {
      //read from bluetooth
      if(bluetooth.available())
      {
      long duration,duration2, distance, distance2;
      digitalWrite(trigPin, HIGH);
      delayMicroseconds(20);
      digitalWrite(trigPin, LOW);
      duration = pulseIn(echoPin, HIGH);
      distance = (duration/2) / 29.1;

      digitalWrite(trigPin2, HIGH);
      delayMicroseconds(20);
      digitalWrite(trigPin2, LOW);
      duration2 = pulseIn(echoPin2, HIGH);
      distance2 = (duration2/2) / 29.1;


      if (distance >= 200 || distance <= 0 || distance2 >= 200 || distance2 <= 0)
       { 
        Serial.println("Out of range");
        bluetooth.print(1,DEC);
        bluetooth.print(2,DEC);

       }
      if (distance < 4)
       {  
         digitalWrite(rLed,HIGH); // When the Red condition is met, the Green LED should turn off
         digitalWrite(gLed,LOW);
         int val = digitalRead(rLed);//stores value 1 in val
         bluetooth.print(val,DEC); 
       }
        if (distance2 < 4)
       {  
         digitalWrite(rLed,HIGH); // When the Red condition is met, the Green LED should turn off
         digitalWrite(gLed,LOW);
         int val = digitalRead(rLed);//stores value 1 in val
         bluetooth.print(val,DEC); 
       }
      else
      {
        digitalWrite(rLed,LOW);
        digitalWrite(gLed,HIGH);
        int vacant=digitalRead(rLed);//stores value 0 in vacant
        bluetooth.print(vacant,DEC);
      }

      delay(500);
      }
    }
#包括
int bTx=2;
int-bRx=3;
软件串行蓝牙(bTx、bRx);
#定义trigPin 12
#定义echoPin 13
#定义trigPin2 10
#定义echoPin2 11
#定义rLed 6
#定义gLed 7
无效设置(){
Serial.begin(9600);//与计算机的usb串行连接
//设置蓝牙与android的串行连接
蓝牙开始(9600);
bluetooth.print(“$$”)//从模式
延迟(100);
引脚模式(trigPin,输出);
pinMode(echoPin,输入);
pinMode(trigPin2,输出);
pinMode(echoPin2,输入);
pinMode(rLed,输出);
pinMode(gLed,输出);
}
void循环()
{
//从蓝牙读取
if(bluetooth.available())
{
长持续时间,持续时间2,距离,距离2;
数字写入(trigPin,高);
延迟微秒(20);
数字写入(trigPin,低电平);
持续时间=脉冲强度(echoPin,高);
距离=(持续时间/2)/29.1;
数字写入(trigPin2,高);
延迟微秒(20);
数字写入(trigPin2,低电平);
持续时间2=脉冲强度(echoPin2,高);
距离2=(持续时间2/2)/29.1;

如果(距离>=200 | |距离=200 | |距离2)可能重复,请避免多次发布同一问题;如果您有更新,请将其添加到您的