#include <Servo.h> // Library for controlling the servo motor
Servo myServo; // Make myServo object
void setup() { // setup function
pinMode(10, INPUT_PULLUP); // Connecting pin 10 as input with a pull up resistor
myServo.attach(3); // Connecting the servo motor to pin 3
}
void loop() { // looping function
if(digitalRead(10)==LOW){ // Condition when pin 10 is LOW (pushbutton pressed)
for(int i=75; i<110;i++){ // Loop to move the servo from 75 to 110 degree position
delay(10); // Stop for 10 milliseconds
myServo.write(i); // Set the servo position according to the value of i
delay(3); // Stop for 3 milliseconds
}
delay(500); // Stop for 500 milliseconds after reaching 110 degrees
for(int i=110; i>75;i--){ // Loop to move the servo from position 110 back to 75 degrees
delay(10); // Stop for 10 milliseconds
myServo.write(i); // Set the servo position according to the value of i
delay(3); // Stop for 3 milliseconds
}
delay(100); // Stop for 100 milliseconds
}
if(digitalRead(10)==HIGH){ // Condition when pin 10 is HIGH (pushbutton not pressed)
myServo.write(75); // Set the servo position to 75 degrees
}
delay(200); // Stop for 200 milliseconds before the next loop
}