简体   繁体   中英

How to move forward and backward with the Arduino Stepper motor?

I want to create an Arduino program for a system that has three buttons and one stepper motor. If button 1 is pressed, the stepper should go for example 50 steps forward. If button 2 is pressed, the stepper should go 50 steps backward. If button 3 is pressed, the stepper should go 50 steps forward after that 50 steps backward.

I used Arduino's stepper library and wrote the following code. The functions Forward() , Backward() and Continuous() implement the actions to be performed for each button. Each function moves the motor step by step and logs the action on a serial output.

But I could not achieve my desired results: The stepper does not go backward but only goes forward. More precisely:

  • Forward() works as expected
  • Backward() produces the expected logging output (counting the steps up to 50), but the motor only moves forward instead of backward.
  • Continuous() function is not working either: after 50 step forward (moving and logging the step count), it continues moving forward logging just 1 for the counter.

I need your help. How can I make the motor going backwards in Backward() ? And how to correct Continuous() to achieve move forward and backward, and producing the right backward step count?

Here my code:

#include <Stepper.h>

int forward_button = 2;
int backward_button = 3;
int cont_button = 4;

int button_cond1;
int button_cond2;
int button_cond3;

int del = 50;

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
int stepCount = 0;         // number of steps the motor has taken
int steps;

void Forward() { // should go forward by 50 steps
  int stepCount = 0; 
  while (stepCount < 50) {
    steps = 1;
    myStepper.step(steps);
    stepCount++;
    delay(del);
    Serial.print("Forward steps :");
    Serial.println(stepCount);
  }
}

void Backward() { // should go backward by 50 steps
   int stepCount = 0;
   while (stepCount < 50) {
    steps = 1;
    myStepper.step(steps);
    stepCount++;
    delay(del);
    Serial.print("Backward steps :");
    Serial.println(stepCount);
  }
}

void Continuous() {  // should go forward by 50 steps, then backwards
  int stepCount = 0; 
  while (stepCount < 50) {
    steps = 1;
    myStepper.step(steps);
    stepCount++;
    delay(del);
    Serial.print("Continuous steps :");
    Serial.println(stepCount);
  }
  while (50 < stepCount <= 200) {
    int stepCount = 0;
    steps = 1;
    myStepper.step(steps);
    stepCount++;
    delay(del);
    Serial.print("Continuous steps :");
    Serial.println(stepCount);
  }
}

void setup() {
  // initialize the serial port:
  Serial.begin(9600);

  pinMode(forward_button, INPUT_PULLUP);
  pinMode(backward_button, INPUT_PULLUP);
  pinMode(cont_button, INPUT_PULLUP);

  //myStepper.setSpeed(60);
}

void loop() {
  // step one step:

  button_cond1 = digitalRead(forward_button);
  button_cond2 = digitalRead(backward_button);
  button_cond3 = digitalRead(cont_button);

  if ((button_cond1 == LOW) && (button_cond2 == HIGH) && (button_cond3 == HIGH))  {
    Forward();
  }
  else if ((button_cond1 == HIGH) && (button_cond2 == LOW) && (button_cond3 == HIGH))  {
    Backward();
  }
  else if ((button_cond1 == HIGH) && (button_cond2 == HIGH) && (button_cond3 == LOW))  {
    Continuous();
  } 
}

Issue with the direction of the stepper:

According to the documentation, myStepper.step() turns the motor:

Turns the motor a specific number of steps, at a speed determined by the most recent call to setSpeed().

And more importantly, to move in both directions, it uses a signed argument:

the number of steps to turn the motor - positive to turn one direction, negative to turn the other (int)

In your code, there is no difference between Backward() and Forward() : you provide positive values in both cases. So I suggest to change Backwards() to use a negative number:

myStepper.step(-steps); 

Issues with the logic of Continuous()

The direction needs also to be corrected in the second loop of your Continuous() function, where you want to go backwards.
But first, you need to correct this loop's condition:

while (50 < stepCount <= 200) {  //OUCH compares a boolean and an integer

C++ does not chain the comparison operators as you think. Therefore break it up in two distinct comparisons grouped with a logical and :

while (50 < stepCount && stepCount <= 200) { 

Unfortunately, at the end of the first loop stepCount is exactly 50 . And 50 is not strictly smaller ( < ) than 50. You could use a smaller or equal ( <= ) instead, but since the loop does not decrement the counter, you could simplify the condition to:

while (stepCount <= 200) { // we know that it will stay 50 or above

You also need to delete the redefinition of stepCount in this loop's block, since it creates a new local variable that hides the initial definition (ie one name, two variables, that's very confusing).

If it still does not work

Following the long exchange of comments, here some advice (in bold the one that solved the issue):

  • Make sure the latest version of the code runs on the arduino and there's no compilation error or warning.
  • Make sure that the different buttons execute the expected function.
  • If the motor movements are not as expected, make sure that you Stepper object is configured with a number of steps per rotation that is compatible with the motor.
  • If the movements are erratic, or if there is no difference between positive and negative steps (which should according to the documentation move in opposite directions), then cross check that the Stepper 's constructor gets the pin numbers in the right order : this is important, because Stepper will send a sequence of signals to the different pins in a given order to achieve the right motion.

If it still does not work there is either a hardware issue, or an incompatibility with the library (see for example here , where the authors solved the problem by providing another signal sequences, which is not the easiest way).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM