I changed your code a bit, and now everything works as foreseen. Here is my complete test
//var Servo = require('servo').connect(C7, { range:2 });
function Servo_connect (pin,options) {
var interval, currentPos;
var offs = 1, mul = 1;
if (options && options.range) {
mul = options.range;
offs = 1.5-(mul/2);
}
return {move:function(pos, time, callback) {
if (typeof time === 'function') {
callback = time; time = undefined;
}
if (typeof time !== 'number') time = 1000;
var amt = 0;
if (currentPos===undefined) currentPos = pos;
if (interval)
clearInterval(interval);
var initial = currentPos;
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;
if (callback) callback();
return;
} else {
amt += 1000.0 / (20*time);
}
}, 20);
}};
}
let Servo = Servo_connect(C7, { range:2 });
let Count = 0;
function move () {
if (Count < 10) {
Count++;
print('Count: ' + Count);
Servo.move(1, 1000, move);
}
}
move();
Since you clearInterval, an explicit return is not necessary, but it may be a good idea to restrict time to values > 0 in order to avoid a division by zero (or negative values)
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.
I changed your code a bit, and now everything works as foreseen. Here is my complete test
Since you
clearInterval
, an explicitreturn
is not necessary, but it may be a good idea to restricttime
to values > 0 in order to avoid a division by zero (or negative values)