藍芽合併的問題
不好意思 想請教個問題int sendDataL = (int) (distanceL* 100);
byte packet;
packet = 97; //key send to phone
packet = sendDataL / 256; //divides sendData to two 1 byte packets
packet = sendDataL % 256;
這是我左邊超音波回傳給APP的ˊ程式碼
packet = 98; //key send to phone
packet = sendDataR / 256; //divides sendData to two 1 byte packets
packet = sendDataR % 256;
這是我左邊超音波回傳給APP的ˊ程式碼
byte cmmd;
int insize;
char str;
int i=analogRead(A0);//read sensor value
serialA=I2CBT.read();
packet='a';
packet=i/256;
packet=i%256;
//sprintf(str,"i: %d 0: %d 1: %d 2: %d",i,Data,Data,Data);
// Serial.println(str);
if (serialA == 49){
for(int j=0;j<9;j++)
I2CBT.write(packet);
serialA=0;
可是我後面多了一個這個感測器的程式碼 要怎麼把他們合併
不合併的話 照這樣執行是不是有時候就會接收不到我下面的感測器程式碼
謝謝各位的解答了,求救。 那就合併
不過你寫錯了
'a' 等於 97
下面兩行衝到了
packet=97;
packet='a';
改成
packet=99;
不好意思 前面應該是packet 後面是Data 我用錯個 這樣的合併方式
int sendDataL = (int) (distanceL* 100);
byte packet;
packet = 97; //key send to phone
packet = sendDataL / 256; //divides sendData to two 1 byte packets
packet = sendDataL % 256;
.
右邊超音波
packet = 98; //key send to phone
packet = sendDataR / 256; //divides sendData to two 1 byte packets
packet = sendDataR % 256;
然後另外一個碰撞感測器是
byte Data;
byte cmmd;
int insize;
char str;
int i=analogRead(A0);//read sensor value
serialA=I2CBT.read();
Data='a';
Data=i/256;
Data=i%256;
sprintf(str,"i: %d 0: %d 1: %d 2: %d",i,Data,Data,Data);
Serial.println(str);
if (serialA == 49){
for(int j=0;j<3;j++)
I2CBT.write(Data);
serialA=0;
因為她這樣常常能接收到超音波左右的數據,但是接收不到碰撞感測器的數據,然後因為我發訊訊號是APP是調 'a',所以才會在那行打a
或者是有方法 可以當我 碰撞感測 接收到碰撞訊號 就發送訊號讓我的APP做出動作嗎 其實最上面那個程式就差不多是 OK 的
你不如把整個程式 PO 出來
而且我印象中, 之前已經有人 PO 過
OK 的程式
#include <SoftwareSerial.h> //Arduino I2C library
#include <Wire.h>
// >>> 20181126
const inttrigPinL = 4 ; //左邊超音波 觸發腳Trig
const intechoPinL = 5 ; //左邊超音波 接收腳 Echo
unsigned long distanceL ; // 距離 cm
// >>> 20181126 取得又邊超音波的距離設定
const inttrigPinR = 2 ; // 右邊超音波 觸發腳Trig
const intechoPinR = 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;
//packet = 97; //key send to phone
//packet = sendData / 256; //divides sendData to two 1 byte packets
//packet = sendData % 256;
int sendDataL = (int) (distanceL* 100);
byte packet;
packet = 97; //key send to phone
packet = sendDataL / 256; //divides sendData to two 1 byte packets
packet = 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;
//packet = 97; //key send to phone
//packet = sendData / 256; //divides sendData to two 1 byte packets
//packet = sendData % 256;
int sendDataR = (int) (distanceR* 100);
//byte packet;
packet = 98; //key send to phone
packet = sendDataR / 256; //divides sendData to two 1 byte packets
packet = 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); //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;
byte cmmd;
int insize;
char str;
int i=analogRead(A0);//read sensor value
serialA=I2CBT.read();
Data='a';
Data=i/256;
Data=i%256;
sprintf(str,"i: %d 0: %d 1: %d 2: %d",i,Data,Data,Data);
Serial.println(str);
if (serialA == 49){
for(int j=0;j<3;j++)
I2CBT.write(Data);
serialA=0;
}
}
本帖最後由 超新手 於 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;
//packet = 97; //key send to phone
//packet = sendData / 256; //divides sendData to two 1 byte packets
//packet = sendData % 256;
int sendDataL = (int) (distanceL* 100);
byte packet;
packet = 97; //key send to phone
packet = sendDataL / 256; //divides sendData to two 1 byte packets
packet = 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;
//packet = 97; //key send to phone
//packet = sendData / 256; //divides sendData to two 1 byte packets
//packet = sendData % 256;
int sendDataR = (int) (distanceR* 100);
//byte packet;
packet = 98; //key send to phone
packet = sendDataR / 256; //divides sendData to two 1 byte packets
packet = 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;
byte cmmd;
int insize;
char str;
int i=analogRead(A0);//read sensor value
serialA=I2CBT.read();
Data='a';
Data=i/256;
Data=i%256;
sprintf(str,"i: %d 0: %d 1: %d 2: %d",i,Data,Data,Data);
Serial.println(str);
if (serialA == 49){
for(int j=0;j<3;j++)
I2CBT.write(Data);
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
}
} 不好意思 這樣還是讀取不到我碰撞感應的數據耶 那你的 APP 是怎麼寫的? https://ppt.cc/fipwYx 這是我碰撞感應的APP部分 密碼0000 本帖最後由 超新手 於 2019-4-11 17:26 編輯
這個 APP...根本是沒寫那部份
怎麼可能收的到
應該說, 它只能收到碰撞資料
收不到超音波資料吧?
APP 我沒貼超音波的部分 因為超音波部分是OK的 但是 如果我把我最一開始的碰撞程式碼 獨立運行又跟APP配合的話 他是可以執行的 發生碰撞時, 序列監控視窗顯示什麼?
i:? 0:? 1:? 2:?
APP 的碰撞感測器的數字又顯示多少? 碰撞發生時 視窗 會 寫 touch : 0 如果沒有碰撞就是 touch : 1
int Touch = 2;
void setup() {
pinMode(Touch, INPUT);
Serial.begin(9600);
}
void loop() {
int result = digitalRead(Touch);
Serial.print("Touch: ");
Serial.println(result);
delay(100);
}
我是想把這段加進去 然後改成跟超音波的藍芽傳送方式一樣
packet = 99; //key send to phone
packet = sendDataR / 256; //divides sendData to two 1 byte packets
packet = sendDataR % 256;
這樣
頁:
[1]