首页 > 解决方案 > Arduino:为什么我的步进器不反向移动?

问题描述

我已经通过使用一些非库示例代码验证了它不是接线问题,所以看来问题不在于电路。

我期待下面的代码使电机在一个方向上运行 200 步,然后在相反方向上运行 200 步。它似乎运行了最初的 200 步,但随后停止,为什么?

setup函数中:

stepper.setCurrentPosition(0);
// Set the maximum speed in steps per second:
stepper.setMaxSpeed(1000);  

loop函数中:

 while(stepper.currentPosition() < 300){
   stepper.moveTo(200);
   stepper.setSpeed(200);
   stepper.run();
   
  if (stepper.distanceToGo() == 0) {
      stepper.moveTo(-stepper.currentPosition());
      stepper.setSpeed(200);
      stepper.run();
  }

 }

这是我的整个代码。if/else底部是我要运行电机的地方。我在这个块中放入了一些不工作的示例代码作为示例:

#include <deprecated.h>
#include <MFRC522.h>
#include <MFRC522Extended.h>
#include <require_cpp11.h>
// Include the AccelStepper library:
#include <AccelStepper.h>


// constants won't change. They're used here to set pin numbers:
const int BUTTON_PIN = 2; // the number of the pushbutton pin
#define BLUE 7
#define GREEN 6
#define RED 3
#define RST_PIN 9     // Configurable, see typical pin layout above
#define SS_PIN  53   // Configurable, see typical pin layout above


#define dirPin 10
#define stepPin 11
#define motorInterfaceType 1


// Variables will change:
String authKeyFob = "123456789";
String card_ID="";
int ledState         = 0;     // remember current led state
int buttonState      = 0;  
int oldButtonState = 0;
bool toggle = false;

MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);

void setup() {

  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
 
  while (!Serial);     // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
  SPI.begin();         // Init SPI bus
  mfrc522.PCD_Init();  // Init MFRC522 card


  // initialize the pushbutton pin as an pull-up input
  // the pull-up input pin will be HIGH when the switch is open and LOW when the switch is closed.
  pinMode(BUTTON_PIN, INPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(RED, OUTPUT);
  pinMode(BLUE, OUTPUT);

  stepper.setCurrentPosition(0);
  stepper.setMaxSpeed(1000);
  stepper.setAcceleration(50);
 // stepper.setSpeed(200);
 // stepper.moveTo(200);
  
}

void loop() {
 
/*
  // Change direction once the motor reaches target position
  if (stepper.distanceToGo() == 0) {
    stepper.moveTo(-stepper.currentPosition());
  }
  // Move the motor one step
  stepper.run();
*/
  buttonState = digitalRead(BUTTON_PIN);


  // read the state of the switch/button:
  oldButtonState = LOW;    //  NEED TO ADD A DECLARATION FOR THIS BEFORE SETUP
  buttonState = digitalRead(BUTTON_PIN);

  // Look for new cards, and select one if present
  if (!mfrc522.PICC_IsNewCardPresent() ) {

  }
 

  if(mfrc522.PICC_ReadCardSerial() ){
      card_ID="";
      for (byte i = 0; i < mfrc522.uid.size; i++) {
         card_ID += mfrc522.uid.uidByte[i];
      }

      if(card_ID == authKeyFob){
        toggle = !toggle;
        delay(200);
      }
  }

  //  if the button just became pressed...
  if(buttonState == HIGH && oldButtonState==LOW){
    toggle = !toggle;  // same thing, toggle our variable. 
    delay(200);
  }
  

  if (toggle) {
    digitalWrite(GREEN, HIGH);
    digitalWrite(RED, LOW);
    stepper.moveTo(200);
    stepper.setSpeed(200);
    stepper.run();
       
  } else {
    digitalWrite(GREEN, LOW);
    digitalWrite(RED, HIGH);
    stepper.moveTo(200);
    stepper.setSpeed(200);
    stepper.run();

  }
  oldButtonState = buttonState;  // save the button state for next time

}

标签: arduino

解决方案


run 方法不会阻塞。如果时间到了,它只需要一步。因此,让我们逐步检查您的代码。首先我们进入 while 循环并告诉它步进到 200 并设置速度并调用 run。我们一遍又一遍地重复这一点。如果我们到达一个我们有 0 的点,那么你说 moveTo 负位置,设置速度,调用 run 一次,然后 while 循环退出并重复。在那个重复中,我们说移动到 200 并开始调用运行。因此,您只需将 moveTo 设置为负数调用一次运行。

你需要重新思考这方面的逻辑。我不知道你的循环中还有什么,所以很难说你到底想要什么。如果你真的坚持它在 while 循环中被阻塞,那么:

stepper.moveTo(200);
stepper.setSpeed(200);
while(stepper.distanceToGo() != 0){
   stepper.run();
}
stepper.moveTo(-200);
while(stepper.distanceToGo != 0){
   stepper.run();
}

推荐阅读