#include <Servo.h>
Servo myservo;
const int button1 = 13;
const int button2 = 12;
const int button3 = 11;
const float Position1 = 67.88;
const float Position2 = 42.13;
const float Position3 = 167.88;
void setup() {
myservo.attach(3);
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
myservo.write(90);
}
void loop() {
if (digitalRead(button1) == LOW) {
moveToPosition(Position1);
}
else if (digitalRead(button2) == LOW) {
moveToPosition(Position2);
}
else if (digitalRead(button3) == LOW) {
moveToPosition(Position3);
}
delay(50);
}
void moveToPosition(float targetPos) {
float currentPos = myservo.read();
float step = (targetPos - currentPos) / 200.0;
for (int i = 0; i < 200; i++) {
currentPos += step;
myservo.write(currentPos);
delay(10);
}
myservo.write(targetPos);
}