所以我通过 ble 将数据从我的 flutter 应用程序发送到我的 bluno beelte v1.1(固件更新 v1.97),我看到 rx LED 在我发送数据的瞬间亮起,但它被发布在串行端口中仅在所有数据发送完毕后。它将最后几秒的数据分组在一起并一次性打印出来。但是我需要它在收到它后立即将其发送到串行端口。
我怎样才能做到这一点?
这是arduino上的代码:
#include <Adafruit_NeoPixel.h>
#include <Servo.h>
int estPin = 5;
int servoPin = 2;
int pixelPin = 3;
int pixelNum = 1;
int data;
Servo servo;
Servo esc;
String command;
String r;
String g;
String b;
int color[3] = {150, 0, 0};
Adafruit_NeoPixel pixels(pixelNum, pixelPin, NEO_GRB + NEO_KHZ800);
void setup() {
pixels.begin();
Serial.begin(115200);
servo.attach(servoPin);
esc.attach(estPin);
delay(7000);
}
void loop() {
if(Serial.available()) {
data = Serial.read();
command = Serial.readStringUntil('\n');
Serial.print("command: ");
Serial.println(command);
String code = command.substring(0,3);
if(code == "rgb")
{
String rgb = command.substring(3,command.length());
String c[3];
int index = 0;
for (int i=0;i<3;i++){
index = rgb.indexOf(' ');
c[i] = rgb.substring(0, index);
rgb = rgb.substring(index+1);
color[i] = c[i].toInt();
}
pixels.clear();
for(int i=0; i<pixelNum; i++) {
pixels.setPixelColor(i, pixels.Color(color[0], color[1], color[2]));
pixels.show();
}
}
else if(code == "srv")
{
String value = command.substring(3,6);
servo.write(value.toInt());
Serial.println(value);
}
else if(code == "mtr")
{
String Svalue = command.substring(3,6);
int value = Svalue.toInt();
value = map(value, 0, 100, 1100, 1900);
esc.writeMicroseconds(value);
Serial.println(value);
}
}
}
我发现如果你使用软件串口在bluno beetle的rx tx引脚上打开一个串口,然后附加一个中断,你可以在甲虫收到数据后立即获取数据。
这是我的代码片段:
SoftwareSerial bleSerial(0,1);
void setup()
{
bleSerial.begin(115200);
attachInterrupt(0, handleBLEInterrupt, FALLING);
}
void handleBLEInterrupt()
{
char data = bleSerial.read();
//do your thing here
}