const int servoPin = 2; // set the pin number for the servo int angle = 0; // variable for rotation angle void setup() { pinMode(servoPin, OUTPUT); // set servo pin as output } void loop() { for(angle = 0; angle < 180; angle += 1){ // rotate the servo from 0 to 180 degrees digitalWrite(servoPin, HIGH); // send a high signal to the servo pin delayMicroseconds(500 + (angle * 2000 / 180)); // set the delay corresponding to the angle of rotation digitalWrite(servoPin, LOW); // send low signal to servo pin delay(20); // delay to stabilize the servo } for(angle = 180; angle > 0; angle -= 1){ // rotate the servo from 180 to 0 degrees digitalWrite(servoPin, HIGH); delayMicroseconds(500 + (angle * 2000 / 180)); digitalWrite(servoPin, LOW); delay(20); } }