#define SERVO_PIN 0 // define the pin number for the servo const int servoMinAngle = 0; // define the minimum angle for the servo (in degrees) const int servoMaxAngle = 180; // define the maximum angle for the servo (in degrees) const int servoMinPulse = 500; // define the minimum pulse width for the servo (in microseconds) const int servoMaxPulse = 2500; // define the maximum pulse width for the servo (in microseconds) const int servoUpdateTime = 20; // define the update time for the servo (in milliseconds) void setup() { pinMode(SERVO_PIN, OUTPUT); // set the pin for the servo as an output } void loop() { // move the servo to 0 degrees (4.8V) setServoAngle(0, 0.1); // Angle: 0°, Speed: 0.1 s/60° delay(1000); // move the servo to 90 degrees (4.8V) setServoAngle(90, 0.1); // Angle: 90°, Speed: 0.1 s/60° delay(1000); // move the servo to 180 degrees (6.0V) setServoAngle(180, 0.09); // Angle: 180°, Speed: 0.09 s/60° delay(1000); } void setServoAngle(int angle, float speed) { int pulseWidth = map(angle, servoMinAngle, servoMaxAngle, servoMinPulse, servoMaxPulse); int pulseTime = pulseWidth * speed / 60; for (int i = 0; i < servoUpdateTime / pulseTime; i++) { digitalWrite(SERVO_PIN, HIGH); delayMicroseconds(pulseWidth); digitalWrite(SERVO_PIN, LOW); delay(servoUpdateTime - pulseTime); } }