Help with servos controlled by one potentiometer

Was wondering if anyone could help with this arduino project i’m doing, it involves 2 servos connected to an external power supply, and each having their own digital pin. They share one potentiometer. I have the two servos facing opitsite to each other, therefore i need to flip one’s direction but cant seem to work it out :drooling_face: You’ll probably see what i’m trying to get at if you have a look at my code

#include <Servo.h> 
 
Servo myservo1;  // create servo object to control a servo 
int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin 

Servo myservo2;
int potpin2 = 0;
int val2;

void setup() 
{ 
  myservo1.attach(9);  // attaches the servo on pin 9 to the servo object 
  myservo2.attach(10);
} 
 
void loop() 
{ 
  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) 
  val2 = analogRead(potpin2);            // reads the value of the potentiometer (value between 0 and 1023) 
  val2 = map(val, 0, 1023, 179, 1);     // scale it to use it with the servo (value between 0 and 180) 
 
  myservo1.write(val);                  // sets the servo position according to the scaled value 
  myservo2.write(val2);
  delay(15);                           // waits for the servo to get there 

}

Im fairly new to arduino as you can probably tell, but have read about servos and basic functions etc. Thanks in advance :wink:

Try this:

#include <Servo.h>

Servo myservo1;  // create servo object to control a servo
Servo myservo2;
const int potpin = A0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin

void setup() {
  myservo1.attach(9);  // attaches the servo on pin 9 to the servo object
  myservo2.attach(10);
}

void loop() {
  val = map(analogRead(potpin), 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)

  myservo1.write(val);                  // sets the servo position according to the scaled value
  myservo2.write(179-val);
  delay(15);                           // waits for the servo to get there
}

Thanks alot johnwasser, worked perfectly :smiley:

so trying to get analog from a communication pin.
yu have learned, and that is the object

shooter:
so trying to get analog from a communication pin.
yu have learned, and that is the object

Using “int potpin = 0;” wasn’t a problem. The analogRead() function will use A0 even if you call it 0. I recommend using the name A0 when talking about the A0 pin because that will always work. Some people use 14 instead of A0 but that only works on the Arduino UNO and similar models. On other models A0 is Pin 18 or Pin 54.
The only thing keeping the original code from working was using ‘val’ instead of ‘val2’ in the second map() call. Since ‘val’ had already been mapped into the range 0 to 179 when it was re-mapped it produced an incorrect result.