#include Servo myservo; int pos = 0; // variable to store the servo position void setup() { myservo.attach(20); // attach the servo on pin 9 to the servo object Serial.begin(9600); // start serial communication at 9600bps } void loop() { if (Serial.available() > 0) { int command = Serial.read(); // read the incoming byte if (command == '1') { // Rotate servo for (pos = 0; pos <= 180; pos += 1) { myservo.write(pos); delay(15); } } else if (command == '0') { // Stop servo (return to position 0) myservo.write(0); } } }