本帖最後由 超新手 於 2019-4-11 16:50 編輯
這樣改還是有問題, 但正確多了
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;
}
上面的程式, 移到這邊來, 並小改
if(serialA == 97) //check recieve key from phone
{
Serial.println("succeed!");
for(int i = 0; i < 6; i++)
I2CBT.write(packet[ i]); //send packet to phone
}
} |