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.