Oh yeah, the soft option, and the analogWrite that is actually a PWM - it's not self-explaining. As a quick hack something like this:
interval = setInterval(function() {
currentPos = pos*amt + initial*(1-amt);
digitalPulse(pin, 1, offs+E.clip(currentPos,0,1)*mul);
if (amt >= 1) {
clearInterval(interval);
interval = undefined;
// hold the position if option hold is set
if (options.hold == true) {
// move the calculation of the pulse width out of the interval
// we might re-use 'initial' instead of adding 'pulseWidth' for low memory devices
var pulseWidth = offs+E.clip(pos,0,1)*mul;
// interval to hold the position (must use 'interval' as reference)
interval = setInterval(function() {
digitalPulse(pin, 1, pulseWidth);
}, 20);
}
if (callback) callback();
} else {
amt += 50*time); // equals amt += 1000.0 / (20*time);
}
}, 20);
Using option 'hold' instead of 'soft'
Use a first interval to move the servo (as it was before this change), and conditionally add a second interval to hold the position
It would be possible to add the option 'hold' to the 'connect' function, then there's no need to include it in every call to 'move'. The code is not tested.
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Oh yeah, the soft option, and the analogWrite that is actually a PWM - it's not self-explaining. As a quick hack something like this:
It would be possible to add the option 'hold' to the 'connect' function, then there's no need to include it in every call to 'move'. The code is not tested.