Robofun 機器人論壇

 找回密碼
 申請會員
搜索
熱搜: 活動 交友 discuz
查看: 3662|回復: 13

藍芽合併的問題

[複製鏈接]
發表於 2019-4-11 15:46:36 | 顯示全部樓層 |閱讀模式
不好意思 想請教個問題

int sendDataL = (int) (distanceL* 100);
  byte packet[9];
  packet[0] = 97; //key send to phone
  packet[1] = sendDataL / 256; //divides sendData to two 1 byte packets
  packet[2] = sendDataL % 256;

這是我左邊超音波回傳給APP的ˊ程式碼

  packet[3] = 98; //key send to phone
  packet[4] = sendDataR / 256; //divides sendData to two 1 byte packets
  packet[5] = sendDataR % 256;

這是我左邊超音波回傳給APP的ˊ程式碼

byte cmmd[20];
    int insize;
    char str[256];
    int i=analogRead(A0);//read sensor value
    serialA=I2CBT.read();
    packet[6]='a';
    packet[7]=i/256;
    packet[8]=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<9;j++)
         I2CBT.write(packet[j]);
         serialA=0;
可是我後面多了一個這個感測器的程式碼 要怎麼把他們合併
不合併的話 照這樣執行是不是有時候就會接收不到我下面的感測器程式碼
謝謝各位的解答了,求救。
發表於 2019-4-11 16:07:49 | 顯示全部樓層
那就合併
不過你寫錯了
'a' 等於 97
下面兩行衝到了
packet[0]=97;
packet[6]='a';
改成
packet[6]=99;
 樓主| 發表於 2019-4-11 16:20:10 | 顯示全部樓層

不好意思 前面應該是packet 後面是Data 我用錯個 這樣的合併方式

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;
.

右邊超音波

  packet[3] = 98; //key send to phone
  packet[4] = sendDataR / 256; //divides sendData to two 1 byte packets
  packet[5] = sendDataR % 256;

然後另外一個碰撞感測器是
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;

因為她這樣常常能接收到超音波左右的數據,但是接收不到碰撞感測器的數據,然後因為我發訊訊號是APP是調 'a',所以才會在那行打a

 樓主| 發表於 2019-4-11 16:25:13 | 顯示全部樓層
或者是有方法 可以當我 碰撞感測 接收到碰撞訊號 就發送訊號讓我的APP做出動作嗎
發表於 2019-4-11 16:26:09 | 顯示全部樓層
其實最上面那個程式就差不多是 OK 的
你不如把整個程式 PO 出來

而且我印象中, 之前已經有人 PO 過
OK 的程式
 樓主| 發表於 2019-4-11 16:33:10 | 顯示全部樓層
#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;
      }   
}

發表於 2019-4-11 16:44:34 | 顯示全部樓層
本帖最後由 超新手 於 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
    }
           
}
 樓主| 發表於 2019-4-11 17:04:47 | 顯示全部樓層
不好意思 這樣還是讀取不到我碰撞感應的數據耶
發表於 2019-4-11 17:06:02 | 顯示全部樓層
那你的 APP 是怎麼寫的?
 樓主| 發表於 2019-4-11 17:12:02 | 顯示全部樓層
https://ppt.cc/fipwYx 這是我碰撞感應的APP部分 密碼0000
發表於 2019-4-11 17:16:46 | 顯示全部樓層
本帖最後由 超新手 於 2019-4-11 17:26 編輯

這個 APP...根本是沒寫那部份
怎麼可能收的到
應該說, 它只能收到碰撞資料
收不到超音波資料吧?
 樓主| 發表於 2019-4-11 17:33:25 | 顯示全部樓層
APP 我沒貼超音波的部分 因為超音波部分是OK的 但是 如果我把我最一開始的碰撞程式碼 獨立運行又跟APP配合的話 他是可以執行的
發表於 2019-4-11 17:55:15 | 顯示全部樓層
發生碰撞時, 序列監控視窗顯示什麼?
i:? 0:? 1:? 2:?
APP 的碰撞感測器的數字又顯示多少?
 樓主| 發表於 2019-4-11 18:44:05 | 顯示全部樓層
碰撞發生時 視窗 會 寫 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[6] = 99; //key send to phone
  packet[7] = sendDataR / 256; //divides sendData to two 1 byte packets
  packet[8] = sendDataR % 256;

這樣
您需要登錄後才可以回帖 登錄 | 申請會員

本版積分規則

小黑屋|手機版|Archiver|機器人論壇 from 2005.07

GMT+8, 2024-3-29 13:36 , Processed in 0.238126 second(s), 6 queries , Apc On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表