Robofun 機器人論壇

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

有關於遙控車跟超音波的合併

[複製鏈接]
發表於 2017-10-11 17:58:45 | 顯示全部樓層 |閱讀模式
本帖最後由 leo0983405269 於 2017-10-11 18:03 編輯

合併前
遙控車:
const int Motor_E2 = 11;
const int Motor_E1 = 10;   
const int Motor_M1 = 12;  
const int Motor_M2 = 13;  
char val;  // variable to receive data from the serial port(bluetooth)

void setup()  
{
  // Start serial communication at 9600
  Serial.begin(9600);
  pinMode(Motor_M1, OUTPUT);
  pinMode(Motor_M2, OUTPUT);
}

void loop()
{
  if(Serial.available()>0)
  {
    val = Serial.read();
    switch(val)
    {
      case 'f':   // car forward
                forward(0, 255);
                break;
      case 'd':   // car backward
                backward(0, 255);
                break;
      case 'l':   // car turn left
                left(0, 255);
                break;
      case 'r':   // car turn right
                right(0, 255);
                break;
      case 's':   // car stop
                motorstop(0, 0);
                break;
    }     
  }
}

void motorstop(byte flag, byte motorspeed)
{
  Serial.println("Stop!");
  digitalWrite( Motor_E1, motorspeed);
  digitalWrite( Motor_E2, motorspeed);
}

void forward(byte flag, byte motorspeed)
{
  Serial.println("Forward!");

  digitalWrite( Motor_M1, HIGH);
  digitalWrite( Motor_M2, HIGH);

  analogWrite( Motor_E1, 200);
  analogWrite( Motor_E2, 200);
}

void backward(byte flag, byte motorspeed)
{
  Serial.println("Backward!");

  digitalWrite( Motor_M1, LOW);
  digitalWrite( Motor_M2, LOW);

  analogWrite( Motor_E1, 200);
  analogWrite( Motor_E2, 200);
}

void right(byte flag, byte motorspeed)
{
  Serial.println("Turn Right! ");

  digitalWrite( Motor_M1, HIGH);
  digitalWrite( Motor_M2, HIGH);

  analogWrite( Motor_E1, 50);
  analogWrite( Motor_E2, 0);
}

void left(byte flag, byte motorspeed)
{
  Serial.println("Turn Left!");

  digitalWrite( Motor_M1, HIGH);
  digitalWrite( Motor_M2, HIGH);

  analogWrite( Motor_E1, 0);
  analogWrite( Motor_E2, 50);
}
超音波:
#include <SoftwareSerial.h> //Arduino I2C library

const int trig =  8; //define Arduino pin
const int echo =  7;
const int TX   = 1;
const int RX   = 0;

const int delay_time = 1000; //delay 1000 ms for every measurment

SoftwareSerial BT(RX, TX);
//set Bluetooth RX (-> pin TX of Bluetooth Module), TX (-> pin RX of Bluetooth Module)

void setup() {
  //Serial.begin(9600); //set baud rate of Serial Monitor
  BT.begin(9600); //set baud rate of Bluetooth
  pinMode(trig, OUTPUT); //set trigger pin to OUTPUT (-> pin trig of Ultrasonic Sensor)
  pinMode(echo, INPUT); //set echo pin to INPUT (-> pin echo of Ultrasonic Sensor)
  //Serial.println("Start!!!");
}

void loop() {
  float duration; //duration to record the time of every back and forth
  float distance;
  digitalWrite(trig, HIGH); //trigger sona for 1 ms
  delayMicroseconds(1000);
  digitalWrite(trig, LOW);
  duration = pulseIn(echo, HIGH);
  //use pulseIn() function to get the time of pin echo from HIGH to LOW, unit=microseconds
  distance = (duration / 2) * 0.034;
  //distance = ( half of time of back and forth )x( wave velocity(use 0.034 cm per microsecond) )
  
  //Serial.print("Distance ="); //show result on Serial Monitor
  //Serial.print(distance);
  //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;
  

  
  if(BT.available() > 0) //check BT is succeed
    if(BT.read() == 97) //check recieve key from phone
    {
      Serial.println("succeed!");
      for(int i = 0; i < 3; i++)
        BT.write(packet); //send packet to phone
    }
}
上次新手大大提供把serial//掉我就直接沒在合併上面放進去了

合併後
#include <SoftwareSerial.h> //Arduino I2C library

const int trig =  8; //define Arduino pin
const int echo =  7;
const int TX   = 1;
const int RX   = 0;
const int delay_time = 1000; //delay 1000 ms for every measurment
SoftwareSerial BT(RX, TX);
//set Bluetooth RX (-> pin TX of Bluetooth Module), TX (-> pin RX of Bluetooth Module)

const int Motor_E2 = 11;   
const int Motor_E1 = 10;  
const int Motor_M1 = 12;   
const int Motor_M2 = 13;   
char val;  // variable to receive data from the serial port(bluetooth)

void setup()  
{
  // Start serial communication at 9600
  BT.begin(9600); //set baud rate of Bluetooth
  pinMode(Motor_M1, OUTPUT);
  pinMode(Motor_M2, OUTPUT);
  pinMode(trig, OUTPUT); //set trigger pin to OUTPUT (-> pin trig of Ultrasonic Sensor)
  pinMode(echo, INPUT); //set echo pin to INPUT (-> pin echo of Ultrasonic Sensor)
}

void loop()
{
  float duration; //duration to record the time of every back and forth
  float distance;
  digitalWrite(trig, HIGH); //trigger sona for 1 ms
  delayMicroseconds(1000);
  digitalWrite(trig, LOW);
  duration = pulseIn(echo, HIGH);
  //use pulseIn() function to get the time of pin echo from HIGH to LOW, unit=microseconds
  distance = (duration / 2) * 0.034;
  //distance = ( half of time of back and forth )x( wave velocity(use 0.034 cm per microsecond) )
  
  delay(1000);
  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;

   if(BT.available() > 0) //check BT is succeed
    if(BT.read() == 97) //check recieve key from phone
    {
       for(int i = 0; i < 3; i++)
        BT.write(packet); //send packet to phone
    }
  
  if(BT.available()>0)
  {
    val = BT.read();
    switch(val)
    {
      case 'f':   // car forward
                forward(0, 255);
                break;
      case 'd':   // car backward
                backward(0, 255);
                break;
      case 'l':   // car turn left
                left(0, 255);
                break;
      case 'r':   // car turn right
                right(0, 255);
                break;
      case 's':   // car stop
                motorstop(0, 0);
                break;
    }     
  }
}

void motorstop(byte flag, byte motorspeed)
{
  Serial.println("Stop!");
  digitalWrite( Motor_E1, motorspeed);
  digitalWrite( Motor_E2, motorspeed);
}

void forward(byte flag, byte motorspeed)
{
  Serial.println("Forward!");

  digitalWrite( Motor_M1, HIGH);
  digitalWrite( Motor_M2, HIGH);

  analogWrite( Motor_E1, 200);
  analogWrite( Motor_E2, 200);
}

void backward(byte flag, byte motorspeed)
{
  Serial.println("Backward!");

  digitalWrite( Motor_M1, LOW);
  digitalWrite( Motor_M2, LOW);

  analogWrite( Motor_E1, 200);
  analogWrite( Motor_E2, 200);
}

void right(byte flag, byte motorspeed)
{
  Serial.println("Turn Right! ");

  digitalWrite( Motor_M1, HIGH);
  digitalWrite( Motor_M2, HIGH);

  analogWrite( Motor_E1, 50);
  analogWrite( Motor_E2, 0);
}

void left(byte flag, byte motorspeed)
{
  Serial.println("Turn Left!");

  digitalWrite( Motor_M1, HIGH);
  digitalWrite( Motor_M2, HIGH);

  analogWrite( Motor_E1, 0);
  analogWrite( Motor_E2, 50);
}

請問是哪裡合錯了,兩個功能都出不來
編譯過是沒有錯誤
我現在有點擔心因為手機控制遙控車是 手機對車子  超音波的值是模組對手機
這樣會不會衝突導致沒辦法成功
還是我手機程式沒寫好,不好意思傷害幫忙的人的眼睛了。

如果有單獨遙控車或超音波的問題可以私訊我,我能力範圍內大家一起討論感謝


發表於 2017-10-11 20:28:47 來自手機 | 顯示全部樓層
本帖最後由 超新手 於 2017-10-12 08:19 編輯

1.serial 沒有完全拿掉
2.bt.available沒有整合在一起
應該把 case 97: 整合進去
3.雖然結果相同
但是最好的做法是
把 bt 的部份都改成 serial
也就是拿掉// SoftwareSerial BT(RX, TX);
然後把所有的 BT 改成 Serial
用硬體 uart 取代軟體 uart, 速度較快
當初沒注意到這一點
 樓主| 發表於 2017-10-16 16:41:25 | 顯示全部樓層
本帖最後由 leo0983405269 於 2017-10-16 16:55 編輯

我先在改改看   請問你說把BT改成Serial
可是不是要把serial都要拿掉嗎?

如果把有關SOFTWARE拿掉  那#include <SoftwareSerial.h> //Arduino I2C library也把它拿掉?
 樓主| 發表於 2017-10-16 17:06:47 | 顯示全部樓層
本帖最後由 leo0983405269 於 2017-10-16 17:19 編輯

#include <SoftwareSerial.h> //Arduino I2C library

const int trig =  8; //define Arduino pin
const int echo =  7;
const int TX   = 1;
const int RX   = 0;
const int delay_time = 1000; //delay 1000 ms for every measurment

const int Motor_E2 = 11;      
const int Motor_E1 = 10;   
const int Motor_M1 = 12;   
const int Motor_M2 = 13;   
char val;  // variable to receive data from the serial port(bluetooth)

void setup()  
{
  Serial.begin(9600); //set baud rate of Bluetooth
  pinMode(Motor_M1, OUTPUT);
  pinMode(Motor_M2, OUTPUT);
  pinMode(trig, OUTPUT); //set trigger pin to OUTPUT (-> pin trig of Ultrasonic Sensor)
  pinMode(echo, INPUT); //set echo pin to INPUT (-> pin echo of Ultrasonic Sensor)
}

void loop()
{
  float duration; //duration to record the time of every back and forth
  float distance;
  digitalWrite(trig, HIGH); //trigger sona for 1 ms
  delayMicroseconds(1000);
  digitalWrite(trig, LOW);
  duration = pulseIn(echo, HIGH);
  //use pulseIn() function to get the time of pin echo from HIGH to LOW, unit=microseconds
  distance = (duration / 2) * 0.034;
  //distance = ( half of time of back and forth )x( wave velocity(use 0.034 cm per microsecond) )
  
  delay(1000);
  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;

   
  if(Serial.available()>0)
  {
    val = Serial.read();
    switch(val)
    {
      case 'q'://測距離
      case 'f':   // car forward
                forward(0, 255);
                break;
      case 'd':   // car backward
                backward(0, 255);
                break;
      case 'l':   // car turn left
                left(0, 255);
                break;
      case 'r':   // car turn right
                right(0, 255);
                break;
      case 's':   // car stop
                motorstop(0, 0);
                break;
    }     
  }
}

void motorstop(byte flag, byte motorspeed)
{
  Serial.println("Stop!");
  digitalWrite( Motor_E1, motorspeed);
  digitalWrite( Motor_E2, motorspeed);
}

void forward(byte flag, byte motorspeed)
{
  Serial.println("Forward!");

  digitalWrite( Motor_M1, HIGH);
  digitalWrite( Motor_M2, HIGH);

  analogWrite( Motor_E1, 200);
  analogWrite( Motor_E2, 200);
}

void backward(byte flag, byte motorspeed)
{
  Serial.println("Backward!");

  digitalWrite( Motor_M1, LOW);
  digitalWrite( Motor_M2, LOW);

  analogWrite( Motor_E1, 200);
  analogWrite( Motor_E2, 200);
}

void right(byte flag, byte motorspeed)
{
  Serial.println("Turn Right! ");

  digitalWrite( Motor_M1, HIGH);
  digitalWrite( Motor_M2, HIGH);

  analogWrite( Motor_E1, 50);
  analogWrite( Motor_E2, 0);
}

void left(byte flag, byte motorspeed)
{
  Serial.println("Turn Left!");

  digitalWrite( Motor_M1, HIGH);
  digitalWrite( Motor_M2, HIGH);

  analogWrite( Motor_E1, 0);
  analogWrite( Motor_E2, 50);
}

如果照著我的理解  是修改成這樣嗎

{
       for(int i = 0; i < 3; i++)
        Serial.write(packet); //send packet to phone
    }
這一段要放在哪裡?

還有這樣原本的TX,RX 不用設定了嗎?
我記得原本遙控車就沒有寫到關於TX跟RX腳位了
發表於 2017-10-16 17:22:58 | 顯示全部樓層
本帖最後由 超新手 於 2017-10-16 17:24 編輯

1. Serial 要拿掉的是DEBUG 部份, 像(應該有五個吧?)
這些程式都是給人看的, 它會干擾APP讀值
Serial.println("Stop!");
Serial.println("Turn Left!");
Serial.println("Backward!");
.....
2. 加到 case 中

val = Serial.read();
    switch(val)
    {

      case 'a':   
      for(int i = 0; i < 3; i++)
        Serial.write(packet); //send packet to phone
        break;


    }
               

其中的 97 , 就是 'a', 也就是 0x61

3. 那個 case 'q' 你沒寫程式,
 樓主| 發表於 2017-10-16 17:37:10 | 顯示全部樓層
還是一開始的const int TX =1;
                   const int RX =0;
就設定了
但是如果只有遙控車的話我沒在裡面找到有關藍芽的腳位設定
 樓主| 發表於 2017-10-16 17:54:09 | 顯示全部樓層
本帖最後由 leo0983405269 於 2017-10-16 18:00 編輯
超新手 發表於 2017-10-16 17:22
1. Serial 要拿掉的是DEBUG 部份, 像(應該有五個吧?)
這些程式都是給人看的, 它會干擾APP讀值
Serial.print ...


q  就是我把97改成英文字


我有在論壇找到前人的問題

如果打'97'好像不會變顏色

所以我改成q

如果把
if(Serial.available()>0)
  {
    val = Serial.read();
    switch(val)
    {
      case 'q':   //測距離
      for(int i = 0; i < 3; i++)
        Serial.write(packet); //send packet to phone
        break;
      case 'f':   // car forward
                forward(0, 255);
                break;
      case 'd':   // car backward
                backward(0, 255);
                break;
      case 'l':   // car turn left
                left(0, 255);
                break;
      case 'r':   // car turn right
                right(0, 255);
                break;
      case 's':   // car stop
                motorstop(0, 0);
                break;
    }     
  }
}

會出現call of overladed write(byte [3])' is ambiguous  調用write(byte [3])'是不明確的
是不應該write嗎?
 樓主| 發表於 2017-10-16 18:04:30 | 顯示全部樓層
本帖最後由 leo0983405269 於 2017-10-16 18:07 編輯

所以干擾讀值得是 Serial.println這種的
                           Serial.begin是要留下來跟藍芽連線的
發表於 2017-10-16 20:01:32 | 顯示全部樓層
本帖最後由 超新手 於 2017-10-17 12:50 編輯

1. 必須指定藍芽鮑率
所以 Serial.begin(9600); 不能拿掉
2. 97 是數字, 是 a 的 ASCII CODE(十進位)
所以,不能加引號,字元才要加
case 97:
等於
case 0x61:
等於
case 'a':
三種寫法是相同的, 它是數字, 不會變色也是正常的
但你要改成 'a' 而不是 'q'
不然 APP 那端也要一起改

3. 應該是
  for(int i = 0; i < 3; i++)
        Serial.write(packet[ i]);
不是我寫錯,而是這個論壇的 bug
看看你自己貼的這幾行就知道了
貼出的程式會被論壇改掉
 樓主| 發表於 2017-10-17 17:28:01 | 顯示全部樓層
可以遙控車子側距離了    真的很感謝  已經卡住很久了
只是好像會有點延遲   比如手機端按往前  有時候要多按幾下  是因為測距離送出來的訊號跟遙控車送出來的訊號撞到了嗎?
那如果還想要加入溫度跟溼度的感應  會送更多訊號 這樣是不是會有更大的延遲?
發表於 2017-10-17 18:04:32 | 顯示全部樓層
本帖最後由 超新手 於 2017-10-17 18:26 編輯

延遲可能是因為
1. APP 端 clock1 的速度太快
時間可以調長一點,
   
2. 你加了 delay(1000);
    所以你按再快, sonar 和 motor 也是一秒處理一次
    這個值可以縮短些

3. pulseIn(pin, value)
沒設timeout 時間, 所以一旦沒收到超音波
它"有機會"會卡住一秒鐘的時間

4. 可以把
if(Serial.available()>0)
  {
改成
while(Serial.available()>0)
  {
讓它一次就把資料處理完, 不會卡一大堆資料,
一秒(或很久)才處理一次


可能要自己改看看才知道為什麼
 樓主| 發表於 2017-10-17 19:05:16 | 顯示全部樓層
好  我在改改看  感恩
 樓主| 發表於 2017-10-17 19:31:18 | 顯示全部樓層
如果要在多新的感應器 就造著超音波合併的方法然後在appinventor2在拉clock就可以了嗎?
還是要寫在同一個clock裡面阿?
發表於 2017-10-18 08:28:47 | 顯示全部樓層
寫在同一個即可, 也比較單純
 樓主| 發表於 2017-10-18 15:57:39 | 顯示全部樓層
好  等我買元件來嘗試 謝謝你
您需要登錄後才可以回帖 登錄 | 申請會員

本版積分規則

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

GMT+8, 2024-3-29 16:07 , Processed in 0.084529 second(s), 6 queries , Apc On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

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