2016年8月3日 星期三

nluetooth car


arduino    bluetooth code

其實蠻簡單的就是透過uart 去讀BT...


這邊用了D2和D3 來當software serial

#define RxD 2
#define TxD 3
SoftwareSerial blueToothSerial(RxD,TxD);


const int motorIn1 = 4;
const int motorIn2 = 5;
const int motorIn3 = 6;    
const int motorIn4 = 7;


在setup 這邊...用了四根pin 來控制L298N 的版子

分別是 D4,D5,D6,D7


void setup()
{
  pinMode(motorIn1, OUTPUT);
  pinMode(motorIn2, OUTPUT);
  pinMode(motorIn3, OUTPUT);
  pinMode(motorIn4, OUTPUT);
  Serial.begin(9600); //  可以接到pc ...看debug message  
  blueToothSerial.begin(9600);  // 和HC-05 也是用9600 baud-rate
  Motion_val=0;  
  FLAG=0;
  len=0;
}

在loop 的程式裡面...就是執行 UART_FUN()...  看看BT 模組有沒有接收到訊息....

有接收到的話...就是執行相對應的code...

void loop()
{
  UART_FUN();

  if(Motion_val==KEY_NOTHING)
  {
  motorstop();

  }
  else if(Motion_val==KEY_UP)
  {
forward();

  }
  else if(Motion_val==KEY_DOWN)
  {
backward();

  }
  else if(Motion_val==KEY_RIGHT)
  {
  right();

  }
  else if(Motion_val==KEY_LEFT)
  {
  left();

  }


}


//  這邊就是把相對應的code 轉換成相對應的pin 輸出

void motorstop()
{
  digitalWrite(motorIn1, LOW);
  digitalWrite(motorIn2, LOW);
  digitalWrite(motorIn3, LOW);
  digitalWrite(motorIn4, LOW);
}

void forward()
{
  digitalWrite(motorIn1, HIGH);
  digitalWrite(motorIn2, LOW);
  digitalWrite(motorIn3, HIGH);
  digitalWrite(motorIn4, LOW);
}

void backward()
{
  digitalWrite(motorIn1, LOW);
  digitalWrite(motorIn2, HIGH);
  digitalWrite(motorIn3, LOW);
  digitalWrite(motorIn4, HIGH);
}

// Let right motor keep running, but stop left motor
void right()
{
  digitalWrite(motorIn1, HIGH);
  digitalWrite(motorIn2, LOW);
  digitalWrite(motorIn3, LOW);
  digitalWrite(motorIn4, LOW);
}

// Let left motor keep running, but stop right motor
void left()
{
  digitalWrite(motorIn1, LOW);
  digitalWrite(motorIn2, LOW);
  digitalWrite(motorIn3, HIGH);
  digitalWrite(motorIn4, LOW);
}



重點就是這個UART_FUNC()

一開始先 把 Buffer_temp array 清成 0

然後 delay (100);

然後check  blueToothSerial.available()

如果為一...就去抓  dara()

 Receive_val=blueToothSerial.read();

如果是0xAA..  代表是第一個傳輸的byte....

至於傳輸的資料格式如下:

key_up:
byte[] send = new byte[] {(byte) 0xAA, (byte) 0xBB, 0x02, (byte) 0xF1, 0x03};

key_down:
byte[] send = new byte[] {(byte) 0xAA, (byte) 0xBB, 0x02, (byte) 0xF2, 0x00};

key_left
byte[] send = new byte[] {(byte) 0xAA, (byte) 0xBB, 0x02, (byte) 0xF8, 0x0A};

key_right
byte[] send = new byte[] {(byte) 0xAA, (byte) 0xBB, 0x02, (byte) 0xF4, 0x06};


所以可以看到下面的code 會去check   byte[0], byte[1], byte[3] ..然後解碼出相對應的string value


當blueToothSerial.available() 變成0 後....代表傳輸已經結束....所以離開這個while loop


void UART_FUN()
{

  for(i=0;i<5;i++)
   Buffer_temp[i]=0x0;
    delay(100);
  while(blueToothSerial.available())          
  {                                              
     Receive_val=blueToothSerial.read();
      //Serial.print(Receive_val);
     // Serial.print("\n");
   

     if(Receive_val==0xAA)
     {
      len=0;
      Buffer_temp[len++]=Receive_val;
     }
     else if(len < 5)
     {
      Buffer_temp[len++]=Receive_val;
     }
   
  }

  if(Buffer_temp[0]==0xAA && Buffer_temp[1]==0xBB && Buffer_temp[3]==0xF1)
  {
   Serial.print("UP \n");
   Motion_val=KEY_UP;
   }
   else if(Buffer_temp[0]==0xAA && Buffer_temp[1]==0xBB && Buffer_temp[3]==0xF2)
  {
   Serial.print("DOWN \n");
   Motion_val=KEY_DOWN ;
   }
   else if(Buffer_temp[0]==0xAA && Buffer_temp[1]==0xBB && Buffer_temp[3]==0xF4)
  {
   Serial.print("RIGHT \n");
   Motion_val=KEY_RIGHT ;
   }
  else   if(Buffer_temp[0]==0xAA && Buffer_temp[1]==0xBB && Buffer_temp[3]==0xF8)
  {
   Serial.print("LEFT \n");
   Motion_val=KEY_LEFT ;
   }
   else   if(Buffer_temp[0]==0xAA && Buffer_temp[1]==0xBB && Buffer_temp[3]==0x00)
  {
   Serial.print("KEY_NOTHING \n");
   Motion_val=KEY_NOTHING ;
   }




   




}





3 則留言:

  1. 作者您好可以請教您自走車程式設計方面的問題嗎
    az60425@gmail.com
    line:godgood321

    回覆刪除
  2. 作者您好可以請教您自走車程式設計方面的問題嗎
    az60425@gmail.com
    line:godgood321

    回覆刪除
  3. カジノ シークレット カジノ シークレット 카지노 카지노 カジノ シークレット カジノ シークレット クイーンカジノ クイーンカジノ planet win 365 planet win 365 bet365 bet365 815

    回覆刪除