// Joystick and Servo Control with LED by Technical Shubham

#include <Servo.h>

Servo servo1;
Servo servo2;

int joyX = A0;      // Analog pin for X-axis
int joyY = A1;      // Analog pin for Y-axis
int buttonPin = 2;  // Digital pin for joystick button
int ledPin = 13;    // Digital pin for LED

int buttonState = 0; // Variable to store button state
int joyVal;

void setup()
{
  initHardware();  // Call the hardware initialization function
}

void loop()
{
  joyVal = analogRead(joyX);
  joyVal = map(joyVal, 0, 1023, 0, 180);
  servo1.write(joyVal);

  joyVal = analogRead(joyY);
  joyVal = map(joyVal, 0, 1023, 0, 180);
  servo2.write(joyVal);

  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) {
    digitalWrite(ledPin, HIGH); // Turn on the LED when the button is pressed
  } else {
    digitalWrite(ledPin, LOW);  // Turn off the LED when the button is not pressed
  }

  delay(20);
}

void initHardware()
{
  servo1.attach(3);
  servo2.attach(4);
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
}