• Hi! That's great - the A4988 stepper drivers do a lot of the work for you so you don't have to do much except count pulses.

    How they relate to how the motor turns depends on the motor and also the MS1/2/3 pins (info here) - but lets say your motor is marked as a 48 steps/revolution and you have 16x microstepping set up with the MS pins, that'd be 48*16=768 steps per revolution.

    What you're doing with analogWrite is getting Espruino to use the on-chip hardware to do the stepping - which makes it very reliable but also means it's hard to accurately count the amount of steps.

    While it's possible to send out a certain number of steps using hardware on the MDBT42, that's getting pretty hardcore, and for 100Hz steps you're fine using software.

    you could try something like this:

    function doSteps(cnt, callback) {
      if (cnt<=0) {
        if (callback) callback();
        return;
      }
      PIN_STEP.set();
      PIN_STEP.reset();
      setTimeout(doSteps, 10/*millisecs*/, cnt-1, callback);
    }
    
    // rotate 100 steps
    digitalWrite(PIN_DIRECTION, 1);
    doSteps(100);
    

    or to rotate 100 forward, 100 back:

    digitalWrite(PIN_DIRECTION, 1);
    doSteps(100, function() {
      digitalWrite(PIN_DIRECTION, 0);
      doSteps(100, function() {
       print("Done!");
      });
    });
    
About

Avatar for Gordon @Gordon started