0

我正在尝试使用 EasyDriver、NEMA17 步进电机、电位器和 arduino 构建一个可变速的小型旋转台。我在使用代码时遇到了一些问题。基本上,电机以与电位器位置无关的相同速度旋转。当我将电位器放在末端位置附近时,电机也开始旋转。并且只在那个位置不断旋转。

我检查了所有硬件组件,它们工作正常。我想问题出在代码中。将 LED 二极管放在步进引脚上,我看到 arduino 工作正常,但步进电机没有相应移动。

void setup() {
  // put your setup code here, to run once:

  //STEP PIN
  pinMode (13, OUTPUT);

  //POTPIN
  pinMode (A5, INPUT);

  //DIRPIN
  pinMode (12, OUTPUT);

  digitalWrite (12, LOW); 
}

void loop() {
  // put your main code here, to run repeatedly:

  int potValue =  analogRead (A5) / 8;

  digitalWrite (13, HIGH);

  delay (potValue);

  digitalWrite (13, LOW);

  delay (potValue);
}
4

1 回答 1

0

I believe that your problem is using the delay() function with wrong range of values for this type of motor driver.

Your delay represents the time to make a step, and your rotor speed depends on how many steps has your motor. I recommend playing with delays between 10 and 2000 microsseconds to begin with. Your current delay function is being set between 0 and 128 milliseconds, which is a lot slower than your motor's requirements.

To solve this problem, try using the map() to correctly set up the delay for your motor. I also recommend, instead of delay(), use delayMicroseconds() function.

Basicaly, you can transform your potentiometer values to a correct delay value using:

int delayValue= map(potValue, 0, 1023, 10, 2000);

Your code will be something like the code below:

void setup() {
  pinMode (13, OUTPUT); //STEP PIN
  pinMode (A5, INPUT); //POTPIN
  pinMode (12, OUTPUT); //DIRPIN
  digitalWrite (12, LOW); 
}

void loop() {
  int potValue =  analogRead (A5); // Read from potentiometer
  int delayValue= map(potValue, 0, 1023, 10,2000); // Map potentiometer value to a value from 300 to 4000

  digitalWrite (13, HIGH);
  delayMicroseconds(delayValue);
  digitalWrite (13, LOW);
  delayMicroseconds(delayValue);
}
于 2019-08-21T12:50:20.000 回答