Control 4 servos with 4 pots

can someone point me in the right direction to help me? i made a little robot arm with 4 servos, one for rotation, one for boom angle, other like an elbow and the last one to control an accessory, like a claw or hook. i know how to hook them up to the arduino with a 100nf capacitor, but would i need a shield and what is the code?

Servo's need power (not from Arduino 5V pin), Gnd connected to Arduino Gnd, and a control signal. No shield needed except for convenience. Similarly, a pot needs 5V on on outer leg (can be from Arduino), Arduino Gnd on the other outer leg, and the middle/wiper goes to an analog input pin. No shield needed except for convenience.

See the IDE examples for the code to read a pot and to move a servo.

Servo pot test code.

//zoomkat multi pot/servo test 3-23-13
//includes dead band for testing and limit servo hunting
//view output using the serial monitor

#include <Servo.h> 
Servo myservo1;  //declare servos
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;

int potpin1 = 0;  //analog input pin A0
int potpin2 = 1;
int potpin3 = 2;
int potpin4 = 3;
int potpin5 = 4;

int newval1, oldval1;  //pot input values
int newval2, oldval2;
int newval3, oldval3;
int newval4, oldval4;
int newval5, oldval5;

void setup() 
{
  Serial.begin(9600);  
  myservo1.attach(2);  
  myservo2.attach(3);
  myservo3.attach(4);
  myservo4.attach(5);
  myservo5.attach(6);
  Serial.println("testing multi pot servo");  
}

void loop()
{ 
  newval1 = analogRead(potpin1);           
  newval1 = map(newval1, 0, 1023, 0, 179); 
  if (newval1 < (oldval1-2) || newval1 > (oldval1+2)){ //dead band 
    myservo1.write(newval1); //position the servo
    Serial.print(newval1); //print the new value for testing 
    Serial.print("a,");
    oldval1=newval1; //set the current old value
  }

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

  newval3 = analogRead(potpin3);           
  newval3 = map(newval3, 0, 1023, 0, 179); 
  if (newval1 < (oldval1-2) || newval3 > (oldval3+2)){  
    myservo1.write(newval3);
    Serial.print(newval3);
    Serial.print("c,");
    oldval3=newval3;
  }

  newval4 = analogRead(potpin4);           
  newval4 = map(newval4, 0, 1023, 0, 179); 
  if (newval1 < (oldval1-2) || newval4 > (oldval4+2)){  
    myservo1.write(newval4);
    Serial.print(newval4);
    Serial.print("d,");
    oldval4=newval4;
  }

  newval5 = analogRead(potpin5);           
  newval5 = map(newval5, 0, 1023, 0, 179); 
  if (newval1 < (oldval5-2) || newval5 > (oldval5+2)){  
    myservo1.write(newval5);
    Serial.print(newval5);
    Serial.print("e,");
    oldval5=newval5;
  } 
  delay(50);  //to slow loop for testing and 
                  //reading in serial monitor, adjust as needed
}

so i dont need a shield, good becuase i dont have a motor shield lol. what capacitor do you reccomend, i know a 100nf is good for little ones but what about a Futaba s3003? is 100nf too little?

I don't know what you are thinking of doing with the capacitor. I have never felt the need for one either with servos or potentiometers.

Make a pencil drawing showing all your connections and post a photo of it.

A long time ago I helped someone write code for controlling an excavator and I just now realized I had bookmarked the wrong link. Unfortunately now I cannot find the correct one (and I probably have referred several people to the wrong link :( )

Basically the technique in that Thread was to check whether the Pot is above or below the centre point. If it is above, move the servo in one direction. If it is below, move it in the other direction. To start with I suggest you just get the servos to move at a constant speed. Later you could extend the code so that a bigger movement of the Pot causes faster servo movement.

Note that with this approach the servo does NOT reflect the position of the Pot.

...R

well, the point of the capacitors was to store energy as servos take extra juice to get started up, a 9 volt could only power one servo and it went quickly, im using 4 servos so 4 100nf capacitors worked well

Mike44449: well, the point of the capacitors was to store energy

100 nF ? ? ? - have you got nano-servos ?

Servos normally need between 4.8v and 6v - not 9v, which will probably damage them.

If you are thinking of using a PP3 type of 9v battery - DON'T. They are quite useless for powering motors or an Arduino.

Draw the diagram I requested in Reply #4

...R

Some code which doesn’t suffer the same cut-and-paste bugs as Zoomkat’s. (it’s shorter too)
You just need to populate the “potPin”, “servoPin” and “deadBand” arrays with the correct values.

#include <Servo.h>

#define DEBUG

const byte N_SERVOS = 2;

Servo myservo [N_SERVOS];
const byte potPin [N_SERVOS] = {0, 1};
const byte servoPin [N_SERVOS] = {2, 3};
const byte deadBand [N_SERVOS] = {2, 2};
int oldVal[N_SERVOS];

void setup() 
{
#ifdef DEBUG
  Serial.begin(9600);  
  Serial.println(F("testing dual pot servo"));  
#endif  
  for (byte i = 0; i < N_SERVOS; i++) {
    myservo[i].attach(servoPin [i]);  
  }  
}

void loop() 
{ 
  for (byte i = 0; i < N_SERVOS; i++) {
    int newval = analogRead(potPin[i]);           
    newval = map(newval, 0, 1024, 0, 180); 
    if ((newval < (oldVal[i] - deadBand [i])) || 
        (newval > (oldVal[i] + deadBand [i])))
    {  
      myservo[i].write(newval);
      oldVal[i] = newval;
#ifdef DEBUG
      Serial.print(i);
      Serial.print(F(" - "));
      Serial.println(newval);
#endif
    }  
  }
  delay(50);
}