2

I have this code to control my steppermotor in Javascript with an Espruino.

function motorStep(mySteps){
  var stepperPins = [C6,C7,C8,C9]; // Change these to your pins (digital output)
  var stepBits = [0b0110,0b0101,0b1001,0b1010];

  if (mySteps<0) //reverse
  {
    stepBits.reverse();
    mySteps = -mySteps;
  }

  for (i=0; i< mySteps ; i++)
  {
    digitalWrite(stepperPins, stepBits[i % stepBits.length]);
    //then we need to wait before sending next command
    wait(1); //some motors might need a longer delay
  }

}

function wait(ms){
  var d = new Date();
  var d2 = null;
  do { d2 = new Date(); }
  while(d2-d < ms);
}

I have made a setWatch function to look for a button press to then start a function

setWatch(function () {
action goes here
}, BTN2, {
 repeat : true,
 edge : "rising"
});

I am looking for the stepper motor to do a full 360 turn in 6 minutes. For the stepper to do a full 360 turn it's 350 steps so a 1030ms wait between each step (360000/350?). So when I press BTN2 the stepper spins 360 degrees over a 6 minute time span.

I am unsure how to combine the two, can any of you help me out?

If I do this:

setWatch(function () {
motorStep(350);
}, BTN2, {
 repeat : true,
 edge : "rising"
});

How would I code the time required to do the full action?

4

1 回答 1

1

更改您的 motorstep 函数以获取额外的参数:

function motorStep(mySteps, delay){
  var stepperPins = [C6,C7,C8,C9]; // Change these to your pins (digital output)
  var stepBits = [0b0110,0b0101,0b1001,0b1010];

  if (mySteps<0) //reverse
  {
    stepBits.reverse();
    mySteps = -mySteps;
  }

  for (i=0; i< mySteps ; i++)
  {
    digitalWrite(stepperPins, stepBits[i % stepBits.length]);
    //then we need to wait before sending next command
    wait(delay); //some motors might need a longer delay
  }

}

并按如下方式调用它:

motorStep(350, 360000L/350) ;

我手头没有 espruino,所以我只能希望这行得通。

于 2016-06-14T18:50:01.517 回答