Skip to content

Commit

Permalink
version 1.0.2. see changelog.txt
Browse files Browse the repository at this point in the history
  • Loading branch information
Koryphon committed Mar 1, 2018
1 parent 3d1c033 commit a58fde5
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
9 changes: 9 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
+---------------------------------------------------------------------------+
| SlowMotionServo changelog |
+---------------------------------------------------------------------------+
1.0.2 Default min and max have been changed to 1000 and 2000.
More accurate SMSSmoothBounce trajectory.
Fixed a problem with the initial position.
Added PushButton example.
1.0.1 Fix a typo in the library.properties
1.0 Initial release.
56 changes: 56 additions & 0 deletions examples/PushButton/PushButton.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Drive a servo by using a push button
* Uses the Bounce2 library. This library can be installed using the library manager
*/

#include <Servo.h>
#include <SlowMotionServo.h>
#include <Bounce2.h>

SMSSmoothBounce myServo;
Bounce myButton;

const byte servoPin = 3;
const byte buttonPin = 4;
const byte ledPin = 13;

void setup()
{
pinMode(ledPin, OUTPUT);
/* when the button is pressed, the input is LOW */
pinMode(buttonPin, INPUT_PULLUP);

myButton.attach(buttonPin);
/* scan interval for debounce */
myButton.interval(5);

myServo.setMin(800);
myServo.setMax(2200);
myServo.setSpeed(1.5);
myServo.setInitialPosition(0.0);
myServo.setPin(servoPin);
digitalWrite(ledPin, HIGH);
}

void loop()
{
static float servoTarget = 0.0;

/* update the state of the button */
myButton.update();
/* update the position of the servo */
SlowMotionServo::update();

if (myServo.isStopped()) {
digitalWrite(ledPin, LOW);
if (myButton.fell()) {
/* look at the button only when the servo is stopped */
/* change the target */
servoTarget = 1.0 - servoTarget;
/* set the new target for the servo */
myServo.goTo(servoTarget);
digitalWrite(ledPin, HIGH);
}
}
}

2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=SlowMotionServo
version=1.0.1
version=1.0.2
author=Jean-Luc - Locoduino
maintainer=Jean-Luc - Locoduino
sentence=This library allows to move multiple servos slowly.
Expand Down
22 changes: 17 additions & 5 deletions src/SlowMotionServo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@
#include <SlowMotionServo.h>

static const byte NOPIN = 255;
enum { SERVO_STOPPED, SERVO_UP, SERVO_DOWN, SERVO_DELAYED_UP, SERVO_DELAYED_DOWN };
enum {
SERVO_INIT,
SERVO_STOPPED,
SERVO_UP,
SERVO_DOWN,
SERVO_DELAYED_UP,
SERVO_DELAYED_DOWN
};

/*
* Static member to store a list of servos. This allow to update the
Expand All @@ -46,10 +53,10 @@ unsigned int SlowMotionServo::sDelayUntilStop = 10;
*/
SlowMotionServo::SlowMotionServo() :
mPin(NOPIN),
mState(SERVO_STOPPED),
mState(SERVO_INIT),
mDetachAtMin(false),
mDetachAtMax(false),
mMinPulse(544),
mMinPulse(1000),
mMaxPulse(2400),
mInitialRelativeTime(0.0),
mTargetRelativeTime(0.0),
Expand Down Expand Up @@ -187,6 +194,11 @@ void SlowMotionServo::updatePosition()
unsigned long date = millis();

switch (mState) {
case SERVO_INIT:
position = slopeUp(mCurrentRelativeTime);
writeMicroseconds(position * (mMaxPulse - mMinPulse) + mMinPulse);
mState = SERVO_STOPPED;
break;
case SERVO_UP:
mCurrentRelativeTime = (float)(date - mStartTime) * mTimeFactorUp +
mInitialRelativeTime;
Expand Down Expand Up @@ -275,12 +287,12 @@ float SMSSmooth::slopeDown(float time)

float SMSSmoothBounce::slopeUp(float time)
{
if (time <= 0.79) {
if (time <= 0.795) {
return (1.0 - cos(time * PI))/1.8;
}
else {
float timeOff = 10.0 * (time - 0.55);
return (0.834 + 1.0 / (timeOff * timeOff));
return (0.83 + 1.0 / (timeOff * timeOff));
}
}

Expand Down

0 comments on commit a58fde5

Please sign in to comment.