Arduino library · Motion

VarSpeedServo: an example that runs

Yes. You can use the VarSpeedServo library online in Mokxi with nothing to install, and the sketch runs in your browser against the simulated part (Hobby servo). The example below, "A servo moved at a set speed in the background", runs on the Arduino Uno R3.

Built in on Mokxi: #include <VarSpeedServo.h> and it builds, with nothing to install. Free, and no account needed.

What VarSpeedServo does

Servos that move at a set speed in the background: write(angle, speed, wait), slowmove(), isMoving(), wait(), stop(), read(), and sequencePlay() through position and speed points. That is what VarSpeedServo, by Korman, Philip van Allen, covers. The sketch below uses it on the Arduino Uno: “A servo moved at a set speed in the background”.

The VarSpeedServo library's interface (Korman, after Michael Margolis' Servo; maintained by Philip van Allen), servos that move at a set speed, in the background or waiting, and play sequences.

The simulated chip runs the timer, so the pulses are real. Timer 1 is also what analogWrite() uses on pins 9 and 10 (11 and 12 on the Mega), so, as upstream, those two give up PWM once a servo is attached.

The calls follow release 1.1.4 of the library, so a sketch written here builds on a real board too. The example is the one Mokxi's own tests build and run, so what is printed here is what runs.

Library Manager nameVarSpeedServo
AuthorKorman, Philip van Allen
Include#include <VarSpeedServo.h>
BoardsArduino Uno, Arduino Nano, Arduino Mega 2560, Arduino Leonardo, SparkFun Pro Micro, Arduino Micro, Arduino Pro Mini 3.3 V
In the simulatorSimulated: it runs against the part the way it does on the bench.
Parts it drivesHobby servo

Install it

It builds on Arduino Uno, Arduino Nano, Arduino Mega 2560, Arduino Leonardo, SparkFun Pro Micro, Arduino Micro, Arduino Pro Mini 3.3 V.

In Mokxi it is built in: #include <VarSpeedServo.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "VarSpeedServo" by Korman, Philip van Allen from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add VarSpeedServo to lib_deps in PlatformIO. Mokxi follows the calls of release 1.1.4, so a sketch written here builds there too.

What to know in the simulator: Timer 1's compare interrupt pulses the servos every 20 ms, as upstream, so pins 9 and 10 (11 and 12 on the Mega) lose analogWrite() once a servo is attached. Speed is half-microseconds of pulse per 20 ms frame: speed 30 is about 73 degrees a second. Upstream is for the AVR boards only, and so is this.

The calls you will use

In the order a sketch meets them.

VarSpeedServo myservo

Makes myservo, the VarSpeedServo object the calls below are made on.

myservo.attach(9)

Attaches it and returns a whole number.

myservo.isMoving()

Returns true or false for "is moving".

myservo.read()

Returns the value as a whole number.

Example: A servo moved at a set speed in the background

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino · Arduino Uno R3
#include <VarSpeedServo.h>

VarSpeedServo myservo;  // create servo object to control a servo

void setup() {
  Serial.begin(9600);
  myservo.attach(9);        // attaches the servo to the pin
  myservo.write(0, 255, true);   // to 0 degrees at full speed, and wait
  Serial.println("at 0");

  // Start a slow move and keep working while it runs.
  myservo.write(90, 60, false);
  unsigned long start = millis();
  unsigned long loops = 0;
  while (myservo.isMoving()) {
    loops++;
  }
  Serial.print("moved 90 degrees in ");
  Serial.print(millis() - start);
  Serial.println(" ms");
  Serial.print(loops > 1000 ? "loop kept running, " : "blocked, ");
  Serial.print("at ");
  Serial.println(myservo.read());
}

void loop() {}

Wiring

  • Arduino Uno R3 9 to Hobby servo PWM
  • Arduino Uno R3 5V to Hobby servo VCC
  • Arduino Uno R3 GND to Hobby servo GND

Open it in the editor, wired and ready to run

Try VarSpeedServo without installing anything

Open the example, change the sketch, and keep your version in a free account.