Adding another servo motor

Hello, I am relatively new to Arduino and I have made a servo motor that is controlled by a potentiometer, I wanted to add another to make a sort of belt, however, I wanted to know how could I add another servo to my code, I already tried copy and pasting my code but just changing the variable and defining another version of “potVal” and “potPin”. My second motor is connected to analog 1 “A1” and digital port 10 “~10”.

#include <Servo.h>

Servo myServo;
int const potPin = A0; //analog pin A 
int potVal; //stores value of pot 
int angle = 0; //stores position of servo
void setup() { 
  myServo.attach(9); //tells board which pin the servo is on
  Serial.begin(9600);  
} //end of setup()

void loop() { 
  potVal = analogRead(potPin); 
  Serial.print("potVal: "); 
  Serial.print(potVal);
 angle = map(potVal, 0, 1023, 0, 179); //angle is the pot value re-scaled to 0-179 
 Serial.print(", angle: "); 
 myServo.write(angle); 
 delay(15); 
} //end of loop()

The Code I tried that didn’t work

 #include <Servo.h>

Servo myServo;
int const potPin = A0; //analog pin A 
int const potPi = A1; //analog pin A 
int potVal; //stores value of pot
int potVa; //stores value of pot 
int angle = 0; //stores position of servo
void setup() { 
 myServo.attach(9); //tells board which pin the servo is on
 Serial.begin(9600);
  myServo.attach(10); //tells board which pin the servo is on
 Serial.begin(9600);    
} //end of setup()

void loop() { 
 potVal = analogRead(potPin); 
 Serial.print("potVal: "); 
 Serial.print(potVal);
angle = map(potVal, 0, 1023, 0, 179); //angle is the pot value re-scaled to 0-179 
Serial.print(", angle: "); 
myServo.write(angle); 
delay(15); 
 potVa = analogRead(potPi); 
 Serial.print("potVa: "); 
 Serial.print(potVa);
angle = map(potVa, 0, 1023, 0, 179); //angle is the pot value re-scaled to 0-179 
Serial.print(", angle: "); 
myServo.write(angle); 
delay(15); 
} //end of loop()

Please post the code that you tried

Alright It should be updated now and thanks for the reply

Good, we can now see your code. In the future, post new information in new posts so that the chronological order of the conversation is preserved.

Now, you described your goal and posted code. It is also helpful to express what you expected your code to do, what the actual results were, and how those two differ.

  myServo.attach(9); //tells board which pin the servo is on
  Serial.begin(9600);
  myServo.attach(10); //tells board which pin the servo is on

You need a different name for the second servo then use it in the code when referring to the second servo

Cool, well I expected it to do the same thing that the first set of code did and allow me to use a potentiometer and a servo motor with an Arduino and I have my comments on the code to help me remember what each part did so I thought to copy and pasting while changing the values and defining the ports that the second servo was connected to. Currently, I'm using two arduinos to create a conveyor with the same code but I wanted to allow one arduino to do that instead of two separate ones.

Dippysaurus: Cool, well I expected it to do the same thing that the first set of code did

Which of the earlier Replies are you responding to?

Have you taken note of the comments you have received and updated your program?

...R

Oh I was replying to vinceherman

So after some tinkering, I got it to work thanks so much, everyone :)