//divo odilucky santoso
//2206822326
#include <Servo.h>
Servo myservo;
int button1Pin = 5;
int button2Pin = 6;
int button3Pin = 7;
int x = 326/8;
int pos1 = 20 + x;
int pos2 = 90 - x;
int pos3 = 120 + x;
int currentPos = 0;
void setup() {
myservo.attach(9);
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);
pinMode(button3Pin, INPUT);
myservo.write(currentPos);
}
void loop() {
if (digitalRead(button1Pin) == HIGH) {
moveToPosition(pos1);
} else if (digitalRead(button2Pin) == HIGH) {
moveToPosition(pos2);
} else if (digitalRead(button3Pin) == HIGH) {
moveToPosition(pos3);
}
}
void moveToPosition(int targetPos) {
if (currentPos == targetPos) {
return;
}
int step = (targetPos > currentPos) ? 1 : -1;
int totalSteps = abs(targetPos - currentPos);
int delayTime = 2000 / totalSteps;
for (int pos = currentPos; pos != targetPos; pos += step) {
myservo.write(pos);
delay(delayTime);
}
currentPos = targetPos;
}