铺垫了好久,废话不多说,开始我的实验。这仍然是我目标的多灯控制实验的初步。因为我用的仍然是Google写好的成品软件,不是自己定制的安卓软件。
同时实现的功能仅仅是手机发送信息给蓝牙模块,然后通过Arduino转发给PC上的Arduino软件的串口监视器。
忽然感觉自己在硬件的这条路上,有两个方面真的要提高了:
(1)学习硬件的基础原理知识,能自己用贴片机、空的PCB板子、自己根据需求设计的电路图(我想集成什么模块就集成什么模块,这样我的飞控板就更小更轻了,起飞重量将大大的减小)。
(2)学习硬件的编程,当然这都是用C语言搞定的,但是好多的库文件,真的太好了,假期计划要弄一个相关的手册。
库的连接地址:http://arduino.cc/en/Reference/Libraries,可惜网上下载不到手册版的,就让我来第一个吃螃蟹。
当然飞控的核心仍然是PID算法的设计和滤波的设计。
蓝牙的参数包括蓝牙的名称、数据传输的波特率、配对密码等的信息。
先贴代码在解释(代码是网上找的):
#include <SoftwareSerial.h> //Software Serial Port
#define RxD 9
#define TxD 11
#define LED 13 SoftwareSerial blueToothSerial(RxD,TxD); void setup() { Serial.begin(9600); pinMode(RxD, INPUT); pinMode(TxD, OUTPUT); setupBlueToothConnection(); } void loop() { digitalWrite(LED, HIGH); delay(500); digitalWrite(LED, LOW); delay(500); } void setupBlueToothConnection() { Serial.println("Setting Bluetooth parameters"); blueToothSerial.begin(38400); //Set BluetoothBee BaudRate to default baud rate 38400
delay(100); sendBlueToothCommand("AT\r\n"); sendBlueToothCommand("AT+NAME=HC-05\r\n");//命名模块名
sendBlueToothCommand("AT+ROLE=0\r\n");//设置主从模式:0从机,1主机
sendBlueToothCommand("AT+PSWD=1234\r\n");//设置配对密码,如0123
sendBlueToothCommand("AT+UART=38400,0,0\r\n");//设置波特率9600,停止位1,校验位无
sendBlueToothCommand("AT+RMAAD\r\n");//清空配对列表
delay(100); Serial.println("Setup complete"); } void sendBlueToothCommand(char command[]) { char a; blueToothSerial.print(command); Serial.print(command); delay(100); while(blueToothSerial.available()) { Serial.print(char(blueToothSerial.read())); } }
(1)首先使用的库是SoftwareSerial来虚拟软件的串口用来通信。这个类有自己的不足:
(a)如果用软件虚拟多个串口,那么这下软件的串口在某一时刻只有一个可以接收信息。
(b)在串口中有个Read这个动作是通过中断实现的,所以这个虚拟的串口的RXD针脚要支持中断(不是所有的数字或者模拟针脚都支持中断的),所以 在Mega或者Mega 2560 中可以用来做RXD的针脚是:10, 11, 12, 13, 14, 15, 50, 51, 52, 53, A8 (62), A9 (63), A10 (64), A11 (65), A12 (66), A13 (67), A14 (68), A15 (69)。
在Leonardo中可以用作RXD的针脚为:8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI)。
TXD不做限制,不涉及中断,但是不能用pin0和pin1。
太高兴了,查阅了好久,基础的铺垫弄了好久,终于到结束这个专题笔记的时候了。
这里在安卓上安装的用于和蓝牙通信的软件,我用了另一款,不是说Amarino不好,只是我设计的功能用这个更好,反正最后都要自己在安卓上写软件的,测试用什么都一样。这里推荐的软件的名字是:BlueTerm。就不给连接了,去问万能的Google吧。
通信的代码:
1 /*
2 Software serial multple serial test 3
4 Receives from the hardware serial, sends to software serial. 5 Receives from software serial, sends to hardware serial. 6
7 The circuit: 8 * RX is digital pin 10 (connect to TX of other device) 9 * TX is digital pin 11 (connect to RX of other device) 10
11 Note: 12 Not all pins on the Mega and Mega 2560 support change interrupts, 13 so only the following can be used for RX: 14 10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69 15
16 Not all pins on the Leonardo support change interrupts, 17 so only the following can be used for RX: 18 8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI). 19
20 created back in the mists of time 21 modified 25 May 2012 22 by Tom Igoe 23 based on Mikal Hart's example 24
25 This example code is in the public domain. 26
27 */
28 #include <SoftwareSerial.h>
29
30 SoftwareSerial mySerial(10, 11); // RX, TX
31
32 void setup() 33 { 34 // Open serial communications and wait for port to open:
35 Serial.begin(57600); 36 while (!Serial) { 37 ; // wait for serial port to connect. Needed for Leonardo only
38 } 39
40
41 Serial.println("Goodnight moon!"); 42
43 // set the data rate for the SoftwareSerial port
44 mySerial.begin(4800); 45 mySerial.println("Hello, world?"); 46 } 47
48 void loop() // run over and over
49 { 50 if (mySerial.available()) 51 Serial.write(mySerial.read()); 52 if (Serial.available()) 53 mySerial.write(Serial.read()); 54 }
烧录到Arduino中,把蓝牙模块的四个针脚接好,注意蓝牙的TXD接到pin10,RXD接到pin11,这是在程序里设置好的。
注意数据传输的波特率,如果波特率不匹配就不能同步的接收数据,就会出现乱码。
BlueTerm不支持中文的输入,无所谓了,反正飞控不用中文格式的数据。
用BlueTerm连接蓝牙模块:奇迹出现了,在安卓手机端输入的信息没有任何延迟的出现在了Arduino的串口监视;同样在Arduino的串口监视器中输入的信息直接显示在安卓的手机软件中。窃喜。。。。。。
由于这不是最终的目标程序,只是初步的测试,就不截图了。
终于手机和Arduino的蓝牙模块能完美的实现文本信息的数据传输了,下个阶段就是用安卓的seekBar控件(类似进度条)来控制LED的亮度。由于明天得去堂城,洗澡睡觉。园友们如果遇到什么问题的话,直接给我留言,我每天都会来的。