Step 3: New code
// Controlling a servo position using a potentiometer (variable resistor)
// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
#include <Servo.h>
Servo myservo; // 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
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
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)
myservo.write(val); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
}
Remove these ads by
Signing Up










































Visit Our Store »
Go Pro Today »




Servo myservo1;
Servo myservo2;
Servo myservo3;
you would also need more potentiometers, so youd need to add more potpins, and have the potentiometers hooked up to their respective pins, I think that would look like this:
int potpin1 = 0;
int potpin2 = 1;
int potpin3 = 2;
where potpin1 is what we are calling analog pin 0, but will control myservo1. You also need more variables to store the data from the potpins, so make those too.
int val1;
int val2;
int val3;
the numbers will correspond with the servos of the same number.
now you just need to add the commands for checking all 3 servos into the loop, which would look like this:
void loop()
{
val1 = analogRead(potpin1); // reads the value of potentiometer 1
val1 = map(val1, 0, 1023, 0, 179); // scale it
myservo1.write(val1); // sets servo 1's position according to the scaled value
val2 = analogRead(potpin2); // reads the value of potentiometer 2
val2 = map(val2, 0, 1023, 0, 179); // scale it
myservo2.write(val2); // sets servo 2's position according to the scaled value
val3 = analogRead(potpin3); // reads the value of potentiometer 3
val3 = map(val3, 0, 1023, 0, 179); // scale it
myservo3.write(val3); // sets servo 3's position according to the scaled value
delay(15); // delays for 15ms to let the servos catch up
}
I think this would work, but someone check over my work because I'm a complete noob at arduino coding. Someone with an arduino and 3 servos should write this and see if it works :p
void setup() {
myservo1.attach(9); //attach servo 1 to pin 9
myservo2.attach(10); //attach servo 2 to pin 10
myservo3.attach(11); //attach servo 3 to pin 11
}
etc.