How to read a string value with a delimiter on Arduino?

后端 未结 7 941
小鲜肉
小鲜肉 2020-12-10 03:34

I have to manage servos from a computer.

So I have to send manage messages from computer to Arduino. I need manage the number of servo and the corner. I\'m thinking

7条回答
  •  半阙折子戏
    2020-12-10 04:17

    This is a Great sub I found. This was super helpful and I hope it will be to you as well.

    This is the method that calls the sub.

    String xval = getValue(myString, ':', 0);
    

    This is The sub!

    String getValue(String data, char separator, int index)
    {
      int found = 0;
      int strIndex[] = {
        0, -1  };
      int maxIndex = data.length()-1;
      for(int i=0; i<=maxIndex && found<=index; i++){
        if(data.charAt(i)==separator || i==maxIndex){
          found++;
          strIndex[0] = strIndex[1]+1;
          strIndex[1] = (i == maxIndex) ? i+1 : i;
        }
      }
      return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
    }
    

提交回复
热议问题