#include <Servo.h>

Servo servo1;
Servo servo2;

int x_pin = A1;
int y_pin = A0;
int button = A2; 
int leftLED = 3;
int rightLED = 4;
int off_LED = 5;

int x_pos;
int y_pos;
int servo1_pin = 8;
int servo2_pin = 9;

int servo1_initialposition = 90;
int servo2_initialposition = 90;

bool leftLEDState = false;  
bool rightLEDState = false; 
bool leftLEDToggle = false; 
bool rightLEDToggle = false; 

unsigned long previousMillisLeftLED = 0;
unsigned long previousMillisRightLED = 0;
const long interval = 500; 

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

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

  servo1.write(servo1_initialposition);
  servo2.write(servo2_initialposition);

  pinMode(x_pin, INPUT);
  pinMode(y_pin, INPUT);
  pinMode(button, INPUT_PULLUP);

  pinMode(leftLED, OUTPUT);
  pinMode(rightLED, OUTPUT);
  pinMode(off_LED, OUTPUT);

  digitalWrite(leftLED, LOW);  
  digitalWrite(rightLED, LOW); 
}

void loop() {
  x_pos = analogRead(x_pin);
  y_pos = analogRead(y_pin);
  bool selectPressed = (analogRead(button) == LOW); 

  if (selectPressed) {
    leftLEDState = false;
    rightLEDState = false;
  } else {
    //x-axis control
    if (x_pos < 300) {
      if (servo1_initialposition >= 10) {
        servo1_initialposition -= 20;
        servo1.write(servo1_initialposition);
        delay(100);
      }
    } else if (x_pos > 700) {
      if (servo1_initialposition <= 180) {
        servo1_initialposition += 20;
        servo1.write(servo1_initialposition);
        delay(100);
      }
    }
    //y-axis control (blinkers)
    if (y_pos < 300 && !rightLEDState) {
      if (!leftLEDToggle) {
        leftLEDState = !leftLEDState;
        leftLEDToggle = true;
        if (servo2_initialposition >= 10) {
          servo2_initialposition -= 20; 
          servo2.write(servo2_initialposition);
        }  
      }
    } 
    else if (y_pos > 700 && !leftLEDState) {
      if (!rightLEDToggle) {
        rightLEDState = !rightLEDState; 
        rightLEDToggle = true;
        if (servo2_initialposition <= 180) {
          servo2_initialposition += 20;
          servo2.write(servo2_initialposition);
          delay(100);
        }
      }
    } else {
      leftLEDToggle = false;  
      rightLEDToggle = false;
    }
  }
  // Blinking logic for left and right LED
  if (leftLEDState) {
    unsigned long currentMillis = millis();
    if (currentMillis - previousMillisLeftLED >= interval) {
      previousMillisLeftLED = currentMillis;
      digitalWrite(leftLED, !digitalRead(leftLED));  // Toggle the LED
    }
  } else {
    digitalWrite(leftLED, LOW);
  }

  if (rightLEDState) {
    unsigned long currentMillis = millis();
    if (currentMillis - previousMillisRightLED >= interval) {
      previousMillisRightLED = currentMillis;
      digitalWrite(rightLED, !digitalRead(rightLED));  // Toggle the LED
    }
  } else {
    digitalWrite(rightLED, LOW);
  }

  // If both LEDs are off, turn on the off_LED
  if (!leftLEDState && !rightLEDState) {
    digitalWrite(off_LED, HIGH);  
  } else {
    digitalWrite(off_LED, HIGH);
  }
}