#include "Servo.h"

#define minInterval 500
#define maxInterval 4000
Servo myServo;

void setup() {
  myServo.attach(2);
  pinMode(3,INPUT_PULLUP);
  myServo.write(0);
}

void loop() {
  static bool servoState=false;
  static uint64_t servoTimer=millis();
  if (millis()>=servoTimer) {
    if (digitalRead(3)) {
      servoState=false;
    } else {
      servoState^=true;
      servoTimer=millis()+map(analogRead(A0),0,1023,minInterval,maxInterval);
    }
    myServo.write(180*servoState);
  }
}