// project made by pranjal kharel
#include <Servo.h>
Servo myservo; // create servo object
int val = 90; // default position (middle)
void setup() {
myservo.attach(9); // attach servo to pin 9
Serial.begin(9600); // start serial communication
myservo.write(val); // move to default position
Serial.println("Enter angle (0 to 180):");
}
void loop() {
if (Serial.available() > 0) {
int input = Serial.parseInt(); // get the user input
if (input >= 0 && input <= 180) {
val = input; // update position
myservo.write(val); // move servo
Serial.print("Servo moved to: ");
Serial.println(val);
} else {
Serial.println("Invalid angle. Enter a value between 0 and 180.");
}
// Clear buffer
while (Serial.available() > 0) {
Serial.read();
}
}
}