Convert serial.read() into a useable string using Arduino?

后端 未结 15 1125
盖世英雄少女心
盖世英雄少女心 2020-11-29 15:27

I\'m using two Arduinos to sent plain text strings to each other using newsoftserial and an RF transceiver.

Each string is perhaps 20-30 characters in length. How do

15条回答
  •  鱼传尺愫
    2020-11-29 16:11

    This always works for me :)

    String _SerialRead = "";
    
    void setup() {
      Serial.begin(9600);
    }
    
    void loop() {
      while (Serial.available() > 0)        //Only run when there is data available
      {
        _SerialRead += char(Serial.read()); //Here every received char will be
                                            //added to _SerialRead
        if (_SerialRead.indexOf("S") > 0)   //Checks for the letter S
        {
          _SerialRead = "";                 //Do something then clear the string
        }
      }
    }
    

提交回复
热议问题