|
我在Arduino uno上连接了一个蓝牙模块和传感器,目的想把传感器的数据通过蓝牙模块发送给连接的手机端。
在最初调试代码时候,一直使用USB供电。直到代码成功后,因为具体要求,利用电池进行供电,但是蓝牙模块不传输数据了。之前有看别的帖子,说是电池问题,我起初也怀疑这个,但用了实验室的恒稳电源发生器设置成12V,蓝牙模块还是工作不了(可以连接设备但是不能发送数据)。
后来把传感器给拆掉只留下蓝牙模块,还是老样子。我现在认为是这个SoftwareSerial库的问题,涉及到了串口。但是本人没有办法能够让通信不涉及到串口,希望大家可以给出建议。
使用的Arduino板子是RedBoard,蓝牙模块是BlueSMiRF Gold。
测试代码如下:
#include <SoftwareSerial.h>
int bluetoothTx = 2; // TX-O pin of bluetooth mate, Arduino D2
int bluetoothRx = 3; // RX-I pin of bluetooth mate, Arduino D3
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
int bpm = 50;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // we agree to talk fast!
bluetooth.begin(115200); // The Bluetooth Mate defaults to 115200bps
bluetooth.print("$"); // Print three times individually
bluetooth.print("$");
bluetooth.print("$"); // Enter command mode
delay(100); // Short delay, wait for the Mate to send back CMD
bluetooth.println("U,9600,N"); // Temporarily Change the baudrate to 9600, no parity
// 115200 can be too fast at times for NewSoftSerial to relay the data reliably
bluetooth.begin(9600); // Start bluetooth serial at 9600
}
void loop() {
// put your main code here, to run repeatedly:
if (bpm >= 100)
{
bpm = 50;
}
bluetooth.println((float)bpm);
bpm = bpm+1;
delay(2000);
} |
|