I just wrote this after cobbling together several different sketches I found on the web and it seems to work fairly well:
// 4 button 2 servo controller w pot slew v 3.d
#include <Servo.h>
Servo servo1; // define servo aliases
Servo servo2;
#define leftPin 2 // define buttons attached to pins
#define rightPin 3
#define upPin 4
#define downPin 5
int pos1 = 90; // set angle when inialitized
int pos2 = 135;
#define POT 3 // pot to analog pin 3
int potValue = 0; // variable for pot value
void setup()
{
servo1.write(pos1); // Put servo1 at home position
servo2.write(pos2); // Put servo1 at home position
servo1.attach(10); // attaches the servo on pin 9 to the servo object
servo2.attach(9); // attaches the servo on pin 10 to the servo object
pinMode(leftPin, INPUT);
pinMode(rightPin, INPUT);
pinMode(upPin, INPUT);
pinMode(downPin, INPUT);
}
void loop()
{
//pot control
{
potValue = analogRead(potValue); // reads the value of the potentiometer
// (value between 0 and 1023)
potValue = potValue/4; // convert from 0-1023 to 0-255
}
// left/right servo control
if(digitalRead(leftPin) == HIGH) // left button instructions
{
// in steps of 1 degree
if( pos1 > 0)
--pos1;
servo1.write(pos1); // tell servo to go to position in variable 'pos1'
delay(potValue);
}
if(digitalRead(rightPin) == HIGH) // right button instructions
{
if( pos1 < 180)
++pos1;
servo1.write(pos1); // tell servo to go to position in variable 'pos1'
delay(potValue);
}
// up/down servo control
if(digitalRead(upPin) == HIGH) // up button instructions
{
// in steps of 1 degree
if( pos2 > 0)
--pos2;
servo2.write(pos2); // tell servo to go to position in variable 'pos2'
delay(potValue);
}
if(digitalRead(downPin) == HIGH) // down button instructions
{
if( pos2 < 180)
++pos2;
servo2.write(pos2); // tell servo to go to position in variable 'pos2'
delay(potValue);
}
}
At first it was written so a numerical parameter was inserted in the int potValue = 0; section but my need requires me to adjust the speed on-the-fly.
I can send you the numerical sketch is you wish.
This sketch controls two servos but it can be pared down if needed.
Good luck.
Peace.