第一次尝试后串行通信卡住

时间:2021-05-20 14:36:42

标签: arduino

我正在使用 arduino Mega 2560 通过串行通信通过 touchdesigner 控制一些振动电机。我映射了像素来控制每个电机,它工作了几秒钟,很快就卡住了。我的代码有问题吗?

这是我的 arduino 草图:

#define MOTOR_COUNT 12
int motors[MOTOR_COUNT];

void setup()
{

  Serial.begin(115200);
  Serial.println("Ready to receive frames.");

  for (int i = 0; i <= 12; i++) {
    pinMode(i, OUTPUT);
  }


void loop()
{
  
  if (Serial.available())
  {
    char c = Serial.peek();
    if (!(c >= '0' && c <= '9'))
    {
      Serial.read(); // Discard non-digit character
    }
    else if (Serial.read() == '\n')
    {
      for (uint16_t i = 0; i < MOTOR_COUNT; i++)
      {
        motors[i] = Serial.parseInt();
        Serial.print("motor ");
        Serial.print(i);
        Serial.print(":");
        Serial.println(motors[i]);

        if (i <= 12) {
          analogWrite(i + 2, motors[i]);
        }
      }
    }
  }
}

0 个答案:

没有答案