#include <Servo.h>

Servo servo1;
Servo servo2;

int x_key = A1;
int y_key = A0;
int x_pos;
int y_pos;
int servo1_pin = 8;
int servo2_pin = 9;
int initial_position1 = 90;
int initial_position2 = 90;

void setup() {
  Serial.begin(9600);

  servo1.attach(servo1_pin);
  servo2.attach(servo2_pin);

  servo1.write(initial_position1);
  servo2.write(initial_position2);

  pinMode(x_key, INPUT);
  pinMode(y_key, INPUT);
}

void loop() {
  // Read joystick values
  x_pos = analogRead(x_key);
  y_pos = analogRead(y_key);

  // X-axis control for servo1
  if (x_pos < 300) {
    if (initial_position1 >= 10) {
      initial_position1 -= 20;  // Decrease position
      servo1.write(initial_position1);
      delay(100);
    }
  } 
  else if (x_pos > 700) {
    if (initial_position1 <= 180) {
      initial_position1 += 20;  // Increase position
      servo1.write(initial_position1);
      delay(100);
    }
  }

  // Y-axis control for servo2
  if (y_pos < 300) {
    if (initial_position2 >= 10) {
      initial_position2 -= 20;  // Decrease position
      servo2.write(initial_position2);
      delay(100);
    }
  } 
  else if (y_pos > 700) {
    if (initial_position2 <= 180) {
      initial_position2 += 20;  // Increase position
      servo2.write(initial_position2);
      delay(100);
    }
  }
}