You are reading a single comment by @allObjects and its replies. Click here to read the full conversation.
  • Btw, code in post #20 has to be changed to make proper direction changes in all step phases.

    Current code has a step sequence discontinuity / hick-up when changing direction in lines 36 and 43.

    After issuing a stop, there is also a small issue: if stopping in an 'instable' step phase - where two magnetic fields 'fight' for the rotor and move the rotor between the fighting poles - it may come to rest in either pole position - back or forward after digitalWrite of 0b000 / taking the power off. To compensate for that when continuing, power with the same last step phase has to be applied before moving on... - of course for the proper amount of time. Implementation depends on whether the setInterval(...) invokes the function the first time at beginning or end of the first interval, which I do not know.

    Simplest resolution is to remove the stop() in the direction change, change the stop, and - most important - change the sequence in the backward steps sequence and adjust the step adjustment in direction change.

    This are the 4 changed lines:

    16.var stBW = [0b1001,0b0001,0b0011,0b0010,0b0110,0b01­00,0b1100,0b1000];

    36.} else { if (stT) { st = Math.abs(st - 7); } if (!stI) { st--; } setI((stT = t),stFW," FW"); }

    43.} else { if (stT) { st = Math.abs(st - 7); } if (!stI) { st--; } setI(-(stT = t),stBW," BW"); }

    49.if (stI) { stI = clearInterval(stI); stI = null; }

    Resulting code:

    // stPs stepper pins
    // st   step 0..8
    // stT  step Time in milliseconds [ms]
    // stI  step Interval (from setInterval() and for clearInterval()
    // sts  steps 0001,0011,0010,... pin signals
    // stBW sts - steps Backwards
    // stFW sts - steps Forward
    // dmy  ...because of (cond) ? exprT : exprF needs something to assign to
    var run = false;
    var st = 0;
    var stT = 0;
    var stI = null;
    var sts = null;
    var stSt =  0b0000;
    var stFW = [0b1000,0b1100,0b0100,0b0110,0b0010,0b00­11,0b0001,0b1001];
    var stBW = [0b1001,0b0001,0b0011,0b0010,0b0110,0b01­00,0b1100,0b1000];
    var stPs = [C6,C7,C8,C9];
    
    // setI setInterval(i,stsC) i in [ms] with (optionl) step Change (if not null), 
    // and direction info (string)
    var setI = function(t,stsN,d) {
      console.log("t = ",t, d); 
      if (stI) clearInterval(stI);
      if (stsN) sts = stsN;
      run = true;
      stI = setInterval(stp,t);
    };
    
    // stp step
    var stp = function() { digitalWrite(stPs, sts[st = ++st % 8]); };
    
    // _sFW step ForWard
    var _sFW = function(t) {
      console.log("FW w/ " + t);
      if (stT > 0) { setI((stT = t),null," ~F");
      } else { if (stT) { st = Math.abs(st - 7); } if (!stI) { st--; } setI((stT = t),stFW," FW"); }
    };
    
    // _sBW step BackWards
    var _sBW = function(t) {
      console.log("BW w/ " + t);
      if (stT < 0) { setI(-(stT = t),null," ~B"); 
      } else { if (stT) { st = Math.abs(st - 7); } if (!stI) { st--; } setI(-(stT = t),stBW," BW"); }
    };
    
    // stop
    var stop = function() {
      console.log("stop");
      if (stI) { stI = clearInterval(stI); stI = null; }
      console.log(stI);
      run = false;
      digitalWrite(stPs, stSt);
    };
    
    
    // run function - t is stepping interval in [ms]
    var r = function(t) {  
      if (typeof t === "undefined" ) {
        if (stT) {
          console.log((stT > 0) ? "F>B" : "B>F");
          r(-stT);
        } else {
          console.log("What ?");
        }
      } else { 
        dmy = (t) ? (t>0) ? _sFW(t) : _sBW(t) : stop(); 
      }
    };
    
About

Avatar for allObjects @allObjects started