測試透過藍芽通訊協定,傳達指令給 Arduino開關LED,但在這之前我們必須清楚了解我們手邊的藍芽型號是哪一種以及baud rate預設是多少。這的例藍芽型號採HC-06,baud rate改為57600 bps。
- 確認型號及腳位
- 跟改藍芽baud rate改,下列為更改為57600 bps,詳細可至下列參考資料查詢。
// A simple sketch to set baud rate of HC-0x bluetooth module // If successful configured, a response like "OK75600" will be shown on serial monitor // Command: AT+BAUDx, where x can be: // 1: 1200, 2:2400, 3:4800, 4:9600, 5:19200, // 6:38400, 7:57600, 8:115200, 9:230400 void setup() { Serial.begin(9600); // Baud rate of HC-0x bluetooth module delay(6000); Serial.write("AT+BAUD7"); // Set baud rate as 57600 } void loop() { if (Serial.available()) Serial.write(Serial.read()); }
- 上傳code至Arduino:上傳前記得要將藍芽電源拔除。
#includeSoftwareSerial BTSerial(10, 11); // RX | TX void setup() { Serial.begin(57600); pinMode(13, OUTPUT); pinMode(8, OUTPUT); BTSerial.begin(57600); // HC-06 current bound rate (default 57600) } void loop() { // 讀出第 1 個字元 unsigned char charreceived = BTSerial.read(); switch(charreceived){ case '1': //開啟LED digitalWrite(13, HIGH); digitalWrite(8, HIGH); Serial.println(" Arduino Led On"); break; case '0': //關閉LED digitalWrite(13, LOW); digitalWrite(8, LOW); Serial.println(" Arduino Led Off"); break; default: break; } Serial.flush(); delay(10); }
- 將所有線路接至適當地腳位
腳位完成圖 |
- 下載安裝 Blueterm
- 完成影片
PS:把 LED 接到 pin8,長腳(陽極)接到 pin8,短腳(陰極)接到 GND 參考資料
- http://www.funbroad.tw/p/maker-single-arduino-android-double_5129.html
- http://coopermaa2nd.blogspot.tw/2012/07/config-bluetooth-baud-rate-w-arduino.html
- http://coopermaa2nd.blogspot.tw/2012/09/blueterm-and-arduino.html
沒有留言:
張貼留言