若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。
If you like this post, please click the ads on the blog or buy me a coffee. Thank you very much.在製作 Arduino 藍牙遙控器之前,我們先來了解藍牙的使用方式,以手機連接HC-06藍牙模組流程約有底下幾個步驟:
Before building a customized bluetooth controller, there are several steps we need to know about using bluetooth communication. For example, the following steps are for connecting HC-06 with a smart phone:
- 開啟手機藍牙。 (Turn on bluetooth on a smart phone)
- 尋找 HC-06 藍牙模組。(Scan for the nearby bluetooth devices)
- 與 HC-06 藍牙模組配對。(Paire HC-06 with the smart phone)
- 開啟手機藍牙App。(Open a BT application on the smart phone)
- 使用手機藍牙App與 HC-06 藍牙模組連線。(Use the BT application to connect with HC-06)
The role of smart phone is bluetooth master. The role of HC-06 is bluetooth slave. HC-05(master) have to store the bluetooth master configuration in itself. After saving the bluetooth master configuration in HC-05(master), it will connect to HC-06(slave) automatically.
For more details, go to "Connecting 2 Arduinos by Bluetooth using a HC-05 and a HC-06: Pair, Bind, and Link".
此外主控端與從端的速度(baudrate)要一樣,密碼(password)也要一樣。此文所用的 baudrate為 9600,password為 1234。
The baudrate and password must be the same in BT master/slave. In this tutorial, the baudrate is 9600 and the password is 1234.
二、硬體材料(Materials)
- Arduino Nano 板子 x 1
- Arduino UNO 板子 x 1
- Arduino Sensor Shield V5.0 感測器擴展板 x 1
- NFU Dream Maker 所設計的 PCB from NUF Dream Maker x 1
- RGB LED x 3
- 電阻 Resistor 220 ohm x 3
- HC-05 藍牙模組 x 1
- HC-06 藍牙模組 x 1
- 雙軸按鍵搖桿 Joystick x 1
- 母隊母杜邦線 Female to Female Jumper x 8
設定藍牙模組的電路與AT指令請參考 HC-05與HC-06藍牙模組補充說明(二):連接電路
與 HC-05與HC-06藍牙模組補充說明(三):使用Arduino設定AT命令 兩篇文章。
HC-06的設定值 (For HC-06)
所需的指令如下(The AT Commands List):
紅色 1 OK 是 AT 檢查裝置指令回傳結果。(RED 1 OK is the return value of the AT command.)
藍色 2 OKsetPIN 是 AT+PIN1234 設定密碼指令回傳結果。(Blue 2 OKsetPIN is the return value of the AT+PIN1234 command.)
綠色 3 OKsetname 是 AT+NAMEPingLun 設定藍芽名稱指令回傳結果。(Green 3 OKsetname is the return value of the AT+NAMEPingLun command.)
紫色 4 OK9600 是 AT+BAUD4 設定傳輸速度指令回傳結果。(Purple 4 OK9600 is the return value of the AT+BAUD4 command.)
接著可用 Bluetooth Scanner 來找出 HC-06 模組的位址(Address)。
Use Bluetooth Scanner to figure out the MAC address of HC-06.
注:上圖中的位址需轉為 98D3,31,FB5396 的格式,才能給 AT 指令用,此部分可的說明請參考 藍牙模組補充說明(四):無線連結兩個Arduino控制板一文中的藍芽裝置位址一節。
PS: the MAC address must be the 98D3,31,FB5396 for AT command.
HC-05的設定值(For HC-05)
要用AT指令讓HC-05主控端連上HC-06從端時,需將HC-05切換到指令模式,這需要在上電前壓住按鈕(如下圖用夾子夾住),注意 HC-05在 指令模式下的速度為 38400 。
Hold the reset button on the HC-05 before power on. This will enable HC-05 in command mode. A plastic peg could hold the reset button for you.
藍牙模組互聯連線成功影片(注意燈號閃爍的速度):
The successful connection video(notice the onboard LED blinking speed)
尚未連線前:HC-06 與 HC-05 的指示燈會快速閃爍。
Before connection: the HC-06 and HC-05 onboard LED blink fast.
連線成功後:HC-06 的指示燈會恆亮;HC-05 的指示燈每兩秒快速閃爍兩次。
After connection: the HC-06 onboard will turn on, not blinking; the HC-05 will blink twice every two seconds.
If you have any problem to pair HC-05 with HC-06, please refer to this tutorial: "How to Configure HC-05 Bluetooth Module As Master and Slave Via AT Command".
四、HC-06 從端藍牙小夜燈程式 (The Code for HC-06 and Nano)
參考資料:
[1] HC-05與HC-06藍牙模組補充說明(一)
[2] HC-05與HC-06藍牙模組補充說明(二):連接電路
[3] HC-05與HC-06藍牙模組補充說明(三):使用Arduino設定AT命令
[4] 藍牙模組補充說明(四):無線連結兩個Arduino控制板
[5] Connecting 2 Arduinos by Bluetooth using a HC-05 and a HC-06: Pair, Bind, and Link
與 HC-05與HC-06藍牙模組補充說明(三):使用Arduino設定AT命令 兩篇文章。
Please refer to "How to Set AT Command Mode for HC-05 Bluetooth Module" and "AT Command Mode of HC-05 and HC-06 Bluetooth Module" fro AT commands for HC-05/HC-06.
三、藍牙模組的設定(Bluetooth Module Configuration)HC-06的設定值 (For HC-06)
所需的指令如下(The AT Commands List):
- AT
- AT+PIN1234
- AT+NAMEyour_name
- AT+BAUD4
紅色 1 OK 是 AT 檢查裝置指令回傳結果。(RED 1 OK is the return value of the AT command.)
藍色 2 OKsetPIN 是 AT+PIN1234 設定密碼指令回傳結果。(Blue 2 OKsetPIN is the return value of the AT+PIN1234 command.)
綠色 3 OKsetname 是 AT+NAMEPingLun 設定藍芽名稱指令回傳結果。(Green 3 OKsetname is the return value of the AT+NAMEPingLun command.)
紫色 4 OK9600 是 AT+BAUD4 設定傳輸速度指令回傳結果。(Purple 4 OK9600 is the return value of the AT+BAUD4 command.)
接著可用 Bluetooth Scanner 來找出 HC-06 模組的位址(Address)。
Use Bluetooth Scanner to figure out the MAC address of HC-06.
注:上圖中的位址需轉為 98D3,31,FB5396 的格式,才能給 AT 指令用,此部分可的說明請參考 藍牙模組補充說明(四):無線連結兩個Arduino控制板一文中的藍芽裝置位址一節。
PS: the MAC address must be the 98D3,31,FB5396 for AT command.
HC-05的設定值(For HC-05)
要用AT指令讓HC-05主控端連上HC-06從端時,需將HC-05切換到指令模式,這需要在上電前壓住按鈕(如下圖用夾子夾住),注意 HC-05在 指令模式下的速度為 38400 。
Hold the reset button on the HC-05 before power on. This will enable HC-05 in command mode. A plastic peg could hold the reset button for you.
- AT+RMAAD 清除已配對的藍牙模組 (Clear the paired BT devices)
- AT+ROLE=1 設定為 Master (Set the role to Master)
- AT+BIND=98D3,31,FB5396 (Binding with the MAC address: 98D3,31,FB5396)
有回應OK才算成功。下圖的 ERROR 就是沒成功的例子,此時再下指令直到出現 OK。
Execute every command until it succeeds, got a OK response. The ERROR:(0) response say: "the command need to execute again".
Execute every command until it succeeds, got a OK response. The ERROR:(0) response say: "the command need to execute again".
藍牙模組互聯連線成功影片(注意燈號閃爍的速度):
The successful connection video(notice the onboard LED blinking speed)
尚未連線前:HC-06 與 HC-05 的指示燈會快速閃爍。
Before connection: the HC-06 and HC-05 onboard LED blink fast.
連線成功後:HC-06 的指示燈會恆亮;HC-05 的指示燈每兩秒快速閃爍兩次。
After connection: the HC-06 onboard will turn on, not blinking; the HC-05 will blink twice every two seconds.
If you have any problem to pair HC-05 with HC-06, please refer to this tutorial: "How to Configure HC-05 Bluetooth Module As Master and Slave Via AT Command".
四、HC-06 從端藍牙小夜燈程式 (The Code for HC-06 and Nano)
此部分的程式和 Arduino Nano 藍牙小夜燈相同。The code for Arduino Nano with HC-06 is the same with Control a Lamp from Android Phone.
五、HC-05 主控端藍牙搖桿程式(The Code for HC-05 and UNO)
搖桿的控制方式如下圖,箭頭的方向代表增加該顏色的亮度,例如紅線為X軸方向,向右會增加紅色燈的亮度,向左會減少紅色燈的亮度。黑色線為控制三種顏色的亮度。
This diagram shows the mechanism of LED lightness. For example, the red line means the RED lightness will increase from left to right(X Coordinate); the blue line mean the BLUE lightness will increase from top to bottom(Y Coordinate).
將HC-06藍牙小夜燈 與 HC-05藍牙搖桿兩程式分別燒錄好後,就可以用搖桿來控制小夜燈了。
After uploading the Nano(HC-06) and UNO(HC-05), the bluetooth lamp could be controlled remotely by a bluetooth joystick.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int redColor = 0, greenColor = 0, blueColor = 0; // 三色LED燈顏色數值 | |
int rLedPin = 9, gLedPin = 10, bLedPin = 11; // 三色燈在Nano上的數位腳位 | |
void setup() { | |
// put your setup code here, to run once: | |
Serial.begin(9600); | |
pinMode(rLedPin, OUTPUT); | |
pinMode(gLedPin, OUTPUT); | |
pinMode(bLedPin, OUTPUT); | |
// 此例所用的三色LED為共陽極 | |
analogWrite(rLedPin, 255-redColor); | |
analogWrite(gLedPin, 255-greenColor); | |
analogWrite(bLedPin, 255-blueColor); | |
} | |
void loop() { | |
// 當資料超過 3 Bytes 時,將前 3 Bytes分別當成 Red, Green, Blue 顏色的數值(0~255) | |
if (Serial.available() >= 3) { | |
redColor = Serial.read(); | |
greenColor = Serial.read(); | |
blueColor = Serial.read(); | |
analogWrite(rLedPin, 255-redColor); | |
analogWrite(gLedPin, 255-greenColor); | |
analogWrite(bLedPin, 255-blueColor); | |
} | |
} |
搖桿的控制方式如下圖,箭頭的方向代表增加該顏色的亮度,例如紅線為X軸方向,向右會增加紅色燈的亮度,向左會減少紅色燈的亮度。黑色線為控制三種顏色的亮度。
This diagram shows the mechanism of LED lightness. For example, the red line means the RED lightness will increase from left to right(X Coordinate); the blue line mean the BLUE lightness will increase from top to bottom(Y Coordinate).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// set pin numbers joystick axes | |
const int xAxis = A0; // joystick X axis | |
const int yAxis = A1; // joystick Y axis | |
int responseDelay = 50; // response delay of the mouse, in ms | |
const int centerValue = 512; | |
const int range = 200; | |
const int upperBond = centerValue + range; | |
const int lowerBond = centerValue - range; | |
// RGB LED brightness | |
byte red = 0; | |
byte green = 0; | |
byte blue = 0; | |
void setup() { | |
Serial.begin(9600); | |
} | |
void loop() { | |
// read the two axes: | |
int xReading = readAxis(A0); | |
int yReading = readAxis(A1); | |
// Trigger when the joystick aren't in the center | |
if( xReading != 0 || yReading != 0 ) { | |
// Red color | |
if(xReading == 1 && yReading == 0 && red < 255) red += 5; | |
if(xReading == -1 && yReading == 0 && red > 0) red -= 5; | |
// Green color | |
if(xReading == -1 && yReading == 1 && green < 255) green += 5; | |
if(xReading == 1 && yReading == -1 && green > 0) green -= 5; | |
// Blue color | |
if(xReading == 0 && yReading == 1 && blue < 255) blue += 5; | |
if(xReading == 0 && yReading == -1 && blue > 0) blue -= 5; | |
// all color | |
if(xReading == 1 && yReading == 1 ) { | |
if( red < 255 ) red += 5; | |
if( green < 255 ) green += 5; | |
if( blue < 255) blue += 5; | |
} | |
if(xReading == -1 && yReading == -1) { | |
if( red > 0 ) red -= 5; | |
if( green > 0 ) green -= 5; | |
if( blue > 0 ) blue -= 5; | |
} | |
byte value[3] = {(byte)red, (byte)green, (byte)blue}; | |
Serial.write(value, 3); | |
Serial.flush(); | |
} | |
delay(responseDelay); | |
} | |
/* | |
* Determin increment or decrement | |
* 1: increment | |
* 0: nothing happen | |
* -1: decrement | |
*/ | |
int readAxis(int thisAxis) { | |
// read the analog input: | |
int reading = analogRead(thisAxis); | |
if( reading > upperBond ) | |
return 1; | |
else if( reading < lowerBond ) | |
return -1; | |
else | |
return 0; | |
} |
After uploading the Nano(HC-06) and UNO(HC-05), the bluetooth lamp could be controlled remotely by a bluetooth joystick.
參考資料:
[1] HC-05與HC-06藍牙模組補充說明(一)
[2] HC-05與HC-06藍牙模組補充說明(二):連接電路
[3] HC-05與HC-06藍牙模組補充說明(三):使用Arduino設定AT命令
[4] 藍牙模組補充說明(四):無線連結兩個Arduino控制板
[5] Connecting 2 Arduinos by Bluetooth using a HC-05 and a HC-06: Pair, Bind, and Link
沒有留言:
張貼留言