// Controlling a servo position using a potentiometer (variable resistor) // by Michal Rinott // Added a second servo moving in the other direction // by Markus Jabs #include Servo myservo_1; // create servo object 1 to control servo 1 Servo myservo_2; // create servo object 2 to control servo 2 int potpin = 0; // analog pin used to connect the potentiometer int val; // variable to read the value from the analog pin int val_1; // variable to to send to the servo_pin 1 int val_2; // variable to to send to the servo_pin 2 void setup() { myservo_1.attach(9); // attaches the servo 1 on pin 9 to the servo object myservo_2.attach(10); // attaches the servo 2 on pin 10 to the servo object } void loop() { val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023) val_1 = map(val, 0, 1023, 0, 179); // scale it to use it with the servo 1 (value between 0 and 180) val_2 = map(val, 0, 1023, 179, 0); // scale it backwards to use it with the servo 2 (value between 180 and 0) myservo_1.write(val_1); // sets the servo position according to the scaled value myservo_2.write(val_2); // sets the servo position according to the scaled value delay(15); // waits for the servo to get there }