Arduino读取串口数据并进行字符串分割

String comdata = "";
int numdata[6] = {0}, PWMPin[6] = {3, 5, 6, 9, 10, 11}, mark = 0;
void setup()
{
  for(int i = 0; i < 6; i++) pinMode(PWMPin[i], OUTPUT);
  Serial.begin(9600);
}
void loop()
{
  int j = 0;
  while (Serial.available() > 0)
  {
    comdata += char(Serial.read());
    delay(2);
    mark = 1;
  }
  if(mark == 1)
  {
    Serial.println(comdata);
    Serial.println(comdata.length());
    for(int i = 0; i < comdata.length() ; i++)
    {
      if(comdata[i] == ',')
      {
        j++;
      }
      else
      {
        numdata[j] = numdata[j] * 10 + (comdata[i] - '0');
      }
    }
    comdata = String("");
    for(int i = 0; i < 6; i++)
    {
      Serial.print("Pin ");
      Serial.print(PWMPin[i]);
      Serial.print(" = ");
      Serial.println(numdata[i]);
      analogWrite(PWMPin[i], numdata[i]);
      numdata[i] = 0;
    }
    mark = 0;
  }
}

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页