Hello. I'm working on an Arduino code and accompanying Pd patch, sending values from Pd to Arduino to control the PWM value sent to certain digital pins.

As of now, the code parses incoming strings in the format int,int and ends with a carriage return. This works just fine when sending the string from the Arduino software's own serial monitor, but I cannot seem to get it work when sending messages to the [comport] object. Right now I'm just using a single banged message box (ex. [3,80]) sent to [comport], so I suspect that I need to end the message with a carriage return in some format.

How would I go about doing this? I know \ is not allowed in Pd messages. What format does the Arduino understand in this situation? Ascii?

In the code below, I suppose I could change the string terminating character to something other than a carriage return, but I would be unsure of how to execute it properly.

I'm quite new to the Arduino platform, so if anything looks and sounds completely off, please do tell me.


void setup()
{
  Serial.begin(9600);
  
}

String command;

void loop()
{
 if (Serial.available() > 0)
 {
  char c = Serial.read();
 
  if(c == '\n')
  {
    parseCommand(command);
    command = "";
  }
  else
  {
    command += c;
  }
 }
}

void parseCommand(String com)
{
  String part1;
  String part2;

  //int SPACE int
  part1 = com.substring(0, com.indexOf(","));
  part2 = com.substring(com.indexOf(",") + 1);

  int pin = part1.toInt();
  int pwmval = part2.toInt();

  analogWrite(pin, pwmval);

}

Thank you.