arduino 的速度、时间、周期和方向步进电机

speed,time,cycle and direction stepper motor by arduino

我想让我的步进电机运行以指定的速度运行指定的时间并停止指定的时间,然后重复相同的循环。 事实上,用户必须这样做的次数将决定用户 我有最后一个失败的代码,但是它不能正常工作超过 30 秒并且步进电机永久旋转。

#include <Stepper.h>
const int stepPin = 6;  //PUL -Pulse
const int stepsPerRevolution = 1600;
const int dirPin = 7; //DIR -Direction
const int enPin = 8;  //ENA -Enable
int one = 30000;//user input
int c = 2;// user input
int rpm = 1200;//user input
unsigned long t = 0;
Stepper myStepper(stepsPerRevolution, 6, 7);

void setup() {
  Serial.begin(9600);
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(enPin, OUTPUT);
  digitalWrite(enPin, LOW);
  myStepper.setSpeed(rpm);
}
void loop() {
  Cycle();
  digitalWrite(enPin, HIGH);
}
void Cycle() {

  int cycle = 1;
  for (cycle ; cycle <= c; cycle++) {

    t = millis();
    while ((millis() - t) < one) {
      myStepper.step(stepsPerRevolution);//counter clockwise rotation

    }
    delay(3000);
  }

}
int one = 30000;//user input

如果您打算使用 int 作为计时变量,那么您需要查看 int 在您的特定板上可以容纳的最大值是多少。如果您使用的是 UNO 或 Mega 之类的东西,那么它就是 32767,这可能会解释为什么它对超过 32 秒的任何东西都不起作用。