#include <Servo.h>  // Include the Servo library

// Servo objects
Servo servo1;
Servo servo2;

// Define joystick and LED pins
int x_key = A0;  // Joystick X-axis (left-right control)
int y_key = A1;  // Joystick Y-axis (forward-backward control)
int x_pos;       // Joystick X-axis position
int y_pos;       // Joystick Y-axis position

// Define LED pins for indicators
int ledLeft = 2;      // Left turn signal
int ledRight = 4;     // Right turn signal
int ledGreen = 3;     // Green LED for movement indication

// Initial servo positions
int initialServoPosition = 90; // Middle position (neutral)

void setup() {
  Serial.begin(9600);  // Start serial communication

  // Attach servos
  servo1.attach(8);  // Servo 1 attached to pin 8
  servo2.attach(9);  // Servo 2 attached to pin 9
  servo1.write(initialServoPosition);  // Set initial position
  servo2.write(initialServoPosition);

  // Setup pins for joystick and LEDs
  pinMode(x_key, INPUT);
  pinMode(y_key, INPUT);
  pinMode(ledLeft, OUTPUT);
  pinMode(ledRight, OUTPUT);
  pinMode(ledGreen, OUTPUT);

  // Initially, turn off all LEDs
  digitalWrite(ledLeft, LOW);
  digitalWrite(ledRight, LOW);
  digitalWrite(ledGreen, LOW);
}

void loop() {
  x_pos = analogRead(x_key);  // Read horizontal joystick value
  y_pos = analogRead(y_key);  // Read vertical joystick value

  // Handling left and right turns
  if (x_pos < 300) {  // Left turn
    servo1.write(120);  // Turn servos to the left
    servo2.write(120);
    digitalWrite(ledLeft, HIGH);  // Blink left LED
    delay(500);
    digitalWrite(ledLeft, LOW);
    delay(500);
  } else if (x_pos > 700) {  // Right turn
    servo1.write(60); // Turn servos to the right
    servo2.write(60);
    digitalWrite(ledRight, HIGH);  // Blink right LED
    delay(500);
    digitalWrite(ledRight, LOW);
    delay(500);
  } else {  // Center position
    servo1.write(initialServoPosition);  // Return servos to center
    servo2.write(initialServoPosition);
  }

  // Handling forward and backward movements
  if (y_pos < 300 || y_pos > 700) {
    digitalWrite(ledGreen, HIGH);  // Turn on green LED when moving
    delay(500);
  } else {
    digitalWrite(ledGreen, LOW);  // Turn off green LED
  }

  delay(500);  // Stability delay
}
$abcdeabcde151015202530fghijfghij