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) )
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 取得左邊超音波的距離
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) )
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);