#include Servo motor; const int stopUs = 1500; // Adjust for your servo's actual neutral. unsigned long startedAt = 0; bool moving = false; void setup() { Serial.begin(9600); motor.attach(9); motor.writeMicroseconds(stopUs); Serial.println("f: forward, r: reverse, s: stop; automatic stop after 2 seconds"); } void loop() { while (Serial.available() > 0) { const char command = Serial.read(); if (command == 'f' || command == 'r') { motor.writeMicroseconds(stopUs + (command == 'f' ? 100 : -100)); startedAt = millis(); moving = true; } else if (command == 's') { motor.writeMicroseconds(stopUs); moving = false; } } if (moving && millis() - startedAt >= 2000) { motor.writeMicroseconds(stopUs); moving = false; } }