0
我有两个蓝牙模块(HC05)连接到单独的arduinos。一个充当主人,另一个充当奴隶。一个LDR连接到从属部件,它将连续读取并通过蓝牙将其发送给主部件。通过蓝牙传感器读取通信
模块成功配对。我甚至可以使用连接到从属设备的按钮来控制连接到主设备的LED。
由于4天我努力获得主控串口监视器上的LDR读数。
(具有LDR)该项目的从属部分:
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // RX | TX
#define ldrPin A0
int ldrValue = 0;
void setup() {
pinMode(9, OUTPUT); // this pin will pull the HC-05 pin 34 (key pin) HIGH to switch module to AT mode
digitalWrite(9, HIGH);
pinMode(ldrPin, INPUT);
BTSerial.begin(9600);
Serial.begin(9600);
}
void loop()
{
ldrValue = analogRead(ldrPin);
BTSerial.println(ldrValue);
Serial.println(ldrValue);
delay(1000);
}
将要获得reaings和显示串行监视器上的项目的主部分:
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // RX | TX
const byte numChars = 1024;
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;
void setup() {
pinMode(9, OUTPUT); // this pin will pull the HC-05 pin 34 (key pin) HIGH to switch module to AT mode
digitalWrite(9, HIGH);
BTSerial.begin(9600);
Serial.begin(9600);
Serial.println("<Arduino is ready>");
}
void loop() {
recvWithEndMarker();
showNewData();
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
while (BTSerial.available() > 0 && newData == false) {
rc = BTSerial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void showNewData() {
if (newData == true) {
Serial.print("This just in ... ");
Serial.println(receivedChars);
newData = false;
}
}
但问题是在串行监视器中只有最高位(392中的3)显示在串行监视器中。读数是正确的,但不显示完整的读数。 串行监测显示反应是这样的:
<Arduino is ready>
This just in ... 1
This just in ... 1
This just in ... 1
This just in ... 1
This just in ... 1
This just in ... 3
This just in ... 3
This just in ... 3
This just in ... 3
This just in ... 3
IFIN奴隶部分,而不是LDR读数,如果我发送一个字符串“hello”,则打印为:
<Arduino is ready>
This just in ... h
This just in ... h
This just in ... h
This just in ... h
This just in ... h
This just in ... h
我已经提到这个串口通讯链接0
有人可以帮助我,因为我是新的arduino。
相反BTSerial.read的()尝试BTSerial .readString()就像在[docs](https://www.arduino.cc/en/Serial/ReadString)中一样,并且它会容易很多@ vishruth-kumar –
谢谢!得到它的工作! –
太棒了!我会将其作为官方答复发布 –