#includeServo myservo; void setup() { myservo.attach(9); // attaches the servo on pin 9 to the servo object } void loop() { for (int pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } for (int pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } }
#includeThe Servo library is included in the Arduino IDE.Servo myservo; // create servo object to control a servo int potpin = 0; // analog pin used to connect the potentiometer int val; // variable to read the value from the analog pin void setup() { myservo.attach(9); // attaches the servo on pin 9 to the servo object } void loop() { val = analogRead(potpin); // read the value from the potentiometer val = map(val, 0, 1023, 0, 180); // map the value to a range from 0 to 180 myservo.write(val); // set the servo angle to the value read from the potentiometer delay(15); // wait for the servo to get there }