Arduino:我正在尝试将通过蓝牙传入的字符串转换为字符数组

Arduino : I am trying to convert the string coming via Bluetooth into an char array

大家好:我正在尝试将通过蓝牙传来的字符串转换为字符数组 我想要的是从蓝牙获取字符串(这将是一个词)并将其转换为 char 数组: 例如:

Arduino 从蓝牙接收:water

我希望将其转换为:char arrayThing[30] = {'w','a','t','e','r',0};

以下是我正在尝试做的代码,请帮帮我

char arrayThing[30]; arrayThing[30]= Serial.read();

 for (int index = 0; index < 30; index++)
 {
    Serial.write(arrayThing[index]);
 }
   //  char arrayThing[30] = {'w','a','t','e','r',0};
    ouijaPrint(arrayThing);
    homeing();//Go back to home after each massage is printed

Serial.read() returns 最大。 1 个字符,您将其放入 char 数组的所需位置。串行传输很慢,所以你最好不要等待,而是传输一个消息结束字符,例如<换行符>又名。 '\n'

char arrayThing[30];
byte pos = 0; 
void loop() {
  if (Serial1.available() {
      arrayThing[pos] = Serial1.read();  
      if (arrayThing[pos] == '\n') {
         arrayThing[pos] = 0;  // text end 
         pos = 0; // prepare to receive the next text line
         Serial.println(arrayThing); // work with the result 
      } else {
          if (pos < 29) pos++; // prepare for next character
      }
   }
} 

Serial1 是您的蓝牙,Serial 是您的调试输出(用于此测试)