#include <Servo.h>

Servo myservo;       // Create a servo object
int potPin = A0;      // Define the analog pin for the potentiometer
int delayTime = 0;    // Variable to store the delay time
int lastUpdateTime = 0; // Variable to store the last update time

const int ledPin = 13;  // LED connected to digital pin 13

void setup() {
  myservo.attach(9);  // Attaches the servo on pin 9
  pinMode(ledPin, OUTPUT);  // Set the LED pin as an output
}

void loop() {
  int potValue = analogRead(potPin);  // Read the potentiometer value
  delayTime = map(potValue, 0, 1023, 100, 5000);  // Map the potentiometer value to delay time 1000 = 1s

  int currentTime = millis();  // Get the current time

  if (currentTime - lastUpdateTime >= delayTime) {
    myservo.write(90);  // Move the servo to the middle position (adjust as needed)
    delay(250);  // Small delay for servo to reach the desired position
    myservo.write(0);   // Move the servo to the minimum position (adjust as needed)

    lastUpdateTime = currentTime;  // Update the last update time
  }

  // Blink the LED every second
  if (currentTime % 1000 == 0) {
    digitalWrite(ledPin, HIGH);
    delay(100);  // Small delay for LED to be visible
    digitalWrite(ledPin, LOW);
  }
}