#include <Servo.h>
Servo servoBase;
Servo servoArm;
Servo servoFore;
Servo servoGrip;
Servo myServo;

int x1_key = A3;                                          
int y1_key = A2;
int x2_key = A1;                                               
int y2_key = A0;                                               
int x1_pos;
int y1_pos;
int x2_pos;
int y2_pos;
int potpin = 4;
int val;

int servoBase_pin = 3;
int servoArm_pin = 5;  
int servoFore_pin = 9;
int servoGrip_pin = 11;
int initial_position1 = 45;
int initial_position2 = 45;
int initial_position3 = 45;
int initial_position4 = 45;

void setup ( ) {
Serial.begin (9600) ;
servoBase.attach (servoBase_pin ) ; 
servoArm.attach (servoArm_pin ) ; 
servoFore.attach(servoFore_pin);
servoGrip.attach(servoGrip_pin);
servoBase.write (initial_position1);
servoArm.write (initial_position2);
servoFore.write(initial_position3);
servoGrip.write(initial_position4);
pinMode (x1_key, INPUT) ;                     
pinMode (y1_key, INPUT) ; 
pinMode (x2_key, INPUT) ;                     
pinMode (y2_key, INPUT) ;    
myServo.attach(10);                 
}
void loop ( ) {
  val = analogRead(potpin);
  val = map(val, 0, 1023, 0, 180);
  myServo.write(val);
  delay(15);

x1_pos = analogRead (x1_key) ;  
y1_pos = analogRead (y1_key) ;
x2_pos = analogRead (x2_key) ;  
y2_pos = analogRead (y2_key) ;                       
if (x1_pos < 300){
  if (initial_position1 < 10) { 
  } 
else{ 
  initial_position1 = initial_position1 - 5; 
  servoBase.write ( initial_position1 ) ; 
  delay (30) ; } } 
if (x1_pos > 700){
  if (initial_position1 > 180){  
    }  
  else{
    initial_position1 = initial_position1 + 5;
    servoBase.write ( initial_position1 ) ;
    delay (30) ;
  }
}
if (y1_pos < 300){
  if (initial_position2 < 10) { 
    } 
  else{ 
    initial_position2 = initial_position2 - 5; 
    servoArm.write ( initial_position2 ) ; 
    delay (30) ; 
    } 
  } 
if (y1_pos > 700){
  if (initial_position2 > 180){  
  }        
else{
initial_position2 = initial_position2 + 5;
servoArm.write ( initial_position2 ) ;
delay (30) ;
  }
}
if (x2_pos < 300){
  if (initial_position3 < 10) { 
  } 
else{ 
  initial_position3 = initial_position3 - 5; 
  servoFore.write ( initial_position3 ) ; 
  delay (30) ; } } 
if (x2_pos > 700){
  if (initial_position3 > 180){  
    }  
  else{
    initial_position3 = initial_position3 + 5;
    servoFore.write ( initial_position3 ) ;
    delay (30) ;
  }
}
if (y2_pos < 300){
  if (initial_position4 < 10) { 
    } 
  else{ 
    initial_position4 = initial_position4 - 5; 
    servoGrip.write ( initial_position4 ) ; 
    delay (30) ; 
    } 
  } 
if (y2_pos > 700){
  if (initial_position4 > 180){  
  }        
else{
initial_position4 = initial_position4 + 5;
servoGrip.write ( initial_position4 ) ;
delay (30) ;
  }
}
}