Reading multiple, specific values over serial connection

Hello. This is my first post on the forum, so please direct me to somewhere else if this happens to be the wrong place.

I'm trying to output multiple specific PWM values (to switch a mosfet) received over a serial connection to specific digital pins, but I lack the knowledge of how to - what I assume must be to - parse the multiple incoming values and send them to the specific pins. Ex. whatever val1 receives, send to pin3, whatever val2 receives, send to pin5, and so forth.

I'm sending the values from Pure Data over serial (using the [comport] object), and I can get it working just fine with just a single value sent to a single pin, as the code I'm using just takes the incoming serial data and outputs it to the pin.

Is there a way to establish multiple, with a lack of better words, inputs and outputs through serial? Or would I have to send some sort of specifically formatted string that would have to be parsed and separated on the Arduino's end?

The code for the simple one-value setup is this:

int val1 = 0; 

int motorPin3 = 3; 
 
void setup() 
{ 

  Serial.begin(9600);
  pinMode(motorPin3, OUTPUT);
} 
 
void loop() {

if (Serial.available() > 0) {
val1 = Serial.read();

analogWrite(motorPin3, val1); 

}
}

I can see how I can add the relevant code to interface it with another pin, but I can't figure out how to only send it a specific value.
Something along these lines I assume:

int val1 = 0; 
int val2 = 0; 

int motorPin3 = 3; 
int motorPin5 = 5; 
 
void setup() 
{ 
  
  Serial.begin(9600);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin5, OUTPUT);
} 
 
 
void loop() {

if (Serial.available() > 0) {
// read the incoming byte:
val1 = Serial.read();
val2 = Serial.read();

analogWrite(motorPin3, val1); 
analogWrite(motorPin5, val2);
}
}

I'm not well-versed in coding in general and I'm slowly learning the Arduino environment as I'm going along, my primary knowledge comes from Pure Data and alike, so I'm probably misusing and confusing some terminology, so please correct me if I'm wrong.
There is an obvious hole in my knowledge so far, but I can't seem to find out where to pick up and begin from.
Thank you in advance.

Is there a way to establish multiple, with a lack of better words, inputs and outputs through serial?

Yes. The key is having some way of knowing that the nth value goes to the nth pin.

In general it is better to always send two values - the pin number and the pin value - separated by commas, with start and end markers, like “<3, 203>”, which means to analogWrite() the value 203 to pin 3.

I see. Thank you.
Would I not be able to do something along the lines of
analogWrite(incomingSerialValue); , where the incoming serial value would be <$pin, $value>? I can’t quite do that as analogWrite demands pin number and output value separately. Can I declare that the incoming value contains both parameters on its message?

Would I not be able to do something along the lines of

No. The analogWrite() function does not take a string.

Can I declare that the incoming value contains both parameters on its message?

You can stand on a mountain top and shout that to your heart's content. It won't do any good, though. You MUST collect the data in a string, and parse the string.

Fortunately, that is almost trivial.

You can stand on a mountain top and shout that to your heart's content. It won't do any good, though

I'm sure it won't, but it sure sounds cathartic. It would undoubtedly be healthy from time to time.

It seems like it's a very common beginner's hurdle, so thank you for your help. I'll get into it and hopefully emerge with the solution. Thanks.

Alright, I've experimented and I've written a code that seems to work.
I can send the data in the right format through Arduino's own serial monitor, but it doesn't work when sending messages from Pure Data. I cannot use Pure Data's serial object (comport) at the same time, only one single instance can be active at the same time.
The messages I'm sending are probably not formatted right as it's missing the carriage return to end the string, but Pure data doesn't accept the use of \ in messages, so I'll have to read into how I do that.

I would appreciate if anyone would take a glance at the code and tell me if something looks off. Right now it seems to work.

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);

}

Rosaria:
I cannot use Pure Data’s serial object (comport) at the same time, only one single instance can be active at the same time.

I don’t understand what you are trying to say. Why would you need two instances of PureData? - whatever that is.

If you mean that you can’t use the Arduino Serial Monitor and PureData at the same time then that is the normal behaviour. Only one application can use a serial port at any one time.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

…R

If you mean that you can't use the Arduino Serial Monitor and PureData at the same time then that is the normal behaviour. Only one application can use a serial port at any one time.

Exactly. I'm still figuring out the limitations of all this serial communication.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

So I've read a couple of times today. Is there no reason to try and learn stringparsing and so forth with using String if I've got no previous coding experience in C++? (or anything other than visual programming languages - which is what Pure Data is).

Rosaria:
Is there no reason to try and learn stringparsing and so forth with using String if I've got no previous coding experience in C++?

For Arduino programming I recommend that you learn to use cstrings. I know they are not quite as simple to work with but the code will be more reliable.

...R

Much appreciated.