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

时间:2016-03-29 06:07:48

标签: bluetooth arduino sensor

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

我需要简单地将arduino中的0,1,2值发送到蓝牙模块, 也只会有两个领导者。

帮我描绘两个传感器的读数?或者我可能必须为每个传感器使用两个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);
      }
    }

0 个答案:

没有答案