#include <Servo.h>

int xpin = A0;  // X-axis pin
int ypin = A1;  // Y-axis pin
int spin = 2;   // Joystick button pin

int xpinval;  // X-axis pin value
int ypinval;  // Y-axis pin value
int spinval;  // Joystick button state
int dt = 250; // Delay time

Servo servo1;  // Vertical servo (Y-axis)
Servo servo2;  // Horizontal servo (X-axis)

void setup() {
  pinMode(xpin, INPUT);
  pinMode(ypin, INPUT);
  pinMode(spin, INPUT);
  digitalWrite(spin, HIGH);
  Serial.begin(9600);

  // Attach servos to pins
  servo1.attach(9);  // Attach vertical servo to pin 9
  servo2.attach(10); // Attach horizontal servo to pin 10
}

void loop() {
  // Read joystick values
  xpinval = analogRead(xpin);
  ypinval = analogRead(ypin);
  spinval = digitalRead(spin);

  // Map joystick values to servo angle range (0 to 180 degrees)
  int verticalAngle = map(ypinval, 0, 1023, 0, 180);  // Map Y-axis to 0-180°
  int horizontalAngle = map(xpinval, 0, 1023, 0, 180); // Map X-axis to 0-180°

  // Control servos based on joystick input
  servo1.write(verticalAngle);  // Set vertical servo angle
  servo2.write(horizontalAngle); // Set horizontal servo angle

  // Print the values for debugging
  Serial.print("xpin value is: ");
  Serial.print(xpinval);
  Serial.print("; & ");
  Serial.print("angle of servo1 is: ");
  Serial.println(verticalAngle);
  


  Serial.print("ypin value is: ");
  Serial.print(ypinval);
  Serial.print("; & ");
  Serial.print("angle of servo2 is: ");
  Serial.println(horizontalAngle);
  
  delay(dt);  // Delay for a short time
}
74HC595
74HC595