Skip to content

Commit

Permalink
Cleaned up README.md, and minor fixes to ServoSequence example comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip van Allen committed Dec 16, 2013
1 parent 56bfb82 commit f9a2eb5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 29 deletions.
47 changes: 26 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
VarSpeedServo
VarSpeedServo.h
===============

This Arduino library allows the use of up to 8 servos used with an Arduino. Since it uses interrupts, servos can run asynchronously. The libarary also permits the setting of the speed of a move, and a write() call to position can wait and only return when the move is finished.
The VarSpeedServo.h Arduino library allows the use of up to 8 servos moving asynchronously (because it uses interrupts). In addition, you can set the speed of a move, optionally wait (block) until the servo move is complete, and create sequences of moves that run asynchronously.

This code is an adaptation of the standard Arduino Servo library, which was first adapted by Korman and posted on the [Arduino forum](http://forum.arduino.cc/index.php?topic=61586.0) to add the speed capability. Philip van Allen updated it for Arduino 1.0 + and added the ability to to wait for the move to complete.
This code is an adaptation of the standard Arduino Servo.h library, which was first adapted by Korman and posted on the [Arduino forum](http://forum.arduino.cc/index.php?topic=61586.0) to add the speed capability. Philip van Allen updated it for Arduino 1.0 + and added the ability to to wait for the move to complete.

* Supports up to 8 servos
* Allows simultaneous, asynchronous movement of all servos
* Speed of the move can be set
* A servo write() function to set a new position can wait for completion before returning
* Allows the use of asynchronous sequences of positions, where the servo can go at a specified speed to each position in order
* The speed of a move can be set
* The write() function initiates a move and can optionally wait for completion of the move before returning
* A servo can be sent a sequence of moves (where each move has a position and speed)

Sample Code
----------------------------

#include <VarSpeedServo.h>
VarSpeedServo myservo; // create servo object to control a servo
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
myservo.write(180, 30, true); // move to 180 degrees, use a speed of 30, wait until move is complete
myservo.write(0, 30, true); // move to 0 degrees, use a speed of 30, wait until move is complete
}
```
#include <VarSpeedServo.h>
VarSpeedServo myservo; // create servo object to control a servo
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
myservo.write(180, 30, true); // move to 180 degrees, use a speed of 30, wait until move is complete
myservo.write(0, 30, true); // move to 0 degrees, use a speed of 30, wait until move is complete
}
```

Additional examples are included in the distribution and are available in the Arduino Examples section.

Expand All @@ -40,17 +43,19 @@ VarSpeedServo - Class for manipulating servo motors connected to Arduino pins. M
attach(pin, min, max ) - Attaches to a pin setting min and max values in microseconds
default min is 544, max is 2400

write(value) - Sets the servo angle of value in degrees. (invalid angle that is valid as pulse in microseconds is treated as microseconds)
write(value) - Sets the servo angle in degrees. (invalid angle that is valid as pulse in microseconds is treated as microseconds)
write(value, speed) - speed varies the speed of the move to new position 0=full speed, 1-255 slower to faster
write(value, speed, wait) - wait is a boolean that, if true, causes the function call to block until move is complete

writeMicroseconds() - Sets the servo pulse width in microseconds
read() - Gets the last written servo pulse width as an angle between 0 and 180.
readMicroseconds() - Gets the last written servo pulse width in microseconds. (was read_us() in first release)
readMicroseconds() - Gets the last written servo pulse width in microseconds. (was read_us() in first release)
attached() - Returns true if there is a servo attached.
detach() - Stops an attached servos from pulsing its i/o pin.

slowmove(value, speed) - The same as write(value, speed), retained for compatibility with Korman's version
slowmove(value, speed) - The same as write(value, speed), retained for compatibility with Korman's version

stop() - stops the servo at the current position

sequencePlay(sequence, sequencePositions); // play a looping sequence starting at position 0
sequencePlay(sequence, sequencePositions, loop, startPosition); // play sequence with number of positions, loop if true, start at position
Expand Down
29 changes: 23 additions & 6 deletions VarSpeedServo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,16 +396,16 @@ void VarSpeedServo::write(int value, uint8_t speed) {

if (speed) {
if (value < MIN_PULSE_WIDTH) {
// treat values less than 544 as angles in degrees (valid values in microseconds are handled as microseconds)
// updated to use constrain instead of if, pva
value = constrain(value, 0, 180);
value = map(value, 0, 180, SERVO_MIN(), SERVO_MAX());
// treat values less than 544 as angles in degrees (valid values in microseconds are handled as microseconds)
// updated to use constrain instead of if, pva
value = constrain(value, 0, 180);
value = map(value, 0, 180, SERVO_MIN(), SERVO_MAX());
}
// calculate and store the values for the given channel
byte channel = this->servoIndex;
if( (channel >= 0) && (channel < MAX_SERVOS) ) { // ensure channel is valid
// updated to use constrain instead of if, pva
value = constrain(value, SERVO_MIN(), SERVO_MAX());
// updated to use constrain instead of if, pva
value = constrain(value, SERVO_MIN(), SERVO_MAX());

value = value - TRIM_DURATION;
value = usToTicks(value); // convert to ticks after compensating for interrupt overhead - 12 Aug 2009
Expand Down Expand Up @@ -512,3 +512,20 @@ void VarSpeedServo::sequenceStop() {
this->curSeqPosition = CURRENT_SEQUENCE_STOP;
}

/*
To do
int VarSpeedServo::targetPosition() {
byte channel = this->servoIndex;
return map( servos[channel].target+1, SERVO_MIN(), SERVO_MAX(), 0, 180);
}
int VarSpeedServo::targetPositionMicroseconds() {
byte channel = this->servoIndex;
return servos[channel].target;
}
bool VarSpeedServo::isMoving() {
byte channel = this->servoIndex;
int servos[channel].target;
}
*/
4 changes: 2 additions & 2 deletions examples/ServoSequence/ServoSequence.ino
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(analogPin);
if (sensorValue > 200) {
myservo1.sequencePlay(slow, 3); // play sequence slowHalf that has 3 positions, loop and start at first position
myservo1.sequencePlay(slow, 3); // play sequence "slowHalf" that has 3 positions, loop and start at first position
} else {
myservo1.sequencePlay(twitchy, 4, true, 2); // play sequence 1, loop, start at third position
myservo1.sequencePlay(twitchy, 4, true, 2); // play sequence "twitchy", loop, start at third position
}
delay(2); // delay in between reads for analogin stability
}
Expand Down

4 comments on commit f9a2eb5

@GIANNHSitia
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hello.

Happy NEW year my friend. i find maybe a bug. the command "stop" no work

@GIANNHSitia
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to suggest you add another command,The movement of the servo to th is done for the Time write(value,time) . example Servo1.write(2400,5000);, move the servo 1 from old positio to now 2400us for 5000ms (5sec)

@athanasiuskircher
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, I like this library, it helps a lot with the project that I am working on: however is there any way to increase the limitation on servos? (the 8 that are the max) I am working with a mega and I need 12

@pvanallen
Copy link
Collaborator

@pvanallen pvanallen commented on f9a2eb5 Apr 15, 2014 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.