Go Down

Topic: 2 different directions of 2 stepper motors (Read 120 times) previous topic - next topic

obiektofilia

I'm working on my first project with Arduino and need your help with setting my first model. I follow exactly step by step this tutorial http://moving-electrons.blogspot.com/2015/07/driving-multiple-stepper-motors.html and its working! I succeeded changing the speed of the motors but now I struggling to change the direction of one of the stepper motor. One goes forward and the other goes reverse. Does anyone could help me?

Code: [Select]

#include "Arduino.h"

// enumeration used to define the direction the motor
// is driven
enum MotorDirection {
  MotorDirectionForward = 1,
  MotorDirectionReverse = -1
};

// StepperMotor class is used to asynchronously
// drive a stepper motor.  The motor is driven using
// full-stepping where 2 coils are active during any
// one cycle. 
class Motor {
private:

  // the pin values represent which GPIO pin on
  // the microcontroller output the motor is attached to
  int pin1;
  int pin2;
  int pin3;
  int pin4;
 
  // the direction the motor is driving
  MotorDirection dir;

  // how long to wait in microseconds between state changes
  // on the output pins
  unsigned long cycleDuration;

  // which step the motor is currently on
  int step;

  // the last time the motor was stepped
  unsigned long lastStepTime;

public:
  Motor(int p1, int p2, int p3, int p4) :
    pin1(p1), pin2(p2), pin3(p3), pin4(p4),
    dir(MotorDirectionForward),
    cycleDuration(2000),
    step(0),
    lastStepTime(0)
  {
    // set all the pins on the motor to OUTPUT mode
    pinMode(pin1, OUTPUT);
    pinMode(pin2, OUTPUT);
    pinMode(pin3, OUTPUT);
    pinMode(pin4, OUTPUT);
  }

  void setDirection(MotorDirection d) {
    dir = d;
  }

  void setCycleDuration(unsigned long duration) {
    cycleDuration = duration;
  }

  // call tick frequently to update the motor state
  void tick();

private:

  // update the current step and set the pin voltages
  void doStep();

  // increment (or decrement) the step depending on the
  // direction and set the new step modulus 4
  void advanceStep();
};

void Motor::tick() {
  unsigned long now = micros();
 
  if (now - lastStepTime > cycleDuration) {
    doStep();
    lastStepTime = now;
  }
}

void Motor::advanceStep() {
  step = step + dir;

  if (step == 4)  step = 0;
  if (step == -1) step = 3;
}

void Motor::doStep() {
  advanceStep();

  switch (step) {
  case 0:  // 1010
    digitalWrite(pin1, HIGH);
    digitalWrite(pin2, LOW);
    digitalWrite(pin3, HIGH);
    digitalWrite(pin4, LOW);
    break;
  case 1:    // 0110
    digitalWrite(pin1, LOW);
    digitalWrite(pin2, HIGH);
    digitalWrite(pin3, HIGH);
    digitalWrite(pin4, LOW);
    break;
  case 2:    //0101
    digitalWrite(pin1, LOW);
    digitalWrite(pin2, HIGH);
    digitalWrite(pin3, LOW);
    digitalWrite(pin4, HIGH);
    break;
  case 3:    //1001
    digitalWrite(pin1, HIGH);
    digitalWrite(pin2, LOW);
    digitalWrite(pin3, LOW);
    digitalWrite(pin4, HIGH);
    break;
  }
}

Motor *m1 = NULL, *m2 = NULL;

void setup() {
  m1 = new Motor(4, 6, 5, 7);
  m1->setCycleDuration(4600);
  m2 = new Motor(8, 10, 9, 11);
  m2->setCycleDuration(4600);
}

void loop() {
  m1->tick();
  m2->tick();
  delayMicroseconds(10);
}



Paul_KD7HB

I'm working on my first project with Arduino and need your help with setting my first model. I follow exactly step by step this tutorial http://moving-electrons.blogspot.com/2015/07/driving-multiple-stepper-motors.html and its working! I succeeded changing the speed of the motors but now I struggling to change the direction of one of the stepper motor. One goes forward and the other goes reverse. Does anyone could help me?

Code: [Select]

#include "Arduino.h"

// enumeration used to define the direction the motor
// is driven
enum MotorDirection {
  MotorDirectionForward = 1,
  MotorDirectionReverse = -1
};

// StepperMotor class is used to asynchronously
// drive a stepper motor.  The motor is driven using
// full-stepping where 2 coils are active during any
// one cycle. 
class Motor {
private:

  // the pin values represent which GPIO pin on
  // the microcontroller output the motor is attached to
  int pin1;
  int pin2;
  int pin3;
  int pin4;
 
  // the direction the motor is driving
  MotorDirection dir;

  // how long to wait in microseconds between state changes
  // on the output pins
  unsigned long cycleDuration;

  // which step the motor is currently on
  int step;

  // the last time the motor was stepped
  unsigned long lastStepTime;

public:
  Motor(int p1, int p2, int p3, int p4) :
    pin1(p1), pin2(p2), pin3(p3), pin4(p4),
    dir(MotorDirectionForward),
    cycleDuration(2000),
    step(0),
    lastStepTime(0)
  {
    // set all the pins on the motor to OUTPUT mode
    pinMode(pin1, OUTPUT);
    pinMode(pin2, OUTPUT);
    pinMode(pin3, OUTPUT);
    pinMode(pin4, OUTPUT);
  }

  void setDirection(MotorDirection d) {
    dir = d;
  }

  void setCycleDuration(unsigned long duration) {
    cycleDuration = duration;
  }

  // call tick frequently to update the motor state
  void tick();

private:

  // update the current step and set the pin voltages
  void doStep();

  // increment (or decrement) the step depending on the
  // direction and set the new step modulus 4
  void advanceStep();
};

void Motor::tick() {
  unsigned long now = micros();
 
  if (now - lastStepTime > cycleDuration) {
    doStep();
    lastStepTime = now;
  }
}

void Motor::advanceStep() {
  step = step + dir;

  if (step == 4)  step = 0;
  if (step == -1) step = 3;
}

void Motor::doStep() {
  advanceStep();

  switch (step) {
  case 0:  // 1010
    digitalWrite(pin1, HIGH);
    digitalWrite(pin2, LOW);
    digitalWrite(pin3, HIGH);
    digitalWrite(pin4, LOW);
    break;
  case 1:    // 0110
    digitalWrite(pin1, LOW);
    digitalWrite(pin2, HIGH);
    digitalWrite(pin3, HIGH);
    digitalWrite(pin4, LOW);
    break;
  case 2:    //0101
    digitalWrite(pin1, LOW);
    digitalWrite(pin2, HIGH);
    digitalWrite(pin3, LOW);
    digitalWrite(pin4, HIGH);
    break;
  case 3:    //1001
    digitalWrite(pin1, HIGH);
    digitalWrite(pin2, LOW);
    digitalWrite(pin3, LOW);
    digitalWrite(pin4, HIGH);
    break;
  }
}

Motor *m1 = NULL, *m2 = NULL;

void setup() {
  m1 = new Motor(4, 6, 5, 7);
  m1->setCycleDuration(4600);
  m2 = new Motor(8, 10, 9, 11);
  m2->setCycleDuration(4600);
}

void loop() {
  m1->tick();
  m2->tick();
  delayMicroseconds(10);
}



What have you tried?


Paul

PaulS

Quote
One goes forward and the other goes reverse. Does anyone could help me?
If that code makes one motor move one direction and the other motor move the other direction, then you have one the motors wired differently. Make sure that both motors are connected the same way.

It appears that the Arduino is powering the stepper motors. That's not the case, is it?

Using a proper stepper driver, with two pins - one for direction and one to trigger e single step - is a MUCH better idea than what you are using now.
The art of getting good answers lies in asking good questions.

obiektofilia

#3
Apr 18, 2019, 01:40 pm Last Edit: Apr 18, 2019, 01:42 pm by obiektofilia
Thank you so much for reply. Currently they both move in the same direction - my goal is to run one of the stepper motor move in the opposite direction. They all powered by 9V battery (which is stupid idea, because the battery runs out quickly but I don't know how to change it for different source of power). I did try to code this changing the code but without success as is my first ardurino project and I don't understand most of it (I'm learning and reading a lot but also start already trying - thats why I ask so simple questions here).

PaulS

Quote
Currently they both move in the same direction
You have a setDirection() method in your class. You never call it. If it were me, I would call that method for each instance, so I knew which way the motor was going to step, and I'd use different values for each call, if I wanted them to rotate in different directions.
The art of getting good answers lies in asking good questions.

obiektofilia

OK, thank you! Will try to understand that and do that way. I found now temporary solution inspired by your previous response: I change the order of pins in a code from 8, 10, 9, 11 to 11, 9, 10, 8. It works. But know I try to understand how to setDirection i make it clear. Thank you for the help!

Go Up