You are reading a single comment by @allObjects and its replies. Click here to read the full conversation.
  • Used your code modified and built some stuff around to easily control it with a global command function r(). I chose r for now because I commanding it from the console.

    Function r accepts the stepping interval in milliseconds as argument.
    Special values are 0 and no argument (undefined), which stop the motor or reverse current direction, respectively. For latter, there exists also a global stop() command function.
    Changing direction does not need a stop command, the code manages that by itself. Positive values step clock-wise, negative ones counter clock-wise (looking at the spindle side).

    1. r(1); // the fastest - about 15 rpm - you can go
    2. r(); // reverses direction (after above r(1) it is like issuing r(-1);
    3. r(0); // stops the motor
    4. stop(); // stops the motor as well

    Note that a stop always stops at an even 'position' and switches all driving pins off.

    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,0b0011,0b0001,0b1001];
    var stBW = [0b0001,0b0011,0b0010,0b0110,0b0100,0b1100,0b1000,0b1001];
    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) { stop(); } 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) { stop(); } st++; setI(-(stT = t),stBW," BW"); }
    };
    
    // stop
    var stop = function() {
      console.log("stop");
      if (stI) { stI = clearInterval(stI); }
      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(); 
      }
    };
    

    To get to to higher speeds, a different approach then setInterval() has to be taken. In order not to hog the Espruino just for the stepper, an interrupt driven stepping in assembler needs to be implemented to get to shorter pulses.

    Attached clip shows 2 seconds fastest forward r(1);, then for same time and same speed reverse r();, then 5 seconds slower forward r(6);, and finally stop stop();

    The 2 seconds ticking you hear in the background comes from a digital clock standing close by... ;-)

    Console:

    >r(1); setTimeout(function(){r(); setTimeout(function(){ r(6); setTimeout(function(){ stop(); },5000); },2000); },2000);
    FW w/ 1
    t =  1  FW
    =2
    F>B
    BW w/ -1
    stop
    t =  1  BW
    FW w/ 6
    stop
    t =  6  FW
    stop
    >
    

    2 Attachments

About

Avatar for allObjects @allObjects started