servo project. 3 servos controlled via potentiometers

I'm trying to recreate this project with three servos rather than one. I'm an arduino novice, so I don't understand how to code very well yet. I also don't completely understand how to set up the circuit so that it will be able to contain all three servos along with the potentiometers. I'm really sorry about how little I know, but I would really appreciate your help.

here's the project:

http://arduino.cc/en/Tutorial/Knob

I know this code only programs for one servo, but I'm not sure how to alter it to support three:

#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 
}

martse97:
I know this code only programs for one servo, but I'm not sure how to alter it to support three.

One word: Arrays.

#include <Servo.h> 

Servo myservo[3];  // create servo object to control a servo 

const int potpin[3] = {
  A0, A1, A2};  // analog pins used to connect the potentiometer
const int servoPins[3] = {
  3, 5, 8};

int val;    // variable to read the value from the analog pin 

void setup(){
  for (int i=0; i<3; i++)
    myservo[i].attach(servoPins[i]);
} 

void loop() {
  for (int i=0; i<3; i++) {
    val = analogRead(potpin[i]);            // 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[i].write(val);                  // sets the servo position according to the scaled value 
  }
  delay(15);                           // waits for the servo to get there 
}

[/quote]

I'm also hoping I could get some help on how I should wire the circuit to contain the three servos, three potentiometers, and the arduino

Below is a basic diagram of wiring a servo. Bottom is test code for two servos and two pots that includes a deadband setup to limit hunting. You should be able to add a third servo by looking how the first two are coded.

//zoomkat dual pot/servo test 12-29-12

#include <Servo.h> 
Servo myservo1;
Servo myservo2;

int potpin1 = 0;  //my pot pin
int potpin2 = 1;

int newval1, oldval1;
int newval2, oldval2;

void setup() 
{
  Serial.begin(9600);  
  myservo1.attach(2);  
  myservo2.attach(3);
  Serial.println("testing dual pot servo");  
}

void loop() 
{ 
  newval1 = analogRead(potpin1);           
  newval1 = map(newval1, 0, 1023, 0, 179); 
  if (newval1 < (oldval1-2) || newval1 > (oldval1+2)){  
    myservo1.write(newval1);
    Serial.print("1- ");
    Serial.println(newval1);
    oldval1=newval1;
  }

  newval2 = analogRead(potpin2);
  newval2 = map(newval2, 0, 1023, 0, 179);
  if (newval2 < (oldval2-2) || newval2 > (oldval2+2)){  
    myservo2.write(newval2);
    Serial.print("2- ");    
    Serial.println(newval2);
    oldval2=newval2;
  }
  delay(50);
}

The pic attached shows how three servos can be connected if you use breadboard. In the pic, the right-hand one doesn't have a yellow wire shown, but of course it needs one. This method uses zoomcat's suggestion of powering servos from their own supply, not from the Arduino which is what you're probably doing if you followed the pic in the tutorial.

Note if you use breadboard, most of the full-size models need the gap in the middle of the power rails to be bridged with wire, as shown.

Three pots is difficult to manipulate.... I don't know what your project is, but you may find it useful to use a small joystick like this one which is just two pots at right angles. That link is my local supplier- sorry I don't know the actual manufacturer but I'm sure you can hunt that or a similar one down.

Edit.... btw if you go for John's excellent approach using arrays, you might like to read this.