serial problem

i need to control 3 outputs with pwm from serialMonitor .
i wanna control them with x - y - z
for example : when i send x30 the x show that the output number one and 30 show the pwm
when i send y28 , the y show that the output number 2. and the digits shows the pwm
..

, i tried a lot and still can't get the answer .
:frowning:

What have you tried and how did it fail? Post the full sketch and explain what you expect it to do.

for example : when i send x30 the x show that the output number one and 30 show the pwm

It would be much better to send "<x,30>". The start (<) and end (>) markers allow you to know when to start and stop saving data. Once both have arrived, the stored data would be "x,30", which is easily parsed, using strtok() and converted to values using atoi().

https://forum.arduino.cc/index.php?topic=288234.0

Below is some servo control test code that has a format that may work in your situation. The control data sent from the serial monitor has a numeric control value, a servo identifier, and an end of data marker like this "1500c,".

//zoomkat 11-22-12 simple delimited ',' string parse 
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added 
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h> 
Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo 

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

  //myservoa.writeMicroseconds(1500); //set initial servo position if desired

  myservoa.attach(6);  //the pin for the servoa control
  myservob.attach(7);  //the pin for the servob control
  myservoc.attach(8);  //the pin for the servoc control
  myservod.attach(9);  //the pin for the servod control 
  Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}

void loop() {

  //expect single strings like 700a, or 1500c, or 2000d,
  //or like 30c, or 90a, or 180d,
  //or combined like 30c,180b,70a,120d,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out

        int n = readString.toInt();  //convert readString into a number

        // auto select appropriate value, copied from someone elses code.
        if(n >= 500)
        {
          Serial.print("writing Microseconds: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
          if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
          if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
          if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
        }
        else
        {   
          Serial.print("writing Angle: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.write(n);
          if(readString.indexOf('b') >0) myservob.write(n);
          if(readString.indexOf('c') >0) myservoc.write(n);
          if(readString.indexOf('d') >0) myservod.write(n);
        }
         readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

PaulS:
It would be much better to send "<x,30>". The start (<) and end (>) markers allow you to know when to start and stop saving data. Once both have arrived, the stored data would be "x,30", which is easily parsed, using strtok() and converted to values using atoi().

Serial Input Basics - Programming Questions - Arduino Forum

thank you PaulS , i got my answers the problem solved. i had problem with parse things and you put me in the Right way . thank to you thank to Robin2 thank you all of you guys up ther :wink: