I am using this API function which I can not change:
xMotor.ccw().step(1, function() { /*callback*/ });
It serves to control a stepper motor which should physically turn the motor a certain amount of steps. Now I need to turn this motor until a button physically gets pressed and I want to measure the travelled length
. I went about this like so for now:
var length = 0;
async function initialize(){
while (motor.checkButton(buttonPin)) {
xMotor.ccw().step(1, function() { /*CALLBACK*/ });
length++;
await sleep(10);
}
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
But since this API function is non-blocking my length variable gets way to many counts. In reality the while loop is way to fast for physically moving the motor one step. I could just increase the sleep
value but this really does not seem like a good solution and the movement of the motor gets very jittery.
All I/O signals are running from a Arduino. And I'm using the Johnny-five framework.
Thanks in advance!