#include <Servo.h>
const int buttonForwardPin = 2;
const int buttonBackwardPin = 3;
const int buttonStop1Pin = 4;
const int buttonStop2Pin = 5;
Servo myServo;
//Die Taster sind an die Pins 2, 3, 4 und 5 des Arduino angeschlossen.
int currentAngle = 90; // Initial position of the servo
void setup() {
myServo.attach(9); // Attach the servo to pin 9
myServo.write(currentAngle); // Set the initial position of the servo
pinMode(buttonForwardPin, INPUT_PULLUP);
pinMode(buttonBackwardPin, INPUT_PULLUP);
pinMode(buttonStop1Pin, INPUT_PULLUP);
pinMode(buttonStop2Pin, INPUT_PULLUP);
}
void loop() {
if (digitalRead(buttonForwardPin) && digitalRead(buttonBackwardPin) == HIGH){
stopServo();
}
else{
if (digitalRead(buttonForwardPin) == LOW) {
moveForward();
} else if (digitalRead(buttonBackwardPin) == LOW) {
moveBackward();
} else if (digitalRead(buttonStop1Pin) == LOW || digitalRead(buttonStop2Pin) == LOW) {
stopServo();
}
}
}
void moveForward() {
if (currentAngle <= 180) {
currentAngle++;
myServo.write(currentAngle);
delay(15); // Adjust delay for smoother movement
}
}
void moveBackward() {
if (currentAngle > 0) {
currentAngle--;
myServo.write(currentAngle);
delay(15); // Adjust delay for smoother movement
}
}
void stopServo() {
// No action required to stop the servo in this simple implementation
}