#include <SoftwareSerial.h> //Arduino I2C library 
#include <Wire.h> 
// >>> 20181126  
const int  trigPinL = 4 ; //左邊超音波 觸發腳Trig 
const int  echoPinL = 5 ; //左邊超音波 接收腳 Echo 
unsigned long distanceL ; // 距離 cm 
 
// >>> 20181126 取得又邊超音波的距離設定 
const int  trigPinR = 2 ; // 右邊超音波 觸發腳Trig 
const int  echoPinR = 3 ;  //右邊超音波 接收腳 Echo 
unsigned long distanceR ; // 距離 cm 
// <<< 20181126 取得又邊超音波的距離設定 
 
//const int trig =  4; //define Arduino pin 
//const int echo =  5; 
SoftwareSerial I2CBT(11,10); 
byte serialA; 
 
const int delay_time = 1000; //delay 1000 ms for every measurment 
 
 
///>>>酒精偵測 
const int LEDPin=13;  // LED燈條 
const int AOUTpin=0; 
const int DOUTpin=8; 
const int buzzerPin=12; 
unsigned char i = 0;     
int limit; 
int value;  
 
// >>> 20181126 取得左邊超音波的距離 
unsigned long pingL() { 
  digitalWrite(trigPinL,HIGH) ; //觸發腳位設定為高電位 
  delayMicroseconds(1000);   //持續5微秒 
  digitalWrite(trigPinL,LOW) ; 
  //  
 // return  ( pulseIn(echoPinL,HIGH)/58)  ;  // 換算成 cm 並傳回 
  return  ( pulseIn(echoPinL,HIGH))  ;  // 換算成 cm 並傳回 
} 
 
// >>> 20181126 取得右邊超音波的距離 
unsigned long pingR() { 
    digitalWrite(trigPinR,HIGH) ; //觸發腳位設定為高電位 
    delayMicroseconds(1000);   //持續5微秒 
    digitalWrite(trigPinR,LOW) ; 
    return  ( pulseIn(echoPinR,HIGH))  ;  // 換算成 cm 並傳回 
  } 
// <<< 20181126 取得右邊超音波的距離 
 
void setup() { 
  Serial.begin(9600); //set baud rate of Serial Monitor 
  I2CBT.begin(9600); //set baud rate of Bluetooth 
  pinMode(trigPinL, OUTPUT); //set trigger pin to OUTPUT (-> pin trig of Ultrasonic Sensor) 
  pinMode(echoPinL, INPUT); //set echo pin to INPUT (-> pin echo of Ultrasonic Sensor) 
  pinMode(trigPinR, OUTPUT); //set trigger pin to OUTPUT (-> pin trig of Ultrasonic Sensor) 
  pinMode(echoPinR, INPUT); //set echo pin to INPUT (-> pin echo of Ultrasonic Sensor) 
  Serial.println("Start!!!"); 
  Serial.begin(9600);//sets the baud rate 
  pinMode(DOUTpin, INPUT);//sets the pin as an input to the arduino 
  pinMode(buzzerPin, OUTPUT);//sets the pin as an output of the arduino 
} 
 
void loop() { 
 
  // >>> 20181126 取得左邊超音波的距離 
  float duration_L; //duration to record the time of every back and forth  
  float distance_L;  
  digitalWrite(trigPinL, HIGH); //trigger sona for 1 ms 
  delayMicroseconds(1000);  
  digitalWrite(trigPinL, LOW); 
  duration_L = pulseIn(echoPinL, HIGH);  
  //use pulseIn() function to get the time of pin echo from HIGH to LOW, unit=microseconds 
  distance_L = (duration_L / 2) * 0.034; 
  //distance = ( half of time of back and forth )x( wave velocity(use 0.034 cm per microsecond) ) 
 
  // >>> 20181126 取得左邊超音波的距離 
  distanceL  = ( pingL() / 2 ) * 0.034; 
   
  Serial.print("DistanceL ="); //show result on Serial Monitor 
  Serial.print(distance_L); 
  Serial.println("cm"); 
  delay(delay_time); 
 
// int sendData = (int) (distance * 100); //times 100 and convert disance to integer  
//  byte packet[3];  
//  packet[0] = 97; //key send to phone 
//  packet[1] = sendData / 256; //divides sendData to two 1 byte packets 
//  packet[2] = sendData % 256; 
   
  int sendDataL = (int) (distanceL* 100); 
  byte packet[6]; 
  packet[0] = 97; //key send to phone 
  packet[1] = sendDataL / 256; //divides sendData to two 1 byte packets 
  packet[2] = sendDataL % 256; 
  // <<<  20181126 取得左邊超音波的距離 
 
   
  // >>> 20181126 取得右邊超音波的距離  
  float duration_R; //duration to record the time of every back and forth  
  float distance_R;  
  digitalWrite(trigPinR, HIGH); //trigger sona for 1 ms 
  delayMicroseconds(1000);  
  digitalWrite(trigPinR, LOW); 
  duration_R = pulseIn(echoPinR, HIGH);  
  //use pulseIn() function to get the time of pin echo from HIGH to LOW, unit=microseconds 
  distance_R = (duration_R / 2) * 0.034; 
  //distance = ( half of time of back and forth )x( wave velocity(use 0.034 cm per microsecond) ) 
  //delay(delay_time); 
  
  // >>> 20181126 取得左邊超音波的距離 
  distanceR  = ( pingR() / 2 ) * 0.034; 
   
  Serial.print("DistanceR ="); //show result on Serial Monitor 
  Serial.print(distance_R); 
  Serial.println("cm"); 
  delay(delay_time); 
 
// int sendData = (int) (distance * 100); //times 100 and convert disance to integer  
//  byte packet[3];  
//  packet[0] = 97; //key send to phone 
//  packet[1] = sendData / 256; //divides sendData to two 1 byte packets 
//  packet[2] = sendData % 256; 
   
  int sendDataR = (int) (distanceR* 100); 
//  byte packet[3]; 
  packet[3] = 98; //key send to phone 
  packet[4] = sendDataR / 256; //divides sendData to two 1 byte packets 
  packet[5] = sendDataR % 256; 
  // <<< 20181126 取得右邊超音波的距離  
    
  if(I2CBT.available() > 0) //check BT is succeed 
    if(I2CBT.read() == 97) //check recieve key from phone 
    { 
      Serial.println("succeed!"); 
      for(int i = 0; i < 6; i++)  
        I2CBT.write(packet[i]); //send packet to phone 
    } 
value= analogRead(AOUTpin); //酒精偵測 
limit= digitalRead(DOUTpin); 
Serial.print("Alcohol value: "); 
Serial.println(value);//prints the alcohol value 
Serial.print("Limit: "); 
Serial.print(limit); 
Serial.print("\n"); 
delay(1000); 
 
if (250<value){ 
 for(i=0; i<80; i++) {              // 輸出一個頻率的聲音(酒精  蜂鳴器) 
    digitalWrite(buzzerPin, HIGH);   // 發聲音 
    delay(3);                        // 延遲  
    digitalWrite(buzzerPin, LOW);    // 不發聲音 
    delay(3);                        // 延時  
  }  
   
  for(i=0; i<100; i++) {             // 輸出另一個頻率的聲音  
    digitalWrite(buzzerPin, HIGH);   // 發聲音 
    delay(3);                        // 延時  
    digitalWrite(buzzerPin, LOW);    // 不發聲音 
    delay(1);                        // 延時  
} 
} 
else{ 
digitalWrite(buzzerPin, LOW); 
} 
if (1000 > distance_L){          // LED燈條 
digitalWrite(LEDPin,HIGH); 
} 
else{ 
digitalWrite(LEDPin,LOW); 
} 
if (1000 >distance_R){ 
digitalWrite(LEDPin,HIGH); 
} 
else{ 
digitalWrite(LEDPin,LOW); 
}   
 byte Data[3]; 
    byte cmmd[20]; 
    int insize; 
    char str[256]; 
    int i=analogRead(A0);//read sensor value 
    serialA=I2CBT.read(); 
    Data[0]='a'; 
    Data[1]=i/256; 
    Data[2]=i%256; 
    sprintf(str,"i: %d 0: %d 1: %d 2: %d",i,Data[0],Data[1],Data[2]); 
     Serial.println(str); 
     if (serialA == 49){ 
         for(int j=0;j<3;j++) 
         I2CBT.write(Data[j]); 
         serialA=0; 
      }    
} 
 
 |