Modify code in order to control multiple servos

Hello!
I’m a newb at programming

I have this code:

#include <Servo.h>

Servo myservo; // create servo object to control a servo
                // a maximum of eight servo objects can be created

int pos = 0;    // variable to store the servo position
int motor = 0;

void setup()
{  
  Serial.begin(9600);  // initialize serial: 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object

 
  Serial.print("Arduino control Servo Motor Connected OK");
  Serial.print('\n');
}

void loop()
{ 
  // if there's any serial available, read it:
  while (Serial.available() > 0) {
    
    // look for the next valid integer in the incoming serial stream:
    motor = Serial.parseInt();
   
    // do it again:
    pos = Serial.parseInt();
  
    // look for the newline. That's the end of your  sentence:
    if (Serial.read() == '\n') {
              
       myservo.write(pos);              // tell servo to go to position in variable 'pos'
       delay(15);                       // waits 15ms for the servo to reach the position
     
      // print the three numbers in one string as hexadecimal:
      Serial.print("Data Response : ");
      Serial.print(motor, DEC);
      Serial.print(pos, DEC);
      
    }
  }
}
 
  //for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees
  //{                                  // in steps of 1 degree
  //  myservo.write(pos);              // tell servo to go to position in variable 'pos'
  //  delay(15);                       // waits 15ms for the servo to reach the position
  //}
  //for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees
  //{                                
  //  myservo.write(pos);              // tell servo to go to position in variable 'pos'
  //  delay(15);                       // waits 15ms for the servo to reach the position
  //}
 
 
  //val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  //val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
  //myservo.write(val);                  // sets the servo position according to the scaled value
  //delay(15);

It controls ONE servo motor with bluetooth
I need to modify it in order that it controls two servos
Could you please help me

joerack: I need to modify it in order that it controls two servos Could you please help me

Try to do it for yourself. If your modified program does not work post it here and we will try to help.

You should probably look at how the servo is controlled in Several Things at a Time. That code does not use a FOR loop. A FOR loop blocks until it is finished which would mean each servo would have to move separately.

...R

I’ve tried to modify in order to get it to control a led and a servo, but I keep getting a mad continuous response.
Please help

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
                // a maximum of eight servo objects can be created

int pos = 0;    // variable to store the servo position
int motor = 0;

void setup()
{  
  Serial.begin(9600);  // initialize serial: 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
    Serial.print("Arduino control Servo Motor Connected OK");
  Serial.print('\n');
  
  pinMode(13, OUTPUT);
 

}

void loop()
{ 
  // if there's any serial available, read it:
  while (Serial.available() > 0) {
    
    // look for the next valid integer in the incoming serial stream:
    motor = Serial.parseInt();
   
    // do it again:
    pos = Serial.parseInt();
  
    // look for the newline. That's the end of your  sentence:
    if (Serial.read() == '\n') {
              
       myservo.write(pos);              // tell servo to go to position in variable 'pos'
       delay(15);
      }                       // waits 15ms for the servo to reach the position
      else if(Serial.read() == 'a'){   //incase of 'a' turn the LED on
      digitalWrite(13, HIGH);}
      else if(Serial.read() == 'b'){   //incase of 'b' turn the LED off
      digitalWrite(13, LOW);}
  }
     
      // print the three numbers in one string as hexadecimal:
      Serial.print("Data Response : ");
      Serial.print(motor, DEC);
      Serial.print(pos, DEC);
      
}
  
 
  //for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees
  //{                                  // in steps of 1 degree
  //  myservo.write(pos);              // tell servo to go to position in variable 'pos'
  //  delay(15);                       // waits 15ms for the servo to reach the position
  //}
  //for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees
  //{                                
  //  myservo.write(pos);              // tell servo to go to position in variable 'pos'
  //  delay(15);                       // waits 15ms for the servo to reach the position
  //}
 
 
  //val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  //val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
  //myservo.write(val);                  // sets the servo position according to the scaled value
  //delay(15);
    if (Serial.read() == '\n') 
    {
      myservo.write(pos);              // tell servo to go to position in variable 'pos'
      delay(15);
    }                       // waits 15ms for the servo to reach the position
    else if (Serial.read() == 'a') 
    { //incase of 'a' turn the LED on
      digitalWrite(13, HIGH);
    }
    else if (Serial.read() == 'b') 
    { //incase of 'b' turn the LED off
      digitalWrite(13, LOW);
    }

I make that 3 Serial.read()s without being sure that there is anything to read. In addition, Serial.read() removes a character from the buffer so what will happen if the buffer contains say 'b' ? It will have been removed by the check for 'a'

If data is available after the Serial.parseInt() then read it into a variable and test that variable rather than doing repeated Serial.read()s

You might also like to look at Serial Input Basics

...R